INCLUDETEXT with VBA?

C

Cheryl

Hi,

I am pretty new to VB and MailMerge. I am doing a mailmerge with a
csv data source and it has a variable number of fields named by their
doc name with a "Y" value if they are to be included.

I was wondering if I can use INCLUDETEXT from within VBA. (It seems
that I have to use VB to get the names of the fields (and count
variable number of fields) Basically, what I want is a variable
number of IF-THEN-ELSE fields that use INCLUDETEXT. (with variable
field names)
{IF {MERGEFIELD <fieldname>}="Y" "{INCLUDETEXT "C:\\<fieldname>.doc"}"
""}

Is there a way to
1. Loop through fields from within word
2. Get the field names?

Or, in VBA can I use INCLUDETEXT within the TrueText of an AddIf
statement?

I have:

For Each afield In wrdDoc.MailMerge.DataSource.FieldNames
wrdMergeFields.AddIf Range:=wrdSelection.Range, _
MergeField:=afield.Name, Comparison:=wdMergeIfEqual,
CompareTo:="Y", _
TrueText:=afield.Name + vbCrLf
Next afield

which works great for listing the names of the files!

What I need is something like:
TrueText:="{INCLUDETEXT" & quote & "C:\\" & afield.name & ".doc"} &
quote

Is there a way to do this?

Thanks very much in advance for any help you can provide.

Cheryl Kinden
 
P

Peter Jamieson

Is there a way to
1. Loop through fields from within word
2. Get the field names?

Assuming I've understood correctly, there's no way to loop through a
variable number of fields /in the field language/, i.e. you have to use VBA.
In any case, you will have to use VBA to attach the data source each time if
it could have a variable number of fields with varyong names.

To loop round the fields in VBA you need something like

Dim df as MailMergeDataField
For Each df in ActiveDocument.MailMerge.DataSource.Datafields
debug.print df.Name, df.Value
Next

You may also need some way of determining which of the fields in your data
source is the sot of field that needs to be converted into an INCLUDETEXT.

If you are using Word XP/2002 or later, you can probably solve this by using
a MailMerge event (the Before Record event) to insert the documents directly
for each record in the mail merge data source, rather than inserting an
INCLUDETEXT field and leaving the merge process to include the file.

Otherwise...
Or, in VBA can I use INCLUDETEXT within the TrueText of an AddIf
statement?

Unfortunately not - the closest you can get is to have the necessary
INCLUDETEXT in an Autotext named in the TrueAutoText of the AddIf. Which
means you either have to set up the autotexts first (a bit of a pain) or do
the insertion in two or three steps, e.g. something like

Sub InsertIncludePerField()
'
'
Dim oDataField As MailMergeDataField
Dim oRange As Range
Dim oDoc As Document
Set oDoc = ActiveDocument
For Each oDataField In oDoc.MailMerge.DataSource.DataFields

' Decide where you want to put the field.
' For the moment, I am just putting them at the end

Set oRange = oDoc.Content
oRange.Collapse direction:=wdCollapseEnd
oDoc.MailMerge.Fields.AddIf _
Range:=oRange, _
MergeField:=oDataField.Name, _
Comparison:=wdMergeIfEqual, _
CompareTo:="Y", _
TrueText:="@placeholder@", _
FalseText:=""

oRange.Find.ClearFormatting
With oRange.Find
.Text = "@placeholder@"
.Replacement.Text = ""
.Forward = False
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oRange.Find.Execute
oRange.Fields.Add _
Range:=oRange, _
Type:=wdFieldEmpty, _
Text:="INCLUDETEXT """ & oDataField.Name & ".doc""", _
PreserveFormatting:=False
Next
Set oRange = Nothing
Set oDoc = Nothing
End Sub
 

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