Bookmarks from access

S

Steve Cee

I'm trying to get an example from a book (Wrox - Accesss 2002 VBA)
running. It's supposed to insert text into a word document at pre-set
bookmarks. However it keeps crashing at the line:

m_objDoc.Bookmark(strBkmk).Select

In the InsertTextAtBookMark prodecure.

The error message is:

Run-time error '91';
Object variable or With Block variable not set




I've included the full code below.

Any help greatly appreciated.
-------------------------------------------------------------------





Option Compare Database
Option Explicit

Private Const m_strDIR As String = "C:\BegVBA\"
Private Const m_strTEMPLATE As String = "Order.dot"

Private m_objWord As Word.Application
Private m_objDoc As Word.Document

Public Sub CreateOrderLetter(recSupp As Recordset, recItems As
Recordset)

' instantiate the word application and create a new
' document based upon the supplied template
Set m_objWord = New Word.Application
Set m_objDoc = m_objWord.Documents.Add(m_strDIR & m_strTEMPLATE)

' insert the customer details
InsertTextAtBookMark "ContactName", recSupp("ContactName")
InsertTextAtBookMark "CompanyName", recSupp("CompanyName")
InsertTextAtBookMark "Address", recSupp("Address")
InsertTextAtBookMark "City", recSupp("City")
InsertTextAtBookMark "State", recSupp("State")
InsertTextAtBookMark "ZipCode", recSupp("ZipCode")
InsertTextAtBookMark "Country", recSupp("Country")

' now the order items
InsertItemsTable recItems

' print the order, not using background printing
' otherwise code continues and we try to quit whilst still
printing
m_objWord.PrintOut Background:=False

' now save and quit
m_objDoc.SaveAs FileName:=m_strDIR & recSupp("CompanyName") & _
" - " & FormatDateTime(Date, vbLongDate) & ".DOC"
m_objDoc.Close
m_objWord.Quit

' clean up
Set m_objDoc = Nothing
Set m_objWord = Nothing

End Sub

Private Sub InsertTextAtBookMark(strBkmk As String, varText As
Variant)

' selects the bookmark and inserts the text
m_objDoc.Bookmark(strBkmk).Select
m_objWord.Selection.Text = varText & ""

End Sub

Private Sub InsertItemsTable(recR As Recordset)

Dim strTable As String
Dim objTable As Word.Table

' create columns separated by tabs
' it's easier to convert this to a table than to create
' a table and worry about the table cells
strTable = "Item" & vbTab & "Quantity" & vbCr
recR.MoveFirst
While Not recR.EOF
strTable = strTable & recR("Name") & vbTab & _
recR("ReOrderPoint") & vbCr
recR.MoveNext
Wend

' now insert the text, convert it to a table, and format it
InsertTextAtBookMark "Items", strTable
Set objTable =
m_objWord.Selection.ConvertToTable(Separator:=vbTab)
With objTable
.AutoFormat Format:=wdTableFormatClassic3, AutoFit:=True, _
ApplyShading:=False
End With

' clean up
Set objTable = Nothing

End Sub
 
S

Steve Cee

Nevermind. I found the issue.

The code was looking for "ZipCode" but the word docuemnt had "local
code". Doh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top