Error 1004 from a .CommandType = 5 line for data import

webkilla

New Member
Joined
Nov 23, 2016
Messages
1
Greetings wise ones

I have been burdened with glorious purpose by my boss, to create an excell file that auto-imports a big ol' list of tables from our sharepoint database, every time the document is opened - mainly to use it as an auto-generating list which is then to be saved into a new file so the original remains blank.

Problem is that the code I've generated via the macro recorder doesn't seem to be working - and this is way above my usual level of just using excell like a really handy mass calculator and graph maker. I know how to use the macro recorder, but I don't understand everything it cranks out, so debugging that is an issue for me.

My code appears as follows: (with the line throwing off the 1004 highlighted)

I should note that currently the code is set up in a macro, tied to a button, so I don't have to open and close the workbook constantly to test things.
Code:
Sub Import_List()
'
' Import_List Macro
' This code automatically imports the tool list in the right view
''
    With Sheets("Sheet1").ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Office.List.OLEDB.2.0;Data Source="""";ApplicationName=Excel;Version=12.0.0.0" _
        , Destination:=Range("$A$1")).QueryTable
[B]        .CommandType = 5 'This is the line that throws off the error. I have no clue why. From what I can tell its a pointer to list data. [/B]
        .CommandText = Array( _
        "<list><viewguid>{75AB5376-AC5F-444A-A7AE-D984214EA11E}</viewguid><listname>{A6479E9F-EA76-424D-B9BA-F8C2845F3765}        , _
        "LISTNAME><listweb>http://teamsite.connect.coloplast.com/sites/Engineering/injection_moulding/Equipment/injectionmou" _
        , _
        "lding/_vti_bin</listweb><listsubweb></listsubweb><rootfolder>/sites/Engineering/injection_moulding/Equipment/inject" _
        , "ionmoulding/Lists/Tool Overview</rootfolder></listname>")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .SourceConnectionFile = "C:\Users\DKALE\Desktop\owssvr.odc"
        .ListObject.DisplayName = "Table_owssvr"
        .Refresh BackgroundQuery:=False
    End With
End Sub

As mentioned previously, then I used the macro recorder to produce the code - and when I use that, it also imports the data just fine. Its when I copy that code onto a new document with the same source connection file set up in advance, basically a file identical to the one I generate the code in, and trigger the import via the button that it wont work.

It also gives the same error if I just delete the imported tables and push the button.

Does anyone have any clues what might be wrong, or how I can make this work?</list>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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