Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
 
Posts: n/a
Default How to get all mail merge fields in a document

I am using COM automation and WordBasic to drive Word from another
application.
I have Word templates defined with mail merge fields in them.
I am trying to open one of my Word templates and ask it for all the
mail merge fields in it.
I am doing this because I want to build the data source file on the
fly.
I want to create a single record in the data source and have in it only
the fields required for the current template.
My problem is that I cant find a function which will return me all the
mail merge field names in a document.
I previously tried using bookmarks instead of mail merge fields and
this worked fine. I could ask the document for all its bookmarks and
then get the data transferred across from my app to Word for just the
bookmarks defined in the document.
Does anyone know of a function which will give me the names of all mail
merge fields in a document ?
The function MergeFieldName$(1) will get me the first mail merge field
name in a data source file but not in a document file.
Thanks

  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP
 
Posts: n/a
Default How to get all mail merge fields in a document

Macro to extract all of the email addresses from a document

Sub CopyAddressesToOtherDoc()


Dim Source As Document, Target As Document, myRange As Range
Set Source = ActiveDocument
Set Target = Documents.Add

Application.ScreenUpdating = False

Source.Activate
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[+0-9A-z._-]{1,}\@[A-z.]{1,}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With

Selection.HomeKey Unit:=wdStory
Target.Activate

End Sub

--
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

wrote in message
oups.com...
I am using COM automation and WordBasic to drive Word from another
application.
I have Word templates defined with mail merge fields in them.
I am trying to open one of my Word templates and ask it for all the
mail merge fields in it.
I am doing this because I want to build the data source file on the
fly.
I want to create a single record in the data source and have in it only
the fields required for the current template.
My problem is that I cant find a function which will return me all the
mail merge field names in a document.
I previously tried using bookmarks instead of mail merge fields and
this worked fine. I could ask the document for all its bookmarks and
then get the data transferred across from my app to Word for just the
bookmarks defined in the document.
Does anyone know of a function which will give me the names of all mail
merge fields in a document ?
The function MergeFieldName$(1) will get me the first mail merge field
name in a data source file but not in a document file.
Thanks



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor
 
Posts: n/a
Default How to get all mail merge fields in a document

Doug is on the right lines here, but the search routine may not give the
results required. Try the following, which extracts all the mergefields from
the document and writes them to another document, then deletes any
duplicates: You end up with a list of all the mergefields in the document.

Sub CopyMergefieldsToOtherDoc()
Dim Source As Document, Target As Document, myRange As Range, sView As
String
Set Source = ActiveDocument
sView = ActiveWindow.View.ShowFieldCodes
Set Target = Documents.Add
Application.ScreenUpdating = False
Source.Activate
ActiveWindow.View.ShowFieldCodes = True
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="^d Merge", _
MatchWildcards:=False, Wrap:=wdFindStop, _
Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = sView
Target.Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Mergefield"
.Replacement.Text = "MERGEFIELD"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Sort FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(*^13)@"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


Doug Robbins - Word MVP wrote:
Macro to extract all of the email addresses from a document

Sub CopyAddressesToOtherDoc()


Dim Source As Document, Target As Document, myRange As Range
Set Source = ActiveDocument
Set Target = Documents.Add

Application.ScreenUpdating = False

Source.Activate
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[+0-9A-z._-]{1,}\@[A-z.]{1,}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With

Selection.HomeKey Unit:=wdStory
Target.Activate

End Sub


wrote in message
oups.com...
I am using COM automation and WordBasic to drive Word from another
application.
I have Word templates defined with mail merge fields in them.
I am trying to open one of my Word templates and ask it for all the
mail merge fields in it.
I am doing this because I want to build the data source file on the
fly.
I want to create a single record in the data source and have in it
only the fields required for the current template.
My problem is that I cant find a function which will return me all
the mail merge field names in a document.
I previously tried using bookmarks instead of mail merge fields and
this worked fine. I could ask the document for all its bookmarks and
then get the data transferred across from my app to Word for just
the bookmarks defined in the document.
Does anyone know of a function which will give me the names of all
mail merge fields in a document ?
The function MergeFieldName$(1) will get me the first mail merge
field name in a data source file but not in a document file.
Thanks



  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
mm
 
Posts: n/a
Default How to get all mail merge fields in a document

Thanks Doug and Graham.
However my point of reference is that I am in my own application, outside of
Word, using COM automation and 'WordBasic' functions to communicate with
Word.
Therefore I don't think I can run the code below from my application to get
the merge field names.
When I use bookmarks instead of merge fields I can invoke the procedure
BookmarkName$(Count) to get the names of bookmarks. I can then 'goto' the
bookmark and then insert text, all through the WordBasic interface.

So my requirement is - to open a word template, ask it for its fields, and
then populate just those fields.
I was hoping I could somehow use mail merge and build the merge data file
with 1 row in it on the fly. I was hoping I could build the merge data file
based on which fields the document needs. However if I can't get the names
of just the fields used in a specific template then I will have to revert
back to using bookmarks.

How about this idea. A merge field can also have a bookmark. If I gave the
merge fields a bookmark name with the same name then I would be all set. I
could ask for the bookmark names and then build a merge data file and
perform a mail merge. What do you think ? Any other ideas ?

Thanks.

Michael.



"Graham Mayor" wrote in message
...
Doug is on the right lines here, but the search routine may not give the
results required. Try the following, which extracts all the mergefields
from the document and writes them to another document, then deletes any
duplicates: You end up with a list of all the mergefields in the document.

Sub CopyMergefieldsToOtherDoc()
Dim Source As Document, Target As Document, myRange As Range, sView As
String
Set Source = ActiveDocument
sView = ActiveWindow.View.ShowFieldCodes
Set Target = Documents.Add
Application.ScreenUpdating = False
Source.Activate
ActiveWindow.View.ShowFieldCodes = True
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="^d Merge", _
MatchWildcards:=False, Wrap:=wdFindStop, _
Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowFieldCodes = sView
Target.Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Mergefield"
.Replacement.Text = "MERGEFIELD"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Sort FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(*^13)@"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute replace:=wdReplaceAll
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub


--

Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org


Doug Robbins - Word MVP wrote:
Macro to extract all of the email addresses from a document

Sub CopyAddressesToOtherDoc()


Dim Source As Document, Target As Document, myRange As Range
Set Source = ActiveDocument
Set Target = Documents.Add

Application.ScreenUpdating = False

Source.Activate
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="[+0-9A-z._-]{1,}\@[A-z.]{1,}", _
MatchWildcards:=True, Wrap:=wdFindStop, Forward:=True) = True
Set myRange = Selection.Range
Target.Range.InsertAfter myRange & vbCr
Loop
End With

Selection.HomeKey Unit:=wdStory
Target.Activate

End Sub


wrote in message
oups.com...
I am using COM automation and WordBasic to drive Word from another
application.
I have Word templates defined with mail merge fields in them.
I am trying to open one of my Word templates and ask it for all the
mail merge fields in it.
I am doing this because I want to build the data source file on the
fly.
I want to create a single record in the data source and have in it
only the fields required for the current template.
My problem is that I cant find a function which will return me all
the mail merge field names in a document.
I previously tried using bookmarks instead of mail merge fields and
this worked fine. I could ask the document for all its bookmarks and
then get the data transferred across from my app to Word for just
the bookmarks defined in the document.
Does anyone know of a function which will give me the names of all
mail merge fields in a document ?
The function MergeFieldName$(1) will get me the first mail merge
field name in a data source file but not in a document file.
Thanks





Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mail merge and text form fields Miss Su Mailmerge 1 February 4th 06 10:29 PM
page number printing on a merged report Print Page # On Merged Report Mailmerge 6 October 17th 05 05:18 AM
How do I make footnote numners static in a mail merge document? Heather Mailmerge 5 October 14th 05 04:59 AM
mail merge with attachments AS Mailmerge 5 April 9th 05 09:49 AM
I can't get a mail merge document to open, I get "the document na. hask104 Mailmerge 0 March 31st 05 01:57 AM


All times are GMT +1. The time now is 03:43 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"