Graphics insertion

G

Guest

Quick question:

The objective is to insert a full page graphic into a page in a
document. On this page exist a couple of bookmarks which are filled in
later. To build the template...

1. Create the bookmarks on the page where I need them.
2. Insert a Microsoft Word Picture object, size it and place it.

This VBA populates the page - but there's a serious inconsistency.
Sometimes, the graphic goes into an edit mode and prompts me to "Close
Picture". This happens once in every 3 or 4 "loads". Second -
regardless of what I do, the bookmark ends up behind the picture. Can
anyone clue me in as to why this is happening?

Cheers,
Mike

<VBA>
Set g_word = New Word.Application
g_word.Application.ChangeFileOpenDirectory "C:\\Documents and
Settings\\foo\\My Documents\\foobar\\VB"

Set g_doc = g_word.Documents.Add("SampleTemplate")
g_word.Visible = False

Dim shp As Word.Shape
Dim r As Word.Range

Set r = g_doc.Bookmarks.Item("CompanyName").Range
r.Text = "Testing 1234"

g_word.ActiveDocument.Shapes("TitlePageGraphic").Select


Set inlineImg = g_word.Selection.InlineShapes.AddPicture(FileName:= _
"C:\Documents and Settings\foo\My
Documents\foobar\Templates\image2.jpg", _
LinkToFile:=False, SaveWithDocument:=True)

Set shp = inlineImg.ConvertToShape

With shp

..RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
..RelativeVerticalPosition = wdRelativeVerticalPositionPage
..Top = InchesToPoints(0.25)
..Left = InchesToPoints(0.25)
..Height = InchesToPoints(10.5)
..Width = InchesToPoints(8#)
..ZOrder msoSendBehindText

End With

Set tmpImg = Nothing
Set inlineImg = Nothing

g_word.Visible = True
</VBA>
 
Å

Åsa Holmgren

Try to insert the graphics as a shape object instead as inline shape:

<VBA>
Set g_word = New Word.Application
Set g_doc = g_word.Documents.Add("SampleTemplate")
Set r = g_doc.Bookmarks("CompanyName").Range

r.Text = "Testing 1234"

Set shp = g_doc.Shapes.AddPicture( _
FileName:="C:\Documents and Settings\foo\" _
& "My Documents\foobar\Templates\image2.jpg", _
LinkToFile:=False, Anchor:=r)

With shp
.Height = InchesToPoints(10.5)
.Width = InchesToPoints(8#)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = InchesToPoints(0.25)
.Top = InchesToPoints(0.25)
.LockAnchor = False
.WrapFormat.AllowOverlap = True
.WrapFormat.Type = 3
.ZOrder msoSendBehindText
End With

g_word.Visible = True
</VBA>
 
G

Guest

Asa,

Thanks for the response. Right now, the picture is referenced not by a
bookmark, but by an object. In your sample snippet, the object
reference is missing. I would have to insert a bookmark where I want
the graphic... Right now, the guy who edits the templates knows exactly
where the graphic will go based on the geometry of the picture box
(empty though it is). While this is an alternative we may have to
pursue, I managed to get it to work by adding

..ZOrder msoSendToBack

before the last .ZOrder command.

WHY this would make any difference at all is beyond me... but hey, it
consistently works, so it'll do. Until it screws up again. :)
Best,
Michael
 
G

Guest

Asa,

Thanks for the response. Right now, the picture is referenced not by a
bookmark, but by an object. In your sample snippet, the object
reference is missing. I would have to insert a bookmark where I want
the graphic... Right now, the guy who edits the templates knows exactly
where the graphic will go based on the geometry of the picture box
(empty though it is). While this is an alternative we may have to
pursue, I managed to get it to work by adding

..ZOrder msoSendToBack

before the last .ZOrder command.

WHY this would make any difference at all is beyond me... but hey, it
consistently works, so it'll do. Until it screws up again. :)
Best,
Michael
 
Å

Åsa Holmgren

Michael,
nice to hear that the solution works!

You could set the achor of the new shape to the range of the
"TitlePageGraphic" shape, like this:

<--snipp-->
Set r = g_doc.Shapes("TitlePageGraphic").Anchor
Set shp = g_doc.Shapes.AddPicture( _
FileName:="C:\Bilder&ljud\Jpeg\Love.jpg", _
LinkToFile:=False, Anchor:=r)
<--snipp-->

If the graphic is to be on the documents first page you don't have to
specify an anchor when you are using wdRelativeXxxPage.

It is the following code that tells the size and position of the inserted
graphic,
not the size and position of the "TitlePageGraphic" shape:

<--snipp-->
With shp
.Height = InchesToPoints(10.5)
.Width = InchesToPoints(8#)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = InchesToPoints(0.25)
.Left = InchesToPoints(0.25)
End With
<--snipp-->

/Åsa
 

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