Strange MERGEFIELD insertion behaviour.....

R

Robin Tucker

I'm trying to programmatically insert MERGEFIELD items into my document.
They all seem to work ok, except those I need to add parameters to. For
example, the following mergefield needs to be inserted:

"MERGEFIELD ASSET_Named_Tool=Camera Spot 1"

ie. I want a merge field included called "ASSET_Named_Tool=Camera Spot 1".
When I come to generate a report using this template, I can parse the
mergefield and extract the value I need called "Camera Spot 1". Anyway, I
digress. When I manually insert a mergefield using the add Field dialog,
giving the field the name ASSET_Named_Tool=Camera Spot 1, the result is as
expected (ie. I get the << >> brackets around the entire string).

When I do the same thing programmatically, it "cuts off" the string at the
end of "Camera". I guess because it isn't too keen on spaces in field names.
However, if I right click on the newly inserted field and "Edit Field", then
click ok when the edit field dialog is shown, the string seems to be changed
to be correct!

Summary:

(1) Manual insertion produces correct result:
«ASSET_Named_Tool=Camera Spot 1»
(2) Programmatic insertion produces erroneous result:
«ASSET_Named_Tool=Camera»
(3) Programmatic insertion, followed by manual "Edit Field", produces
correct result.

Here is the code I'm using to add the field:

Dim theInfo as String

theInfo = "Camera Spot 1"
Dim theText As String = "MERGEFIELD " + "ASSET_Named_Tool" + "=" + theInfo

m_Application.Selection.Fields.Add(Range:=m_Application.Selection.Range, _

Type:=Word.WdFieldType.wdFieldEmpty, _
Text:=CType(theText,
Object), _

PreserveFormatting:=True)

Thanks for any help you can give me.


Robin
 
W

Word Heretic

G'day "Robin Tucker" <[email protected]>,

Code sample please


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Robin Tucker reckoned:
 
R

Robin Tucker

Err, I included a code sample. Here it is again:

Dim theInfo as String

theInfo = "Camera Spot 1"
Dim theText As String = "MERGEFIELD " + "ASSET_Named_Tool" + "=" + theInfo

m_Application.Selection.Fields.Add(Range:=m_Application.Selection.Range, _

Type:=Word.WdFieldType.wdFieldEmpty, _
Text:=CType(theText,
Object), _

PreserveFormatting:=True)
 
W

Word Heretic

G'day "Robin Tucker" <[email protected]>,

Ahhhh. You are inserting an empty field. Insert a Merge field and
include the extensions as the parm.

Dim theText As String = "ASSET_Named_Tool" + "=" + theInfo
m_Application.Selection.Fields.Add( _
Range:=m_Application.Selection.Range, _
Type:=Word.WdFieldType.wdFieldMergeField, _
Text:=CType(theText,Object), _
PreserveFormatting:=True)


I'd be using & as the concat if available - it stops a few typing
problems. Also, I would have thought

Word.WdFieldType.wdFieldMergeField

would be

m_Application.wdFieldMergeField

But I dunno which language you are using.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Robin Tucker reckoned:
 
R

Robin Tucker

Doh!

Thanks....

Word Heretic said:
G'day "Robin Tucker" <[email protected]>,

Ahhhh. You are inserting an empty field. Insert a Merge field and
include the extensions as the parm.

Dim theText As String = "ASSET_Named_Tool" + "=" + theInfo
m_Application.Selection.Fields.Add( _
Range:=m_Application.Selection.Range, _
Type:=Word.WdFieldType.wdFieldMergeField, _
Text:=CType(theText,Object), _
PreserveFormatting:=True)


I'd be using & as the concat if available - it stops a few typing
problems. Also, I would have thought

Word.WdFieldType.wdFieldMergeField

would be

m_Application.wdFieldMergeField

But I dunno which language you are using.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Robin Tucker reckoned:
 
R

Robin Tucker

Oh actually.....my code did include MERGEFIELD, but my example did not (I'm
not sure why). Still I get the described behaviour with the code below:

(where theField = "ASSET_Named_Tool Camera Spot 1")....

m_Application.Selection.Fields.Add(Range:=m_Application.Selection.Range, _

Type:=Word.WdFieldType.wdFieldEmpty, _
Text:="MERGEFIELD "
+ theField.ToString() + " ", _

PreserveFormatting:=True)
 
W

Word Heretic

G'day "Robin Tucker" <[email protected]>,

That's not the code I provided :)


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Robin Tucker reckoned:
 
C

Cindy M -WordMVP-

Hi Robin,

Maybe we need to look at this from a slightly different angle: you appear to
be trying to use mergefields for something for which they were not designed.
Why are you using mergefields, and not something else? What are the
requirements for these "target" areas?

Conceivably, you might be able to force the field to accept the (for that
field) invalid text by using the .Code property of the field object, once
the field has been inserted...
'm trying to programmatically insert MERGEFIELD items into my document.
They all seem to work ok, except those I need to add parameters to. For
example, the following mergefield needs to be inserted:

"MERGEFIELD ASSET_Named_Tool=Camera Spot 1"

ie. I want a merge field included called "ASSET_Named_Tool=Camera Spot 1".
When I come to generate a report using this template, I can parse the
mergefield and extract the value I need called "Camera Spot 1".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
R

Robin Tucker

Well, I am doing a "merge", only manually (with VB). I need to place fields
into the document because the user needs to be able to see them (when
editing the document template), hence I don't think document variables will
work too well. I solved my problem by not allowing the user to input an
asterisk in a merge field name and changing spaces in the field to asterisks
before adding the field. When I parse the document, I change them back.
 

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