0 votes
in VBA by Expert (754 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 Skilled (366 points)

Try this:

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


by Expert (754 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 Skilled (366 points)

Try this:

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


by Expert (754 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
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.


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