Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combine INCLUDEPICTURE with variable?

578 views
Skip to first unread message

Abel MacAdam

unread,
Feb 23, 2010, 2:39:01 AM2/23/10
to
Hi,

Is it possible to use an INCLUDEPICTURE with a variable that is filled while
executing a macro?

In the articles http://www.onmerge.com/articleIncludePicture.html and
http://www.gmayor.com/mail_merge_graphics.htm the use of INCLUDEPICTURE is
explained, in combination with Mail Merge. But I will not use the document
for a mailmerge of customer letters but for contracts that will be written
for new customers in a piecemeal way. In the macro I developed I have a
'customer' field that needs to be filled in. After the macro has executed all
occurrences of the 'customer' field are replaced with the customer name. What
I would like to accomplish is something like:
{ INCLUDEPICTURE “c:\temp\{ “customer” }.jpg” \* MERGEFORMAT \d}

What do I need to do tocombine INCLUDEPICTURE with a variable?

Abel

Graham Mayor

unread,
Feb 23, 2010, 3:16:11 AM2/23/10
to
What sort of field is used to store the CustomerName?

The fact that you have posted this in a vba userform forum suggests that it
might be a field on a userform, but you also mention 'all occurrences of the
customer field'.
If it is a userform then the best way to deal with this is to assign the
value of the CustomerName field on the userform to a docvariable and use
docvariable fields to display the data e.g. In the following code snippet,
let's assume sText is the value from the customer name field on the userform

Dim oVars As Variables
Dim sText As String
sText = "Customer Name"
Set oVars = ActiveDocument.Variables
oVars("varCustomer").Value = sText
ActiveDocument.Fields.Update

In the document you would use

{ IncludePicture "D:\\My Documents\\My Pictures\\{ DocVariable
varCustomer }.jpg" }

to display the image (substituting the correct path to the images). Note the
double slashes.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

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

"Abel MacAdam" <AbelM...@discussions.microsoft.com> wrote in message
news:C17CCEBD-B606-4398...@microsoft.com...

macropod

unread,
Feb 23, 2010, 5:23:30 AM2/23/10
to

Hi Abel,

If you Add the Customer Name to the 'Client' document property (see under File|Properties|Custom), you could use a DOCPROPERTY field
in conjunction with the INCLUDEPICTURE field, thus:
{ INCLUDEPICTURE "c:\temp\{DOCPROPERTY Client}.jpg" \d}
An added bonus is that, having input the client's name into the 'Client' document property, you can use a DOCPROPERTY field to
insert that info wherever else you need it in the document.

--
Cheers
macropod
[Microsoft MVP - Word]


"Abel MacAdam" <AbelM...@discussions.microsoft.com> wrote in message news:C17CCEBD-B606-4398...@microsoft.com...

Graham Mayor

unread,
Feb 23, 2010, 8:33:17 AM2/23/10
to

That is an indication of an incorrectly resolved image filename or path.
Does the image file exist? Have you updated the includepicture field?

Did you use CTRL+F9 to enter the curly brackets {}?

{ INCLUDEPICTURE "c:\\temp\\{ DocVariable customer }.jpg" }


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

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


"Abel MacAdam" <AbelM...@discussions.microsoft.com> wrote in message

news:CEC2B70C-47B9-4F86...@microsoft.com...
> Graham (and macropod),
>
> Thanks for the answer. I tried out what you said. What I got is a big
> "placeholder" for the image with in the top left corner a small red cross.
>
> I'm still doing something wrong.
>
> If I'm correct this is the relevant code:
> [Code===============]
> Sub CallUF()
> Dim oFrm As MyForm
> Dim oVars As Word.Variables
> ...
> Set oVars = ActiveDocument.Variables
> Set oFrm = New MyForm
> With oFrm
> .Show
> If .boolProceed Then
> oVars("customer") = .customer.Text
> ...
> UpdateFormFields
> End If
> End With
> ...
> Unload oFrm
> Set oFrm = Nothing
> Set oVars = Nothing
> End Sub
>
> ...
>
> Sub UpdateFormVelden()
> Dim pRange As Word.Range
> Dim iLink As Long
> iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
> For Each pRange In ActiveDocument.StoryRanges
> Do
> pRange.Fields.Update
> Set pRange = pRange.NextStoryRange
> Loop Until pRange Is Nothing
> Next
> 'Next line is added so customer logo will be updated
> ActiveDocument.Fields.Update
> End Sub
> [/Code===============]
>
> I changed the INCLUDEPICTURE to:
> { INCLUDEPICTURE "c:\\temp\\{ DocVariable customer }.jpg" \* MERGEFORMAT
> \d }
>
> But no luck :(
>
> Abel

>> .
>>


Abel MacAdam

unread,
Feb 23, 2010, 8:04:01 AM2/23/10
to
Graham (and macropod),

...

{ INCLUDEPICTURE “c:\\temp\\{ DocVariable customer }.jpg” \* MERGEFORMAT \d }

But no luck :(

Abel

"Graham Mayor" wrote:

> .
>

Abel MacAdam

unread,
Feb 24, 2010, 5:02:01 AM2/24/10
to
Hi Graham,

Still no luck.

When I hardcode the customer I get the picture:
{ INCLUDEPICTURE "C:\\Temp\\ZX.jpg" \d }

When I try it with the docvariable I get the big square with the small x in
the top left corner:
{ INCLUDEPICTURE "C:\\Temp\\{ DocVariable Klantnaam }.jpg" \d }

Might it be caused by the following piece of code (in which I added the
ActiveDocument.Fields.Update?
[code========]
Sub UpdateFormFields()


Dim pRange As Word.Range
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
For Each pRange In ActiveDocument.StoryRanges
Do
pRange.Fields.Update
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
'Next line is added so customer logo will be updated
ActiveDocument.Fields.Update
End Sub

[/code================]

Is the INCLUDEPICTURE field updated after the ActiveDocument.Fields.Update?
(It must be, otherwise the { INCLUDEPICTURE "C:\\Temp\\ZX.jpg" \d } would not
have delivered the requested image).

Abel

"Graham Mayor" wrote:

> .
>

Graham Mayor

unread,
Feb 24, 2010, 6:52:38 AM2/24/10
to
Display the document formatting (CTRL+SHIFT+8)

What does the field { DocVariable Klantnaam } produce?

Run the macro:

Sub Test()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("Klantnaam").Value = "ZX"
ActiveDocument.Fields.Update
End Sub

Now what does it produce?

Does { INCLUDEPICTURE "C:\\Temp\\{ DocVariable Klantnaam }.jpg" \d }
now work?

If it does (as it should) you need to check back and see *exactly* what you
are writing to the document variable


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

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


"Abel MacAdam" <AbelM...@discussions.microsoft.com> wrote in message

news:AA62BD60-2C15-4E46...@microsoft.com...

macropod

unread,
Feb 24, 2010, 4:57:00 PM2/24/10
to
Hi Graham,

Perhaps you meant Alt-F9 or, select the field and press Ctrl-Shift-F9?

Still, if the OP presses Ctrl-F9 to create a pair of field braces (ie '{}') and fills in the space between them thus:
{ DocVariable Klantnaam }
then presses F9, that should tell us whether the DocVariable field is working and what it is retrieving.

--
Cheers
macropod
[Microsoft MVP - Word]


"Graham Mayor" <gma...@REMOVETHISmvps.org> wrote in message news:u2ndZfUt...@TK2MSFTNGP04.phx.gbl...

Graham Mayor

unread,
Feb 25, 2010, 4:21:09 AM2/25/10
to
No I did mean CTRL+SHIFT+8 which would display things not immediately
obvious, such as unwanted space at the start of end of the 'filename'

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

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


"macropod" <macr...@invalid.invalid> wrote in message
news:eazR4wZt...@TK2MSFTNGP02.phx.gbl...

Abel MacAdam

unread,
Feb 25, 2010, 6:03:01 AM2/25/10
to

Hi Graham (and macropod),

The document formatting produced no result. Running the macro neither. But I
now think I have the problem solved (I get an image on the screen). It was
caused by me having the following line:


{ INCLUDEPICTURE " C:\\Temp\\{ DocVariable Klantnaam }.jpg" \d }

Notice the space after the double quote?

I now am placing the image in a text field so I have some say about the size
of the image.

Thanks
Abel

"Graham Mayor" wrote:

> .
>

Abel MacAdam

unread,
Mar 1, 2010, 5:01:01 AM3/1/10
to
Graham,

I have to ask a follow up question. When I put the
{ INCLUDEPICTURE "C:\\Temp\\{ DocVariable customer }.jpg" \d }
in the document I see the requested image. I tested it with customers and
got two different images. But also two wildly different sizes. I have read I
could solve this by putting the above mentioned code inside a text field, or
a table cell. The size of the text field or table cell should keep the size
of the image in check. I choose for a text field. But then it does not work.
Might that be caused because the
ActiveDocument.Fields.Update
does not reach fields inside text fields?

Abel

"Graham Mayor" wrote:

> .
>

Graham Mayor

unread,
Mar 1, 2010, 5:22:47 AM3/1/10
to

Put the field in a table cell (with or without a border) of fixed width. The
image will adapt to the width of the cell.
Do not use a text box and do not use a text form field.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

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


"Abel MacAdam" <AbelM...@discussions.microsoft.com> wrote in message

news:0DEE2F7F-37D8-4E40...@microsoft.com...

Abel MacAdam

unread,
Mar 2, 2010, 4:26:02 AM3/2/10
to
Hi Graham,

This seems to solve the problem. However, I have to find out how to get a
cell with fixed width. When I try to set it to fixed width it jumps back to
the default auto change ~:-|

Abel

"Graham Mayor" wrote:

> .
>

Graham Mayor

unread,
Mar 2, 2010, 4:39:18 AM3/2/10
to
Select the table then select AutoFit > Fixed Column width?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

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


"Abel MacAdam" <AbelM...@discussions.microsoft.com> wrote in message

news:DF8A633D-B850-4EBB...@microsoft.com...

0 new messages