Results 1 to 4 of 4

Thread: [RESOLVED] Word unable to open datasource

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    20

    [RESOLVED] Word unable to open datasource

    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:

    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
    Last edited by Nynn; Jun 6th, 2006 at 10:44 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Word unable to open datasource

    Your sql query has criteria that is text based so you need to enclose it in single quotes.
    VB Code:
    1. oSql = "SELECT * FROM MemberData WHERE MBRNAME ='" & ComboBox1.Text & "'"
    Are you using 2003 or 2005?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    20

    Re: Word unable to open datasource

    Quote Originally Posted by RobDog888
    Your sql query has criteria that is text based so you need to enclose it in single quotes.
    VB Code:
    1. 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.

    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.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Word unable to open datasource

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width