How to insert mergefield inside a includepicture field using VB?

F

Feng

Hi,
I have also posted this in the programming section, because I wasn't sure which one this belongs to.

currently, I use the following code:
Dim fldCode as String
WordMergeField = WordMergeFields.Add(WordSelection.Range, type)
fldCode = WordMergeField.Code.Text
WordMergeField.Delete()
WordInline.AddPicture("C:\proj\hrosen\JPT0803\Signature\" & fldCode & ".bmp", True, False, WordSelection.Range)

The problem is Code.text removes the brackets around the wordMergeField. the merge field I'm using actually provides the name of each image. Basically I want the Document to show up something like,
{INCLUDEPICTURE "C:\\proj\\hrosen\\JPT0803\\Signature\\{MergeField LIST}.bmp" \d }
instead, I'm getting
{INCLUDEPICTURE "C:\\proj\\hrosen\\JPT0803\\Signature\\MergeField LIST.bmp" \d }
with this code. What can I do to change this?

Thanks
 
F

Feng

if the original is not very specific, I redone the code.
Dim WordDoc As Word.Document
Dim WordObject As Word.Application
Dim WordMerge As Word.MailMerge
Dim WordMergeFields As Word.MailMergeFields
Dim WordSelection As Word.Selection
Dim WordMergeField As Word.MailMergeField
Dim WordFields As Word.Fields
Dim WordInline As Word.InlineShapes
Dim fldCode as String

WordObject = CreateObject("Word.Application")
WordObject.Visible = True

WordDoc = WordObject.Documents.Open("C:\proj\hrosen\JPT0803\f011.doc")
WordDoc.Select()
WordSelection = WordObject.Selection()
WordInline = WordDoc.InlineShapes()
WordMerge = WordDoc.MailMerge()
WordFields = WordDoc.Fields()

WordMergeFields = WordMerge.Fields()
WordMergeField = WordMergeFields.Add(WordSelection.Range, type)
fldCode = WordMergeField.Code.Text
WordMergeField.Delete()
WordInline.AddPicture("C:\proj\hrosen\JPT0803\Signature\" & fldCode & ".bmp", True, False, WordSelection.Range)

The problem is Code.text removes the brackets around the wordMergeField. the merge field I'm using actually provides the name of each image. Basically I want the Document to show up something like,
{INCLUDEPICTURE "C:\\proj\\hrosen\\JPT0803\\Signature\\{MergeField LIST}.bmp" \d }
instead, I'm getting
{INCLUDEPICTURE "C:\\proj\\hrosen\\JPT0803\\Signature\\MergeField LIST.bmp" \d }
with this code. What can I do to change this?

Thanks
 
P

Peter Jamieson

You can't do it in one - you basically have to insert everything except the
{ MERGEFIELD List }, then navigate using a selection or range to the correct
point in the INCLUDEPICTURE string and insert the mergefield.

For example, you could use something that worked roughly like this:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=
_
"INCLUDEPICTURE
""C:\\proj\\hrosen\\JPT0803\\Signature\\@[email protected]"" \d",
PreserveFormatting:=False
Selection.Find.ClearFormatting
With Selection.Find
.Text = "@placeholder@"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=
_
"MERGEFIELD List", PreserveFormatting:=False

However, you'll probably be better off using Range objects where possible
instead of the Selection object, and you may need to ensure that fields are
visible to make it work.

--
Peter Jamieson
MS Word MVP

Feng said:
Hi,
I have also posted this in the programming section, because I wasn't sure which one this belongs to.

currently, I use the following code:
Dim fldCode as String
WordMergeField = WordMergeFields.Add(WordSelection.Range, type)
fldCode = WordMergeField.Code.Text
WordMergeField.Delete()
WordInline.AddPicture("C:\proj\hrosen\JPT0803\Signature\" & fldCode &
".bmp", True, False, WordSelection.Range)
The problem is Code.text removes the brackets around the wordMergeField.
the merge field I'm using actually provides the name of each image.
Basically I want the Document to show up something like,
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RmVuZw==?=,
I have also posted this in the programming section, because I wasn't sure which one this belongs to.
It's very helpful to those who answer questions, as well as for the asker, to
not post multiple, single messages. That makes it very difficult to follow a
discussion. Also, time is valuable, and none of us want to waste any by
duplicating effort.

Yes, Microsoft does not provide any really good way to insert field codes into
fields. I wrote an article with an approach for this a couple of years ago for
Informant publications. Unfortunately, they no longer have the archives
available on the Web, only on a CD...

Roughly, what you need to do is use the Fields.Add method to insert the outer
field level, first. In the TEXT argument, use a placeholder to indicate where
the nested field has to go.

In a next step, display the field code and use Find/Replace to locate the
placeholder. Now use Fields.Add once again to insert the nested field.

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

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

Feng

Hey I tried to use Find to do it, but for some reason, I always gets an error when I run it. When it gets to the line
Selection.Find.Replacement.Text = ""

It always gives an error "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in AutoMerge.exe
Additional information: Exception from HRESULT: 0xC0000005."
When I comment out this line, the lines after Forward give an error "An unhandled exception of type 'System.NullReferenceException' occurred in AutoMerge.exe
Additional information: Object reference not set to an instance of an object."

Also, when I just use Selection.FindExecute, it still gives the error "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in AutoMerge.exe

Additional information: The stub received bad data."

When I got rid of several parameters, it still gives the same error.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RmVuZw==?=,

See if the information in this Knowledge Base article helps

BUG: Automation Client Receives Error Calling Word Find Object [Q292744]
http://support.microsoft.com?kbid=292744
Hey I tried to use Find to do it, but for some reason, I always gets an error when I run it. When it gets to the line
Selection.Find.Replacement.Text = ""

It always gives an error "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in AutoMerge.exe

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

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

Peter Jamieson

Can't help you with the .NET error, but another approach you could consider
is:

Instead of using a placeholder text and finding it, if you're working with
the selection, use

Selection.MoveLeft Unit:=wdCharacter, Count:=6

or whatever to move to the correct place. As long as you always use the same
field options, the number of characters should always be the same.


--
Peter Jamieson
MS Word MVP

Feng said:
Thanks a bunch Cindy, but I can't seem to find any bug report that fits my problem.

Even commenting away the problem attributes of Find Object gives me problems
With WordSelection.Find
.Text = "@placeholder@"
.Forward = True
End With
WordSelection.Find.Execute()
I get this error:
"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in AutoMerge.exe
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?RmVuZw==?=,
Thanks a bunch Cindy, but I can't seem to find any bug report that fits my problem.
did you click the LINK that I posted in my last reply? That should take you right to
the article that deals with this problem, what causes it, and what options you have
for working around it...
BUG: Automation Client Receives Error Calling Word Find Object [Q292744]
http://support.microsoft.com?kbid=292744

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

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

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