Página 1 de 1

Apagar Conexões

Enviado: 14 Jan 2019 às 12:39
por GustavoFV
Boa Tarde a todos,
Me passaram um problema que até então eu consegui descobrir mas não automatiza-la já na própria macro.
Abaixo esta o código da mesma:
Código: Selecionar todos
 
ActiveWorkbook.Queries.Add Name:="pd002r00", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Fonte = Table.FromColumns({Lines.FromBinary(File.Contents(""Z:\PCP\CTG\CRONOGRAMAS\ACESSRIOS\PEAS SOBRESSALENTES\Regulador\Relatrios\pd002r00.txt""), null, null, 1252)})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Fonte"
            Dim Nome As String

ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=pd002r00;Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [pd002r00]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "pd002r00"
        .Refresh BackgroundQuery:=False
    End With

 Application.CommandBars("Queries and Connections").Visible = True
O problema é o seguinte, este comando inicia um conexão com o "pd002" e imprimi as informações dele no Excel, mas ao rodar novamente a macro acontece o erro de que esta conexão já esta em uso. Mexendo um pouco descobri que esta conexão esta em "Queries and Connections" (ate incrementei o código para torna-la visível) e que é preciso exclui-la para a macro funcionar novamente.
Meu objetivo é excluir a conexão automaticamente para que o usuário não tenha que interagir no processo.
Poderiam me ajudar?
Desde já, Agradeço.