Object Variable Or With Block Variable Not Set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • confused99
    New Member
    • May 2007
    • 9

    Object Variable Or With Block Variable Not Set

    Hi everybody... I've been working on moving access data to a word document for the last 10 days but keep getting stuck. The latest problem is that i'm getting the error "Object Variable Or With Block Variable Not Set". The code is as follows.

    Dim objWord As New Word.Document
    Set objWord=GetObje ct("C:\Abc.doc" )
    objWord.Applica tion.Visible=Tr ue

    objWord.MailMer ge.OpenDataSour ce _
    Name:=CurrentDb .Name, _
    LinkToSource:=T rue, _
    Connection:="TA BLE BasicDetails", _
    SQLStatement:=" Select * FROM BasicDetails"
    objWord.MailMer ge.Execute


    Also, is there any other way to send data from a form to a Word Document (where the form is displaying certain rows from a table)?
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    It doesn't look like the error is in this piece of code. This error normally occurs when you haven't closed a loop, if statement, etc.

    Post the full code of this procedure if you can't find it.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by confused99
      Hi everybody... I've been working on moving access data to a word document for the last 10 days but keep getting stuck. The latest problem is that i'm getting the error "Object Variable Or With Block Variable Not Set". The code is as follows.

      Dim objWord As New Word.Document
      Set objWord=GetObje ct("C:\Abc.doc" )
      objWord.Applica tion.Visible=Tr ue

      objWord.MailMer ge.OpenDataSour ce _
      Name:=CurrentDb .Name, _
      LinkToSource:=T rue, _
      Connection:="TA BLE BasicDetails", _
      SQLStatement:=" Select * FROM BasicDetails"
      objWord.MailMer ge.Execute


      Also, is there any other way to send data from a form to a Word Document (where the form is displaying certain rows from a table)?
      Try making your Declaration in a Form Module or Standard Code Module. It looks as though objWord is declared in a Local Procedure, its Reference to the Word Document may be going out of scope (after the Procedure ends) before the code has time to fully execute. This could definately cause an "Object Variable Or With Block Variable Not Set" Error.
      [CODE=vb]'In a Form Code Module
      Dim objWord As New Word.Document

      'In a Standard Code Module (Scope = Global)
      Public objWord As New Word.Document[/CODE]

      Comment

      Working...