how to import data using .odc file in Excel 2013 VBA

stonepreston

New Member
Joined
Jul 7, 2015
Messages
2
I am trying to write some VBA that will import some data into a query table using a .odc file that is stored in a sharepoint data connection library. I used the macro recorder to record the process where I add the connection, then go to the existing connections and import the data into a table in the current worksheet(which worked when I did it manually).
The recorder spit out the following code (I removed the command text since it contains some sensitive info, but it was a big string of sharepoint related stuff like the list and view GUID):

Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">[COLOR=#00008B]Sub[/COLOR][COLOR=#000000] RecordedImportMacro[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]

    Workbooks[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"MyWorkbook.xlsm"[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Connections[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AddFromFile _
        [/COLOR][COLOR=#800000]"http://path/to/my/odcfile/on/sharepoint.odc"[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000] ActiveSheet[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ListObjects[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]SourceType[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Source[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#000000] _
        [/COLOR][COLOR=#800000]"OLEDB;Provider=Microsoft.Office.List.OLEDB.2.0;Data Source="""";ApplicationName=Excel;Version=12.0.0.0"[/COLOR][COLOR=#000000] _
        [/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Destination[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#000000]Range[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"$A$1"[/COLOR][COLOR=#000000])).[/COLOR][COLOR=#000000]QueryTable
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]CommandType [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]5[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]CommandText [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]"some command text here"[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]RowNumbers [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]FillAdjacentFormulas [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]PreserveFormatting [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]RefreshOnFileOpen [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]BackgroundQuery [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]RefreshStyle [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] xlInsertDeleteCells
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]SavePassword [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]SaveData [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AdjustColumnWidth [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]RefreshPeriod [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]PreserveColumnInfo [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]SourceConnectionFile [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]"http://path/to/my/odcfile/on/sharepoint.odc"[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ListObject[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]DisplayName [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]"My_Table"[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]With[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Sub[/COLOR]</code>

However, when I run the macro to seemingly perform the exact same task that worked before I get the following error: Run time error 1004. I googled and it didnt really find anything that pertained to my use case
when I debug the following line is highlighted: .CommandType = 5
Any ideas on how to get this working?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I was able to get it working using code found here: http://www.xtremevbtalk.com/archive/index.php/t-284863.html


Below is the code:


Code:
Sub ThisWorks()


        With ActiveSheet.QueryTables.Add(Connection:= _
            "FINDER;http://path/to/my/odcfile/on/sharepoint.odc" _
            , Destination:=Range("$A$1"))
            .RefreshStyle = xlInsertDeleteCells
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .RefreshOnFileOpen = True
            .HasAutoFormat = True
            .BackgroundQuery = True
            .TablesOnlyFromHTML = True
            .Refresh BackgroundQuery:=False
            .SavePassword = False
            .SaveData = True
        End With
    
    End Sub
 
Upvote 0

Forum statistics

Threads
1,223,102
Messages
6,170,122
Members
452,303
Latest member
c4cstore

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top