Using VB to merge Access data into a Word Document

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TriciaL
    New Member
    • May 2007
    • 6

    #1

    Using VB to merge Access data into a Word Document

    Hi all, I've been going through the threads on the Mergit() function to run a Word mailmerge out of Access, and I've hit a roadblock.

    The first problem I had was discussed in this thread: http://www.thescripts.com/forum/thre...rd+access.html

    So as instructed, I added a reference to Microsoft Word. On the bright side, at least I'm getting a different error than I was before (but I still can't get it to work)

    Here's the code (I haven't written the error handling part yet)
    Code:
    Dim objDoc As Word.Document
    Dim objWord As Word.Application
    Dim strFilename As String
    Dim strQueryName As String
    Dim strDBPath As String
    
    'DocFileName is a field that users choose which document they want to merge to
    strFilename = "c:\DocumentLibary\" & Me.DocFileName 
    strQueryName = "qryIntendedContractMergeSource"
    strDBPath = "c:\ContractModule.mdb"
    
    Set objDoc = objWord.Documents.Open(strFilename)
    
    objWord.Visible = True
    
    With objDoc.MailMerge
        objDoc.MailMerge.OpenDataSource Name:=strDBPath, _ LinktoSource:=True, Connection:="QUERY" & strQueryName, _ SQLStatement:="Select * from " & strQueryName
        .destination = wdSendToNewDocument
        .Execute
        objWord.ActiveDocument.PrintOut False
        objWord.ActiveDocument.Close wdDoNotSaveChanges
    End With
    The error I'm getting is: 91 - Object Variable or With Block Variable not set. (The debugger highlights the "Set objDoc" line.) As far as I can tell I /have/ set the object variable, and I /have/ initialized the with...end with bit. Or not? I'm pretty new at this, so there's a good chance I'm missing something.

    Thanks for any insight you can provide!
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Try this...

    Dim objWord As New Word.Applicatio n

    Comment

    • TriciaL
      New Member
      • May 2007
      • 6

      #3
      Thanks, that helped make some progress... It now opens the right Word document.

      Then it goes back to error 91 at "with objDoc.Mailmerg e"

      Got any ideas for that one?

      Thanks again!

      Originally posted by Killer42
      Try this...

      Dim objWord As New Word.Applicatio n

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by TriciaL
        Thanks, that helped make some progress... It now opens the right Word document.

        Then it goes back to error 91 at "with objDoc.Mailmerg e"

        Got any ideas for that one?
        That depends. What is error 91?

        Comment

        • TriciaL
          New Member
          • May 2007
          • 6

          #5
          It's the same error as I was getting before - just that it gets a bit further before it stops.

          Error 91 - Object Variable or With Block Variable not set
          Originally posted by Killer42
          That depends. What is error 91?

          Comment

          • mahmed7431
            New Member
            • Nov 2013
            • 1

            #6
            Were you able to find the solution to this problem?



            Originally posted by TriciaL
            It's the same error as I was getting before - just that it gets a bit further before it stops.

            Error 91 - Object Variable or With Block Variable not set

            Comment

            Working...