Automating picture insertion- automating Format Picture->Layout->Behind Text

R

Randy

Hi,
I am trying to automate the insertion of a picture into a Word
document. I have created a VBA macro which inserts a particular
picture at the current insertion point. However, I would like the
inserted picture to be placed behind the existing text of the
document. This is accessible after the picture is inserted by
selecting the picture, then selecting the Format Picture command/
toolbar, then the Layout tab and then selecting the Behind Text
button. I cannot seem to figure out how to script this same sequence
using VBA. It won't allow me to record a macro while performing these
steps, either. Does anybody have any thoughts/suggestions?

Thanks in advance,
-Randy
 
J

JE McGimpsey

Randy said:
Hi,
I am trying to automate the insertion of a picture into a Word
document. I have created a VBA macro which inserts a particular
picture at the current insertion point. However, I would like the
inserted picture to be placed behind the existing text of the
document. This is accessible after the picture is inserted by
selecting the picture, then selecting the Format Picture command/
toolbar, then the Layout tab and then selecting the Behind Text
button. I cannot seem to figure out how to script this same sequence
using VBA. It won't allow me to record a macro while performing these
steps, either. Does anybody have any thoughts/suggestions?

One way:

Public Sub InsertPictureToBack()
Const sPICTUREPATH As String = "your path here"
Dim shPicture As Shape
With ActiveDocument.InlineShapes
.AddPicture FileName:=sPICTUREPATH, _
LinkToFile:=False, SaveWithDocument:=True
Set shPicture = .Item(.Count).ConvertToShape
End With
With shPicture
.WrapFormat.Type = wdWrapNone
.ZOrder msoSendBehindText
'position here
End With
End Sub
 
R

Randy

Well, after looking at your posted script and copy/pasting it in (and
then removing the extra spacing), it works! Thank you.

What I don't understand is why I got all sorts of errors based on the
fact that I had whitespace in front of each of the lines after pasting
them into the VB Editor. I got syntax errors on each line. Weird.
Removed the extra space at the front and it works fine.

Thanks again,
-Randy
 
J

JE McGimpsey

Randy said:
What I don't understand is why I got all sorts of errors based on the
fact that I had whitespace in front of each of the lines after pasting
them into the VB Editor. I got syntax errors on each line. Weird.
Removed the extra space at the front and it works fine.

You're probably reading the newsgroups via a web portal that uses
non-breaking spaces when displaying the code instead of properly
wrapping it inside a <pre> element.

When nbsp's are inserted into the Visual Basic Editor, it doesn't
interpret them as whitespace, and so throws the error.
 
R

Randy

One way:

Public Sub InsertPictureToBack()
Const sPICTUREPATH As String = "your path here"
Dim shPicture As Shape
With ActiveDocument.InlineShapes
.AddPicture FileName:=sPICTUREPATH, _
LinkToFile:=False, SaveWithDocument:=True
Set shPicture = .Item(.Count).ConvertToShape
End With
With shPicture
.WrapFormat.Type = wdWrapNone
.ZOrder msoSendBehindText
'position here
End With
End Sub

Okay,
I give up. I got the above working as I noted previously (thank
you thank you thank you again) and I thought I could figure out the
'position here portion. Unfortunately, I have not spent enough time
with VB to grock this fully and can't seem to work it out. If I run
the above macro, it places the inserted picture at the beginning of
the document. What I need to do is have it insert the picture at the
current selection point in the document. (Yes, it got old real fast
having to drag the inserted picture from the beginning back a couple
of pages into the document. :^) Can I simply set the position of
shPicture to the current selection in some fashion?

Thanks,
-Randy
 
J

JE McGimpsey

Randy said:
I give up. I got the above working as I noted previously (thank
you thank you thank you again) and I thought I could figure out the
'position here portion. Unfortunately, I have not spent enough time
with VB to grock this fully and can't seem to work it out. If I run
the above macro, it places the inserted picture at the beginning of
the document. What I need to do is have it insert the picture at the
current selection point in the document. (Yes, it got old real fast
having to drag the inserted picture from the beginning back a couple
of pages into the document. :^) Can I simply set the position of
shPicture to the current selection in some fashion?

Here's one way. This inserts the picture at the start of the selection,
then positions it relative to the left margin and top of line that the
anchor is at.

Public Sub InsertPictureToBack()
Const sPICTUREPATH As String ="your path here"
Dim shPicture As Shape
Dim rng As Range
Set rng = Selection.Range
rng.Collapse Direction:=wdCollapseStart
With ActiveDocument.InlineShapes
.AddPicture FileName:=sPICTUREPATH, _
LinkToFile:=False, SaveWithDocument:=True, Range:=rng
Set shPicture = .Item(.Count).ConvertToShape
End With
With shPicture
.WrapFormat.Type = wdWrapNone
.ZOrder msoSendBehindText
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionMargin
.RelativeVerticalPosition = 3 'relative to line
.Left = 0
.Top = 0
.LockAnchor = True
'position here
End With
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