Avatar billede webkilla Praktikant
23. november 2016 - 14:24 Der er 7 kommentarer

Fejl 1004 ved data import på .CommandType = 5

Hejo

Jeg er ved at prøve at lave en workbook hvor, når man åbner filen, at den selv henter en liste ned fra en sharepoint server (som så bliver slettet når man er færdig med det)

Via macro recorderen har jeg hittet mig noget kode - og det virker fint når jeg henter data ned manuelt - men koden gider ikke at virke, og jeg kan ikke forstå hvorfor.

Her er min kode:
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
      .CommandType = 5  'Det er den her linje som kaster en fejl 1004 af sig       
        .CommandText = Array( _
        "<LIST><VIEWGUID>{75AB5376-AC5F-444A-A7AE-D984214EA11E}</VIEWGUID><LISTNAME>{A6479E9F-EA76-424D-B9BA-F8C2845F3765}</" _
        , _
        "LISTNAME><LISTWEB>*firma URL i ikke har brug for at se*" _
        , _
        "lding/_vti_bin</LISTWEB><LISTSUBWEB></LISTSUBWEB><ROOTFOLDER>/sites/Engineering/injection_moulding/Equipment/inject" _
        , "ionmoulding/Lists/Tool Overview</ROOTFOLDER></LIST>")
        .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 = "Tool_list"
        .Refresh BackgroundQuery:=False
    End With

End Sub
Avatar billede terry Ekspert
23. november 2016 - 15:00 #1
Avatar billede webkilla Praktikant
23. november 2016 - 15:18 #2
That is a great question - and I'm honestly not entire sure. If I knew that answer, then I wouldn't be here.

https://msdn.microsoft.com/en-us/library/office/ff840602.aspx

From what I can tell then it sets the commandtype to contain a table name for accessing OLE DB data sources. This is relevant for importing data from what I can tell
Avatar billede terry Ekspert
23. november 2016 - 15:48 #3
:-)
If you look at the link I gave you can see

adCmdTable = 2, so I guess you should be using that
Avatar billede webkilla Praktikant
02. december 2016 - 14:17 #4
I think that worked... sort of - now I'm getting a different kind of error

Namely a 1004 on the third last line of the code - at the  .Refresh BackgroundQuery:=False

It also adds that it failed to initialize the data source - which doesn't make sense, since I can still perform the data import manually just fine, using the same source connection file that I'm trying to get the code to use.
Avatar billede terry Ekspert
02. december 2016 - 15:07 #5
Know very little about Excel so not at all sure what the code is supposed to do. and its not easy to simulate without same setup as you have.

I can see others have similar problem,
http://stackoverflow.com/questions/26257363/refresh-backgroundquery-false-error-1004

but PreserveFormatting is already true.
I think what I would do is comment out or remove BackgroundQuery:=False
and see what happens.
Avatar billede terry Ekspert
02. december 2016 - 15:22 #6
Oh, and try setting .PreserveFormatting to False, not sure why though, just guessing ...
Avatar billede webkilla Praktikant
20. december 2016 - 11:24 #7
Hi again - commenting out or removing the BackgroundQuery line doesn't really work. Oh it removes the error, yes, but then the code simply doesn't execute properly either: It it doesn't import any data, it just does nothing.

The formatting command similarly had no impact, true or false
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester