MS Access VBA, error '91:Object variable or with block variable not set'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akaess
    New Member
    • Mar 2008
    • 2

    #1

    MS Access VBA, error '91:Object variable or with block variable not set'

    Hey everybody I am currently trying to merger data from a form in MS access but it keeps giving me the error 91: Object variable or with block variable not set. could somebody please look at the code below and please guide me to a solution. Thank you!

    [code=vb]'************** *************** *************** *************** *****
    'MailMerge method opens specified Word document and merges fields
    'specified within the document with the Access fields located in
    'the specified query FRM00034
    '************** *************** *************** *************** *****
    Public Sub MailMerge034()
    Dim strPath As String
    Dim strDataSource As String

    Dim doc As Word.Document
    Dim wrdApp As Word.Applicatio n

    On Error GoTo HandleErrors
    ' can't use CurrentProject. Path in Access 97
    strPath = GetCurrentDBPat h
    ' Delete the rtf file, if it already exists.
    strDataSource = strPath & "MAILMERGEQUERI ES\" & conQuery034 & rel_Base_Mask & ".doc"
    Kill strDataSource

    ' Export the data to rtf format
    DoCmd.OutputTo acOutputQuery, conQuery034, _
    acFormatRTF, strDataSource, False

    ' Start Word using mailmerge template
    Set wrdApp = New Word.Applicatio n
    Set doc = wrdApp.Document s.Add(strPath & conTemplate034)

    ' Do the mail merge to a new document.
    With doc.MailMerge
    .OpenDataSource Name:=strDataSo urce
    .Destination = wdSendToNewDocu ment
    .SuppressBlankL ines = True
    With .DataSource
    .FirstRecord = wdDefaultFirstR ecord
    .LastRecord = wdDefaultLastRe cord
    End With
    If .State = wdMainAndDataSo urce Then .Execute
    End With

    ' Display the mail merge document
    wrdApp.Visible = True

    ExitHere:
    Set doc = Nothing
    Set wrdApp = Nothing
    Exit Sub

    HandleErrors:
    Select Case Err.Number
    Case 5922
    MsgBox "The Data Requested Does Not Exist in Database"
    Resume ExitHere
    Case 2501
    Resume ExitHere
    Case 70
    MsgBox "Close Previously Generated Documents"
    Resume ExitHere
    Case 53 ' File not found
    Resume Next
    Case Else
    MsgBox Err.Number & ": " & Err.Description
    Resume ExitHere
    End Select
    Resume
    End Sub[/code]
    Last edited by Stewart Ross; Mar 20 '08, 10:08 PM. Reason: code tags added
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. I have added code tags around your code to make it easier to read. Could you advise on which of the lines listed your object not set error occurs? It would assist greatly with reviewing what might be going wrong.

    Thanks

    Stewart

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      I'm not 100% sure on this one, but you may have to provide a fully qualified Object Reference in your inner With block, since you are referencing a member of an Object in an outer With block (eliminate the Inner With...End With). Try a simple code substitution, if I'm wrong then there is no harm done. Replace Lines 28 to 38 with:
      [CODE=vb]
      ' Do the mail merge to a new document.
      With doc.MailMerge
      .OpenDataSource Name:=strDataSo urce
      .Destination = wdSendToNewDocu ment
      .SuppressBlankL ines = True
      .DataSource.Fir stRecord = wdDefaultFirstR ecord
      .DataSource.Las tRecord = wdDefaultLastRe cord
      If .State = wdMainAndDataSo urce Then .Execute
      End With[/CODE]

      Comment

      Working...