如何修改Excel在VBA界面显示的连接名称?

How to modify the name of the connection that Excel shows in the interface in VBA?

这是我在论坛中创建的第一个问题,尽管我已经阅读了一段时间并找到了解决方案。它应该非常简单,但我缺少一些东西。

我正在使用 VBA 在 excel 中创建到几个 URL 的连接。连接的属性“.name”似乎没有正常工作。它确实创建了具有所需名称的连接,但连接列表中显示的连接名称只是标准的“Conection”(西班牙语中的“Conexión”)。

通过使用宏录制器,我发现可以使用相同的“.name”来修改名称。但是还是不行。

这只是我现在遇到问题的部分代码。

GroupURL、QueryString 和 DestinationRange 是变量。 GroupURL 的示例是“Q35G10/”。

With Worksheets(DestinationSheet).QueryTables.Add(Connection:=QueryString, _ 
                                                   Destination:=Range(DestinationRange))    
        'This line just does not work but I do not know what it is for.
        .CommandType = 0    
        'The name has a problematic final character that is deleted this way (Ex: "/").
        .Name = "Dump_" & Left(GroupURL, Len(GroupURL) - 1)  
        
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingAll
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = True
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

这得到了如下图所示的结果link(抱歉,蓝色,被遮盖了)

[

所以我尝试在最后添加以下行再次修改名称:

 Worksheets(DestinationSheet).Connections("Dump_" & Left(GroupURL, Len(GroupURL) - 1)).Name = "Prueba"

它声称:“错误 438。该对象不允许此 属性 或方法。”

谢谢

有两点不同:

  • QueryTable 名称,即正在创建的名称。

      queryTables.Add(...) .name
    
  • Excel 显示的连接名称。由

    管理
      ActiveWorkbook.Connections(i).Name
    

默认情况下,Excel 按字母顺序排列连接,并且只添加名称“Connection”和 CorreltiveNumber。正如您在图像中看到的,无论何时创建连接。这是对您的应用程序设置敏感的语言。

Connections Snapshot

因为我一次创建了多个连接,所以我创建了一个新的子节点来创建它们并一一重命名每个连接,假设名称将是“Connection#”。为此,这是我想出的解决方案,它检查连接名称是否已被使用并且适用于西班牙语和英语。

Private Sub Create_Connection(ConnectionSheet As String, QueryString As String, DestinationRange As String, QueryName As String, ConnectionName As String)

Dim ConnectionCount As Long, i As Long, ConnectionMarker As Long, NamelessConnectionCount As Long

'Create connection
    With Worksheets(ConnectionSheet).QueryTables.Add(Connection:=QueryString, Destination:=Range(DestinationRange))
    'If the connection already exists with the same name, excel adds ... "_#".

    'The Name of the Connection will be the value of the Origin Cell
    .Name = QueryName
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = False
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlAllTables
    .WebFormatting = xlWebFormattingAll
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = True
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

'Check how many deffault name connection exist
ConnectionCount = ActiveWorkbook.Connections.Count
NamelessConnectionCount = 0
For i = 1 To ConnectionCount

If Left(ActiveWorkbook.Connections(i).Name, 8) = "Conexión" Or _
    Left(ActiveWorkbook.Connections(i).Name, 10) = "Connection" Then
        ConnectionMarker = i
        NamelessConnectionCount = NamelessConnectionCount + 1
End If
Next

'Change the name of the connection to a controlled one.
    If NamelessConnectionCount = 1 Then
        For i = 1 To ConnectionCount
            If ActiveWorkbook.Connections(i).Name = ConnectionName Then
                    MsgBox "The desired connection name " & ConnectionName & " is already in use." & vbCrLf & _
                    "Connection created with deffault name.", vbOKOnly, "Create Connection Error"
                    GoTo Label
            End If
        Next
        
        With ActiveWorkbook.Connections(ConnectionMarker)
            .Name = ConnectionName
            .Description = ""
        End With
    Else
        MsgBox "Error creating connection. The number of Nameless connections is not 1" & vbCrLf & _
                "Connection created with deffault name.", vbOKOnly, "Create Connection Error"
    End If

'Update connection
    ActiveWorkbook.Connections(ConnectionName).Refresh

Label:
End Sub