Activate a bookmark in a document

N

Nick Ward

Hi I'm trying to insert some text from an access database into an enclosed
bookmark ( got lots already that insert in placeholders)

Set m_objWord = New Word.Application
Set m_objDoc = m_objWord.Documents.Add(m_strDIR & Choose_template)

'lots of stuff going on here

m_objDoc.SaveAs FileName:=M_strDIRSave &
Forms!frm_quote.CompanyName & _
" - " & FormatDateTime(Date, vbLongDate) & Minute(Now()) &
Second(Now()) & ".doc"

'this next bit was from mvsp.org as it is to get over the deleted enclosed
bookmark problem

Dim bmrange As Range
Set bmrange = m_objDoc.Bookmarks("quoteid").Range ' ref to an enclosed
bookmark
bmrange.Text = Forms!frm_quote.quoteID
ActiveDocument.Bookmarks.Add "quoteid", bmrange

all I get is a typmsmatch error when it tries to set bmrange so I'm
obviously not referencing the document correctly as bmrange=nothing

Thanks in advance
 
J

Jonathan West

Hi Nick,

if I understand you right, you're getting an type mismatch error on this
line

Set bmrange = m_objDoc.Bookmarks("quoteid").Range ' ref to an enclosed

If so, then I suggest that check whether you have the correct name for the
bookmark, and whether the bookmark exists at the moment.
 
N

Nick Ward

Hi Jonathan
Yes that bookmark exists, i have tried it with other bookmarks in the
template, same error, have even tried the index number of the bookmark, that
still produces the same error..
 
D

Doug Robbins - Word MVP

Like Jonathon, I can see nothing wrong with your code.

If the bookmark exists in the template returned by m_strDIR &
Choose_template, at the time that the command

Set bmrange = m_objDoc.Bookmarks("quoteid").Range

is issued, then the code should work.

Are you sure that some of the other commands for the "got lots already that
insert in placeholders" are not causing the bookmark "quoteid" to be
deleted?
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
N

Nick Ward

Hi
I stripped absolutely everything out of the function just to test it and its
still not working, vie tried with a template with only 1 bookmark in it as
well ( in answer to your question whether the other function is messing
things up). I've also double checked both the template and the resulting
document and the bookmark is definitely there and I've also tried changing
the bookmark name and changing it to a placeholder bookmark.

I need to know the quoteid number because I'm trying to create a function
that will track the changes made to the document and record it in my
database, is there another way of capturing the information without a
bookmark, say in a table or textbox?

This is the function, the uses chooses a template from an access form

If Not Choose_template = "" Then
Dim m_strDIR, M_strDIRSave As String
m_strDIR = Application.CurrentProject.Path &
"\templates\quotes\"
M_strDIRSave = Application.CurrentProject.Path & "\quotes\"
Set m_objWord = New Word.Application
Set m_objDoc = m_objWord.Documents.Add(m_strDIR & Choose_template)

' insert text at bookmark functions goes here

m_objDoc.SaveAs FileName:=M_strDIRSave &
Forms!frm_quote.CompanyName & _
" - " & FormatDateTime(Date, vbLongDate) & Minute(Now()) &
Second(Now()) & ".doc"

Dim bmrange As Range
Set bmrange = m_objDoc.Bookmarks("quoteid").Range
bmrange.Text = Forms!frm_quote.quoteID
ActiveDocument.Bookmarks.Add "quoteid", bmrange


DoCmd.Close acForm, "frm_template_chooser", acSaveYes
Dim db As Database
Dim rec As Recordset
Dim senttype As String
Dim strtid As String
strtid = Forms!frm_quote.quoteID

Set db = CurrentDb()
Set rec = db.OpenRecordset("tbl_contactNotes")
senttype = "Quote generated"

Addnewone2 rec, senttype, strtid

rec.Close
Set rec = Nothing
m_objDoc.Close
m_objWord.Quit
Set m_objDoc = Nothing
Set m_objWord = Nothing

requerycontacts
Else
End If


TIA
 
D

Doug Robbins - Word MVP

Hi Nick,

You could store the quote in a document variable and display it in the
document by means of a DOCVARIABLE field

In the template where you want the quoteID to appear, insert a DOCVARIABLE
field

{ DOCVARIABLE "vquoteID" }

Then in the code use:

m_objDoc.Variables("vquoteID").Value = Forms!frm_quote.quoteID
m_objDoc.FIelds.Update

to create the variable "vquoteID" containing the quoteID from the form and
update the field to display the data'

You can retrieve the quoteId from the document variable by using

MsgBox m_objDoc.Variables("vquoteID").Value


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
N

Nick Ward

Thanks Doug, it worked like a charm....
Doug Robbins - Word MVP said:
Hi Nick,

You could store the quote in a document variable and display it in the
document by means of a DOCVARIABLE field

In the template where you want the quoteID to appear, insert a DOCVARIABLE
field

{ DOCVARIABLE "vquoteID" }

Then in the code use:

m_objDoc.Variables("vquoteID").Value = Forms!frm_quote.quoteID
m_objDoc.FIelds.Update

to create the variable "vquoteID" containing the quoteID from the form and
update the field to display the data'

You can retrieve the quoteId from the document variable by using

MsgBox m_objDoc.Variables("vquoteID").Value


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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