A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Mailmerge
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Populate Merge Fields via VB.NET



 
 
Thread Tools Display Modes
  #1  
Old June 20th, 2005, 07:37 PM
ben
external usenet poster
 
Posts: n/a
Default Populate Merge Fields via VB.NET

I have several documents that have merge fields identified. I am not able to
populate the forms using mail merge, I need to populate programatically via
VB.NET. I noticed the Word.Document.Fields.count matches the number of merge
fields in the document while Word.Document.FormFields.count = 0. Can someone
point me in the correct direction on populating a merge field programatically
  #2  
Old June 20th, 2005, 10:25 PM
Peter Jamieson
external usenet poster
 
Posts: n/a
Default

"Fields" include Merge fields and Form fields. Form fields and Merge fields
are typically used for different purposes. Generaly speaking, it is probably
a good idea to populate Merge fields by setting up and executing a mail
merge (and letting Word fill in the values of each field).

If the documents that have been given to you were designed to be used in a
particular way, it may not be a straightforward process to adapt them so
that they can be used with mailmerge, or any other form of document
automation. It may help if you can provide some more details about what you
are trying to do,

Peter Jamieson

"ben" wrote in message
...
I have several documents that have merge fields identified. I am not able
to
populate the forms using mail merge, I need to populate programatically
via
VB.NET. I noticed the Word.Document.Fields.count matches the number of
merge
fields in the document while Word.Document.FormFields.count = 0. Can
someone
point me in the correct direction on populating a merge field
programatically



  #3  
Old June 20th, 2005, 10:39 PM
ben
external usenet poster
 
Posts: n/a
Default

I have a word document with a header, body, and footer sections. Merge
fields are located in all sections. I am receiving the data needed to
populate these documents via a web service. The web servcie will populate
the document, then send the document to the printer. Using a datasource is
the last option I have. The preferred option is to have the webservice
populate the document via vb.net code and send to a printer. i am also
struggling on how to get a reference to the header and footer objects.

"Peter Jamieson" wrote:

"Fields" include Merge fields and Form fields. Form fields and Merge fields
are typically used for different purposes. Generaly speaking, it is probably
a good idea to populate Merge fields by setting up and executing a mail
merge (and letting Word fill in the values of each field).

If the documents that have been given to you were designed to be used in a
particular way, it may not be a straightforward process to adapt them so
that they can be used with mailmerge, or any other form of document
automation. It may help if you can provide some more details about what you
are trying to do,

Peter Jamieson

"ben" wrote in message
...
I have several documents that have merge fields identified. I am not able
to
populate the forms using mail merge, I need to populate programatically
via
VB.NET. I noticed the Word.Document.Fields.count matches the number of
merge
fields in the document while Word.Document.FormFields.count = 0. Can
someone
point me in the correct direction on populating a merge field
programatically




  #4  
Old June 21st, 2005, 06:30 AM
Peter Jamieson
external usenet poster
 
Posts: n/a
Default

OK, unless you are actually using an OpenDataSource to open a data source,
you can't actually use Mailmerge to populate those merge fields for you.
Since you're getting your data from a web service, you probably are not able
to use OpenDataSource because that requires the data to come from a file, or
a type of data source recognised by Word such as OLEDB or ODBC. If you are
able to create a file with a local or Microsoft network address (i.e. one
that you can only open using an Internet URL won't work because
OpenDataSource will not open it) then that is probably the simplest way to
populate your merge fields.

If you cannot do that (e.g. because you are not allowed to create local
files) then the only approach is to replace whatever placeholders exist in
the document with your own data, i.e. simulate a merge. That's what I'm
assuming you are trying to do, and if so:
a. be aware that Mailmerge actually does quite a lot of stuff that it is
non-trivial to write yourself. For example, if for some reason there are
nested fields in your document, you may have to ensure that your code
replaces the placeholders in a specific sequence. Inserting data in headers
and footers can also be tricky if you need to create multi-section documents
as output.
b. you may find it useful to use field types other than MERGEFIELD fields
as placeholders. In particular, you can consider using { DOCVARIABLE } or
{ DOCPROPERTY } fields in the main document - the advantage is that you can
populate the Document variables and/or Document properties in code, then
select the document and re-execute all the fields, and Word will do the
rest. You don't need to find the fields in the document etc. etc. The main
drawback is that if you try to put an empty string in a Document variable,
Word will delete the variable and the { DOCPROPERTY } field will display an
error message when evaluated.
c. if you use that approach, you may need to loop through all the "Stories"
in the document to update the fields, e.g. using the VB.NET equivalent of
the following VBA code:

'---------------
Sub RefreshAllStories()
Dim r As Range
For Each r In ActiveDocument.StoryRanges
r.Fields.Update
Next
'---------------


Otherwise, to go back to original question, to loop round all the fields in
a document, you need something more like the following, which should find
most of them but may have trouble detecting some fields in the graphics
layer:

'---------------
Sub IterateMergeFields()
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
For Each s In ActiveDocument.Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
End Sub

'---------------

To get references to Headers and Footers, use the StoryRanges collection to
access specific types of header and footer, e.g.

ActiveDocument.StoryRanges(wdPrimaryHeaderStory)

You may need to trap errors in the case where the specified Story does not
exist.

Peter Jamieson

"ben" wrote in message
...
I have a word document with a header, body, and footer sections. Merge
fields are located in all sections. I am receiving the data needed to
populate these documents via a web service. The web servcie will populate
the document, then send the document to the printer. Using a datasource
is
the last option I have. The preferred option is to have the webservice
populate the document via vb.net code and send to a printer. i am also
struggling on how to get a reference to the header and footer objects.

"Peter Jamieson" wrote:

"Fields" include Merge fields and Form fields. Form fields and Merge
fields
are typically used for different purposes. Generaly speaking, it is
probably
a good idea to populate Merge fields by setting up and executing a mail
merge (and letting Word fill in the values of each field).

If the documents that have been given to you were designed to be used in
a
particular way, it may not be a straightforward process to adapt them so
that they can be used with mailmerge, or any other form of document
automation. It may help if you can provide some more details about what
you
are trying to do,

Peter Jamieson

"ben" wrote in message
...
I have several documents that have merge fields identified. I am not
able
to
populate the forms using mail merge, I need to populate programatically
via
VB.NET. I noticed the Word.Document.Fields.count matches the number of
merge
fields in the document while Word.Document.FormFields.count = 0. Can
someone
point me in the correct direction on populating a merge field
programatically






  #5  
Old June 21st, 2005, 02:09 PM
ben
external usenet poster
 
Posts: n/a
Default

Peter - Thanks for the help. That was very usefull info you provided. I am
able to access all the merge fields, but would like to verify that I am
correctly setting the merge field text correctly. Is the below code correct,
or is there a better more efficient way?
---------------------------
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
f.Select()
Dim oSel as Selection
oSel = ActiveDocument.Selection
oSel.TypeText("text to enter for merge field")
End If
Next
End If
Next
---------------------------

Also, I have one more issue. That is in the header, there is a text box
field that I can not seem to figure out how to get access. When I use your
example for accessing the header / footer, the text box never is accessable.
Could you please shed some light on that for me also?

Thanks. Your input is very helpful


"Peter Jamieson" wrote:

OK, unless you are actually using an OpenDataSource to open a data source,
you can't actually use Mailmerge to populate those merge fields for you.
Since you're getting your data from a web service, you probably are not able
to use OpenDataSource because that requires the data to come from a file, or
a type of data source recognised by Word such as OLEDB or ODBC. If you are
able to create a file with a local or Microsoft network address (i.e. one
that you can only open using an Internet URL won't work because
OpenDataSource will not open it) then that is probably the simplest way to
populate your merge fields.

If you cannot do that (e.g. because you are not allowed to create local
files) then the only approach is to replace whatever placeholders exist in
the document with your own data, i.e. simulate a merge. That's what I'm
assuming you are trying to do, and if so:
a. be aware that Mailmerge actually does quite a lot of stuff that it is
non-trivial to write yourself. For example, if for some reason there are
nested fields in your document, you may have to ensure that your code
replaces the placeholders in a specific sequence. Inserting data in headers
and footers can also be tricky if you need to create multi-section documents
as output.
b. you may find it useful to use field types other than MERGEFIELD fields
as placeholders. In particular, you can consider using { DOCVARIABLE } or
{ DOCPROPERTY } fields in the main document - the advantage is that you can
populate the Document variables and/or Document properties in code, then
select the document and re-execute all the fields, and Word will do the
rest. You don't need to find the fields in the document etc. etc. The main
drawback is that if you try to put an empty string in a Document variable,
Word will delete the variable and the { DOCPROPERTY } field will display an
error message when evaluated.
c. if you use that approach, you may need to loop through all the "Stories"
in the document to update the fields, e.g. using the VB.NET equivalent of
the following VBA code:

'---------------
Sub RefreshAllStories()
Dim r As Range
For Each r In ActiveDocument.StoryRanges
r.Fields.Update
Next
'---------------


Otherwise, to go back to original question, to loop round all the fields in
a document, you need something more like the following, which should find
most of them but may have trouble detecting some fields in the graphics
layer:

'---------------
Sub IterateMergeFields()
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
For Each s In ActiveDocument.Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
End Sub

'---------------

To get references to Headers and Footers, use the StoryRanges collection to
access specific types of header and footer, e.g.

ActiveDocument.StoryRanges(wdPrimaryHeaderStory)

You may need to trap errors in the case where the specified Story does not
exist.

Peter Jamieson

"ben" wrote in message
...
I have a word document with a header, body, and footer sections. Merge
fields are located in all sections. I am receiving the data needed to
populate these documents via a web service. The web servcie will populate
the document, then send the document to the printer. Using a datasource
is
the last option I have. The preferred option is to have the webservice
populate the document via vb.net code and send to a printer. i am also
struggling on how to get a reference to the header and footer objects.

"Peter Jamieson" wrote:

"Fields" include Merge fields and Form fields. Form fields and Merge
fields
are typically used for different purposes. Generaly speaking, it is
probably
a good idea to populate Merge fields by setting up and executing a mail
merge (and letting Word fill in the values of each field).

If the documents that have been given to you were designed to be used in
a
particular way, it may not be a straightforward process to adapt them so
that they can be used with mailmerge, or any other form of document
automation. It may help if you can provide some more details about what
you
are trying to do,

Peter Jamieson

"ben" wrote in message
...
I have several documents that have merge fields identified. I am not
able
to
populate the forms using mail merge, I need to populate programatically
via
VB.NET. I noticed the Word.Document.Fields.count matches the number of
merge
fields in the document while Word.Document.FormFields.count = 0. Can
someone
point me in the correct direction on populating a merge field
programatically






  #6  
Old June 21st, 2005, 05:43 PM
Peter Jamieson
external usenet poster
 
Posts: n/a
Default

Is the below code correct,
or is there a better more efficient way?


tend to take the "if it works, it works" view of software these days - in
other words, I can't tell you if your code is "correct". In this case I
would be inclined to test a few special cases, e.g. what if the field is
right at the beginning or end of a range, what if the text is an empty
string, perhaps check that leading/trailing spaces are retained, and if you
are dealing with "international" data, see what happens to unusual accented
characters.

Also, I have one more issue. That is in the header, there is a text box
field that I can not seem to figure out how to get access. When I use
your
example for accessing the header / footer, the text box never is
accessable.
Could you please shed some light on that for me also?


I think you have encountered the "may have trouble detecting some fields in
the graphics layer" problem I mentioned. I haven't checked this in detail
but I think if you access the appropriate header object (and I didn't realy
give you the complete story about that before) you will be able to get these
too, e.g. to inspect one such object

For Each s In
ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
End Sub

If you need to deal with multiple sections or multiple types of
header/footer, you'll need to wrap that in another suitable loop. My memory
of this is a bit dim, but I think Word actually finds some fields in more
than one of the collections being processed in these code snippets - in your
case that's probably not going to do any harm except maybe to performance.
If you do mange to work out a more precise algorithm that always works,
please do post it back here.

Peter Jamieson
and I don't know how to solve it. Is it possible in this case to convert
the text box to a frame that will do the same job (select the box and use
Fomat Text Box|Text Box|Convert to Frame).

Peter Jamieson

"ben" wrote in message
...
Peter - Thanks for the help. That was very usefull info you provided. I
am
able to access all the merge fields, but would like to verify that I am
correctly setting the merge field text correctly. Is the below code
correct,
or is there a better more efficient way?
---------------------------
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
f.Select()
Dim oSel as Selection
oSel = ActiveDocument.Selection
oSel.TypeText("text to enter for merge field")
End If
Next
End If
Next
---------------------------

Also, I have one more issue. That is in the header, there is a text box
field that I can not seem to figure out how to get access. When I use
your
example for accessing the header / footer, the text box never is
accessable.
Could you please shed some light on that for me also?

Thanks. Your input is very helpful


"Peter Jamieson" wrote:

OK, unless you are actually using an OpenDataSource to open a data
source,
you can't actually use Mailmerge to populate those merge fields for you.
Since you're getting your data from a web service, you probably are not
able
to use OpenDataSource because that requires the data to come from a file,
or
a type of data source recognised by Word such as OLEDB or ODBC. If you
are
able to create a file with a local or Microsoft network address (i.e. one
that you can only open using an Internet URL won't work because
OpenDataSource will not open it) then that is probably the simplest way
to
populate your merge fields.

If you cannot do that (e.g. because you are not allowed to create local
files) then the only approach is to replace whatever placeholders exist
in
the document with your own data, i.e. simulate a merge. That's what I'm
assuming you are trying to do, and if so:
a. be aware that Mailmerge actually does quite a lot of stuff that it is
non-trivial to write yourself. For example, if for some reason there are
nested fields in your document, you may have to ensure that your code
replaces the placeholders in a specific sequence. Inserting data in
headers
and footers can also be tricky if you need to create multi-section
documents
as output.
b. you may find it useful to use field types other than MERGEFIELD
fields
as placeholders. In particular, you can consider using { DOCVARIABLE } or
{ DOCPROPERTY } fields in the main document - the advantage is that you
can
populate the Document variables and/or Document properties in code, then
select the document and re-execute all the fields, and Word will do the
rest. You don't need to find the fields in the document etc. etc. The
main
drawback is that if you try to put an empty string in a Document
variable,
Word will delete the variable and the { DOCPROPERTY } field will display
an
error message when evaluated.
c. if you use that approach, you may need to loop through all the
"Stories"
in the document to update the fields, e.g. using the VB.NET equivalent of
the following VBA code:

'---------------
Sub RefreshAllStories()
Dim r As Range
For Each r In ActiveDocument.StoryRanges
r.Fields.Update
Next
'---------------


Otherwise, to go back to original question, to loop round all the fields
in
a document, you need something more like the following, which should find
most of them but may have trouble detecting some fields in the graphics
layer:

'---------------
Sub IterateMergeFields()
Dim r As Range
Dim f As Field
Dim s As Shape
For Each r In ActiveDocument.StoryRanges
If r.StoryType wdTextFrameStory Then
For Each f In r.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
For Each s In ActiveDocument.Shapes
If s.Type = msoTextBox Then
For Each f In s.TextFrame.TextRange.Fields
If f.Type = wdFieldMergeField Then
' do whatever you need
End If
Next
End If
Next
End Sub

'---------------

To get references to Headers and Footers, use the StoryRanges collection
to
access specific types of header and footer, e.g.

ActiveDocument.StoryRanges(wdPrimaryHeaderStory)

You may need to trap errors in the case where the specified Story does
not
exist.

Peter Jamieson

"ben" wrote in message
...
I have a word document with a header, body, and footer sections. Merge
fields are located in all sections. I am receiving the data needed to
populate these documents via a web service. The web servcie will
populate
the document, then send the document to the printer. Using a
datasource
is
the last option I have. The preferred option is to have the webservice
populate the document via vb.net code and send to a printer. i am also
struggling on how to get a reference to the header and footer objects.

"Peter Jamieson" wrote:

"Fields" include Merge fields and Form fields. Form fields and Merge
fields
are typically used for different purposes. Generaly speaking, it is
probably
a good idea to populate Merge fields by setting up and executing a
mail
merge (and letting Word fill in the values of each field).

If the documents that have been given to you were designed to be used
in
a
particular way, it may not be a straightforward process to adapt them
so
that they can be used with mailmerge, or any other form of document
automation. It may help if you can provide some more details about
what
you
are trying to do,

Peter Jamieson

"ben" wrote in message
...
I have several documents that have merge fields identified. I am not
able
to
populate the forms using mail merge, I need to populate
programatically
via
VB.NET. I noticed the Word.Document.Fields.count matches the number
of
merge
fields in the document while Word.Document.FormFields.count = 0.
Can
someone
point me in the correct direction on populating a merge field
programatically








 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge Fields and Formating - How to easily repeat a pre-set merge. David Nelson Mailmerge 7 December 27th, 2004 09:55 PM
unlinking merge fields Help Me General Discussion 1 August 6th, 2004 03:22 AM
Lost text fields in merge CGiannini General Discussion 5 June 4th, 2004 10:23 PM
Mail Merge two fields that have multiple lines with new line control code Eric Li Mailmerge 7 May 25th, 2004 06:10 PM
How can I force two merge fields to calculation in word 2000 Jane Humbrey Mailmerge 1 April 30th, 2004 12:34 AM


All times are GMT +1. The time now is 10:17 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.