PDA

Click to See Complete Forum and Search --> : [RESOLVED] Word unable to open datasource


Nynn
Jun 6th, 2006, 09:57 AM
I'm writing a program that takes some simple input from the user via a combo box and a treeview and opens a word document and mail merges the appropriate data from a Microsoft SQL server.

I'm pretty sure I have everything hammered out, except when I run the program I end up with the error that Word was unable to open the data source.

Unfortunatly I have not found a wealth of solid information out on the Internet with regards to office interop. I've had to piece a lot together to get it this far. :( If anyone has any suggestions as to why it's unable to open the data source with the following code it would be most appreciated.

On a side note, I'd love to know of a good book or reference that shows more of the office interop with VB and .net.

Thanks!

Error:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in VBPorcupineFrontEnd.exe

Additional information: Word was unable to open the data source.

Code:


Dim docPath As String = "C:\porcupine\docs\"
Dim wrdApp As New Word.Application
Dim wrdDoc As New Word.Document
Dim TemplateFile As String = docPath + TreeView1.SelectedNode.Text & ".doc"

Dim oMissing As New Object
Dim oFile As New Object
Dim oSql As New Object
Dim oFalse = False
Dim oTrue = True
Dim ODCFile As New Object

ODCFile = "C:\Porcupine\memberdata3.odc"

oFile = TemplateFile
oMissing = System.Reflection.Missing.Value

wrdApp.Visible = True

wrdDoc = wrdApp.Documents.Open(oFile, oMissing, oTrue, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)

wrdDoc.Select()

Dim wrdMailMerge = wrdDoc.MailMerge

oSql = "SELECT * FROM MemberData WHERE MBRNAME =" & ComboBox1.Text

wrdDoc.MailMerge.OpenDataSource(ODCFile, oMissing, oMissing, oFalse, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oSql, oMissing, oFalse, oMissing)

wrdMailMerge.SuppressBlankLines = True

wrdMailMerge.Execute(oFalse)

If wrdDoc Is Nothing Then
wrdDoc.Saved = True
wrdDoc.Close(oFalse, oMissing, oMissing)
End If

'If wrdApp Then
wrdApp.Quit(oFalse, oMissing, oMissing)

'End If


wrdMailMerge = Nothing
wrdDoc = Nothing
wrdApp = Nothing

RobDog888
Jun 6th, 2006, 10:02 AM
Your sql query has criteria that is text based so you need to enclose it in single quotes.
oSql = "SELECT * FROM MemberData WHERE MBRNAME ='" & ComboBox1.Text & "'"Are you using 2003 or 2005?

Nynn
Jun 6th, 2006, 10:44 AM
Your sql query has criteria that is text based so you need to enclose it in single quotes.
oSql = "SELECT * FROM MemberData WHERE MBRNAME ='" & ComboBox1.Text & "'"Are you using 2003 or 2005?


Visual Studio 2003.

Thank you very much, worked like a charm. I knew it would be some mundane detail I was overlooking. :D

I'm still very much interested in a good book or reference material that really delves into what can be done with Office and vb.net. A lot of what we're doing is moving more and more to forms automation with almost ALL of our pre-existing forms being in MS Office formats and all of our data being stored in SQL servers to mash into those forms.

RobDog888
Jun 6th, 2006, 10:45 AM
There is allot of good stuff here on our forums but the .NET stuff is building up still.

I am still trying to finish my Office FAQ which will have .NET code examples too.