0 votes
in VBA by Expert (916 points)
edited by

Hello everyone

I have found a code that it should create xml file and it uses ADO ..

At this line

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
        & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

I got provider error and I have searched and found that oledb is suitable for 64 Bit

I found the download link


I have changed the Provider part to be like that

strCon = "Provider=MSOLEDBSQL;Data Source=" & strFile _
        & ";HDR=Yes;IMEX=1"";"

Now the error is different .. I got the error at this line

cn.Open strCon

Which tells me invalid connection string attribute


This is the whole code

Sub Test()
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adPersistXML = 1

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

''It wuld probably be better to use the proper name, but this is
''convenient for notes
strFile = ThisWorkbook.FullName

''Note HDR=Yes, so you can use the names in the first row of the set
''to refer to columns, note also that you will need a different connection
''string for >=2007
'strCon = "Provider=MSOLEDBSQL;Data Source=" & strFile & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1;"
'strCon = "Provider=MSOLEDBSQL;Data Source=" & strFile _
'        & ";HDR=Yes;IMEX=1;"

strCon = "Provider=MSOLEDBSQL;Data Source=""" & strFile _
        & """;HDR=Yes;IMEX=1;"

cn.Open strCon
rs.Open "Select * from [Sheet1$]", cn, adOpenStatic, adLockOptimistic

If Not rs.EOF Then
    rs.Save "C:\Docs\Table1.xml", adPersistXML
End If

End Sub


Any help please

1 Answer

+1 vote
by Expert (805 points)

Try this:

 & ";HDR=Yes;IMEX=1;"


by Expert (916 points)

Thanks a lot. I got the same error

strCon = "Provider=MSOLEDBSQL;Data Source=" & strFile _
        & ";HDR=Yes;IMEX=1;"

strFile refers to thisworkbook

strFile = ThisWorkbook.FullName

Is this may the cause of the conflict?

by Expert (805 points)

Try this:

strCon = "Provider=MSOLEDBSQL;Data Source=""" & strFile _
        & """;HDR=Yes;IMEX=1;"


by Expert (916 points)
Thanks a lot but it is the same error. I have posted the whole code I found, may be there is a clue in the code itself

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.