Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Get filepath from external connection

$
0
0

I am trying to iterate over all Excel files in a directory and collect all references between them, to determine which ones are safe to move to a different location.

As a first step, I want to find the references in a single file. I tried doing that in VBS, but intermediately moved to VBA to have code-completion.

I tried to use Workbook.LinkSources which is the solution you will find everywhere on the internet - but I never got a single result, until I realized that Excel distinguishes between "Links" (that I had queried here) and "Connections" (which is what I have in my test-file).

I found the array Workbook.Connections - does contain the connections I am looking for BUT when I go into the class hierarchy, I can't find a property that contains the filename, which is what I'm looking for.

In this discussion I found this PowerShell code: $workbook.ActiveSheet.QueryTables | Format-Table Name,Connection which seems to do what I want and my workbook does contain a property "ActiveSheet" but code completion doesn't give me any children for it and when I try to access its QueryTables, I get a runtime error. (I'm using 2010, maybe this is for a newer version of Excel?). I tried looking into that object via the debugger, but no success.

The Connection objects have two properties ODBCConnection and OLEDBConnection which look like they should contain the information I'm looking for, but when I try to output their properties SourceConnectionFile, SourceDataFile, etc. I get respectively

runtime error 445 object doesn't support this action

or

runtime error 438 object doesn't support this property or method

This is the code I have so far (pathetic, I know):

Sub foo()
    For i = 1 To Workbook2.Connections.Count
        'MsgBox "Link "& i & ":"& Chr(13) & Workbook2.Connections(i).ODBCDBConnection.SourceConnectionFile
        'MsgBox "Link "& i & ":"& Chr(13) & Workbook2.Connections(i).ODBCDBConnection.SourceDataFile
        'MsgBox "Link "& i & ":"& Chr(13) & Workbook2.Connections(i).OLEDBConnection.SourceConnectionFile
        'MsgBox "Link "& i & ":"& Chr(13) & Workbook2.Connections(i).OLEDBConnection.SourceDataFile
    Next i
End Sub

Viewing all articles
Browse latest Browse all 88835

Trending Articles