Bookmark and Email

L

Lin

Hi,

I have a document which I merged with Access database using bookmarks in the
Document. It works fine. I would like to run the macro to get the email field
(which is a bookmark in Word) and email the document as an attachment. I used
the following code to get the email bookmark
Activedocument.bookmarks("email").range.txt. some how word not recognising
this field as bookmark and keep giving me error as " unrecognised collection
of objects"
why that happening. I would like to email the document only after the merge.
Once it merge there is no bookmark. I am fairly new to the bookmark concept.
What shoul I do in order to get the email field. Also is there any other way
I can do this.

Thanks for your help
 
M

muybn

Try ".text" instead of ".txt". If that doesn't work, let me know because I
have done something very similar that works; but the way I did it involves a
lot of code, so first try this suggestion and we'll go from there.
 
M

muybn

Actually, if you don't get Intellisense with the dot after the main property
(as in "Range."), the command after will probably be invalid. (Don't know if
you intentionally wrote ".txt" or if you were just in a hurry.)
 
L

Lin

I did use 'text' , not txt. Could you please tell me how did you make this
work?

Thanks
 
D

Doug Robbins - Word MVP

Are you sure that the bookmark still exists in the document after you
populate it with the data from Access?

See the article "Working with Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm

and "Inserting Text at a Bookmark without deleting the Bookmark" at:

http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

muybn

Good articles, Doug, especially about InsertAfter and InsertBefore since I've
never used those features.

I was about to write the same thing that Doug wrote about the bookmarks
being deleted in the merge-to document. Otherwise, your syntax to pick up the
bookmark text is correct (with the notation of .text that you affirmed you
are using).

Following is a macro I use for the issue of bookmarks disappearing, but as I
warned earlier, it involves a lot of code and is not really a "merge"
operation, per se. Things to take into account: (1) Don't use the brackets []
literally. (2) This macro assumes that only one DB record is being used at a
time in your document; in other words, that you're not making a list with the
same mergefield being used with multiple records in the same document. (3) I
list some utility macros ("CloseDoc," etc.) after the main one and they are
referenced/used in the main macro. I use these utilities a lot so I just
abbreviate the multiple commands into one macro name.


Sub [YourMacroName]()
Dim strDoc As String
Dim strPath As String
'use variable to pick up doc name
'if it's always the same doc name, use strDoc = "[yourDocName].doc"
'otherwise, if always a different doc name, replace line below with
'strDoc = ActiveDocument.Name
strDoc = "[yourDocName].doc"
strPath = Documents(strDoc).path
'make double sure that desired document is open and active
On Error Resume Next
Documents(strDoc).Activate
On Error GoTo 0
If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc
'macro ends if the correct document doesn't open
If ActiveDocument.name <> strDoc Then End
'select text of whole document
With Selection
.WholeStory
.Copy
End With
'open new document and paste into it
NewDoc
With Selection
.Paste
'delete any extra paragraph mark at end
.Delete
'uncomment and insert correct doc name in next command if you want
to save the doc
'ActiveDocument.SaveAs "[newDocName].doc",
FileFormat:=wdFormatDocument
.WholeStory
'unlinks fields which original merge doc created
.Fields.Unlink
End With
DocHome
'do your bookmark operations here

'uncomment the following if you want to go back to your original doc
after all is said and done
'On Error Resume Next
'Documents(strDoc).Activate
'On Error GoTo 0
'If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc

End Sub

Sub DocHome()
Selection.HomeKey unit:=wdStory
End Sub

Sub NewDoc()
Documents.Add Template:="Normal", NewTemplate:=False
End Sub
 
D

Doug Robbins - Word MVP

I would use DOCVARIABLE fields instead of bookmarks and have code in Access
create VARIABLES in the Word document. You can then always use code to
access the .Value of the variable.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

muybn said:
Good articles, Doug, especially about InsertAfter and InsertBefore since
I've
never used those features.

I was about to write the same thing that Doug wrote about the bookmarks
being deleted in the merge-to document. Otherwise, your syntax to pick up
the
bookmark text is correct (with the notation of .text that you affirmed you
are using).

Following is a macro I use for the issue of bookmarks disappearing, but as
I
warned earlier, it involves a lot of code and is not really a "merge"
operation, per se. Things to take into account: (1) Don't use the brackets
[]
literally. (2) This macro assumes that only one DB record is being used at
a
time in your document; in other words, that you're not making a list with
the
same mergefield being used with multiple records in the same document. (3)
I
list some utility macros ("CloseDoc," etc.) after the main one and they
are
referenced/used in the main macro. I use these utilities a lot so I just
abbreviate the multiple commands into one macro name.


Sub [YourMacroName]()
Dim strDoc As String
Dim strPath As String
'use variable to pick up doc name
'if it's always the same doc name, use strDoc = "[yourDocName].doc"
'otherwise, if always a different doc name, replace line below with
'strDoc = ActiveDocument.Name
strDoc = "[yourDocName].doc"
strPath = Documents(strDoc).path
'make double sure that desired document is open and active
On Error Resume Next
Documents(strDoc).Activate
On Error GoTo 0
If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc
'macro ends if the correct document doesn't open
If ActiveDocument.name <> strDoc Then End
'select text of whole document
With Selection
.WholeStory
.Copy
End With
'open new document and paste into it
NewDoc
With Selection
.Paste
'delete any extra paragraph mark at end
.Delete
'uncomment and insert correct doc name in next command if you want
to save the doc
'ActiveDocument.SaveAs "[newDocName].doc",
FileFormat:=wdFormatDocument
.WholeStory
'unlinks fields which original merge doc created
.Fields.Unlink
End With
DocHome
'do your bookmark operations here

'uncomment the following if you want to go back to your original doc
after all is said and done
'On Error Resume Next
'Documents(strDoc).Activate
'On Error GoTo 0
'If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc

End Sub

Sub DocHome()
Selection.HomeKey unit:=wdStory
End Sub

Sub NewDoc()
Documents.Add Template:="Normal", NewTemplate:=False
End Sub

--
Bryan


Doug Robbins - Word MVP said:
Are you sure that the bookmark still exists in the document after you
populate it with the data from Access?

See the article "Working with Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm

and "Inserting Text at a Bookmark without deleting the Bookmark" at:

http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

muybn

Note #3 reads '(3) I list some utility macros ("CloseDoc," etc.)' but would
be better written as '(3) I list some utility macros ("DocHome," etc.)' since
I didn't include my "CloseDoc" utility macro.

(In the www.ASP.net forums, you can edit your own posts. Seems like there
should be a way provided to do that here.)

--
Bryan


muybn said:
Good articles, Doug, especially about InsertAfter and InsertBefore since I've
never used those features.

I was about to write the same thing that Doug wrote about the bookmarks
being deleted in the merge-to document. Otherwise, your syntax to pick up the
bookmark text is correct (with the notation of .text that you affirmed you
are using).

Following is a macro I use for the issue of bookmarks disappearing, but as I
warned earlier, it involves a lot of code and is not really a "merge"
operation, per se. Things to take into account: (1) Don't use the brackets []
literally. (2) This macro assumes that only one DB record is being used at a
time in your document; in other words, that you're not making a list with the
same mergefield being used with multiple records in the same document. (3) I
list some utility macros ("CloseDoc," etc.) after the main one and they are
referenced/used in the main macro. I use these utilities a lot so I just
abbreviate the multiple commands into one macro name.


Sub [YourMacroName]()
Dim strDoc As String
Dim strPath As String
'use variable to pick up doc name
'if it's always the same doc name, use strDoc = "[yourDocName].doc"
'otherwise, if always a different doc name, replace line below with
'strDoc = ActiveDocument.Name
strDoc = "[yourDocName].doc"
strPath = Documents(strDoc).path
'make double sure that desired document is open and active
On Error Resume Next
Documents(strDoc).Activate
On Error GoTo 0
If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc
'macro ends if the correct document doesn't open
If ActiveDocument.name <> strDoc Then End
'select text of whole document
With Selection
.WholeStory
.Copy
End With
'open new document and paste into it
NewDoc
With Selection
.Paste
'delete any extra paragraph mark at end
.Delete
'uncomment and insert correct doc name in next command if you want
to save the doc
'ActiveDocument.SaveAs "[newDocName].doc",
FileFormat:=wdFormatDocument
.WholeStory
'unlinks fields which original merge doc created
.Fields.Unlink
End With
DocHome
'do your bookmark operations here

'uncomment the following if you want to go back to your original doc
after all is said and done
'On Error Resume Next
'Documents(strDoc).Activate
'On Error GoTo 0
'If ActiveDocument.name <> strDoc Then Documents.Open strPath & strDoc

End Sub

Sub DocHome()
Selection.HomeKey unit:=wdStory
End Sub

Sub NewDoc()
Documents.Add Template:="Normal", NewTemplate:=False
End Sub

--
Bryan


Doug Robbins - Word MVP said:
Are you sure that the bookmark still exists in the document after you
populate it with the data from Access?

See the article "Working with Bookmarks in VBA" at:

http://www.word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm

and "Inserting Text at a Bookmark without deleting the Bookmark" at:

http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

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