We help IT Professionals succeed at work.

Help with Word VBA sub

I've got a sub that works ok, but I'm unsure about the syntax. Therefore I'm not sure that the sub will continue to work once installed in the Normal.NewMacros.doc in an end user workstation. I need to know how to identify the Picture number of any picture in a Word doc. Is there a way? Can a Picture number be set as a variable? My sub goes out and grabs a jpg file in a fixed location and inserts it as a picture into the active document. Then it aligns the picture inside the frame of another picture already in the document.
Sub InsertPicture()
    
Application.ScreenUpdating = False
Dim Doc As Word.Document

Dim PicFile As String

Set Doc = ActiveDocument


PicFile = "\\domain-01.com\dfs$\care-one\users\care-one_nanthony\desktop\Smuckers\IMG.jpg"

Doc.Shapes.AddPicture _
FileName:=PicFile, _
LinkToFile:=False, _
SaveWithDocument:=True

    ActiveDocument.Shapes("Picture 24").Select
    Selection.ShapeRange.IncrementLeft 144#
    Selection.ShapeRange.IncrementTop 7.85

Application.ScreenUpdating = True

End Sub

Open in new window

Comment
Watch Question

GrahamSkanRetired
Top Expert 2012

Commented:
If you are very lucky, or you have arranged circumstances (e.g. very detailed instructions to your users), then the macro might do what you want.


This is what it will do:

1. If the image file is available, the picture will be placed at the beginning of the ActiveDocument. Otherwise an error will be raised.

2. It will be automatically named "Picture n" where 'n' is the new count of picture shapes in the document. (Note that this is true even if there is already a "Picture n").

3. If there are now one or more pictures named "Picture 24", one of them will be moved 144 points to the left and 7.85 points upwards.  Otherwise an error will be raised.







 
GrahamSkanRetired
Top Expert 2012

Commented:
The first problem to be addressed is to inform the code about where to put the picture.

If possible, I suggest that you post the template/document that is to receive the picture.

Author

Commented:
Isn't there a way for the code to move the inserted object to a fixed position on the page? The position of insertion seems to be the same each time, but isn't there a way to add the picture to a set of coordinates on the page? If not, is there a way to embed this position in the vba code via XML language?

Author

Commented:
Here's what ai do know. On an html page, objects can be positioned according to a set of 4 xy coordinates. If VBA is no help here, is there a way to leverage the XML nature of Word 2007 to build in similar coordiantes? If VBA can help, then how? I just want to position an inserted picture onto a one page document. The picture changes from time to time and the end user would only need to drop the jpeg file into the specified folder and run the macro to insert and then position the picture. There are 5 other objects on the single page: 3 graphics and 2  text boxes. The added picture is the 6th object and only one that varies. There is no text outside of the text boxes. The added picture must be centered in a black background portion of one of the graphic objects at the top of the page. The first text box is positiioned above this graphic with the background.

Author

Commented:
Also, is there anyway I can change the name of the inserted picture so that I have control of that reference?  Something like Selection.ShapeRange.Name = "Picture 1000"

???
Retired
Top Expert 2012
Commented:
You could capture the shape object when is is added and set its position relative to the page edges (X and Y, if you like).
Sub InsertPicture()
    
Application.ScreenUpdating = False
Dim Doc As Word.Document
Dim Sh As Shape
Dim PicFile As String

Set Doc = ActiveDocument


PicFile = "C:\Users\Public\Pictures\Sample Pictures\tulips.jpg"

Set Sh = Doc.Shapes.AddPicture(FileName:=PicFile, LinkToFile:=False, SaveWithDocument:=True)
Sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Sh.Left = CentimetersToPoints(3)
Sh.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Sh.Top = CentimetersToPoints(5)

Application.ScreenUpdating = True

End Sub

Open in new window

GrahamSkanRetired
Top Expert 2012

Commented:
Sorry, left my own test path in the code.

Author

Commented:
Thanks. This looks like what I've been looking for. I won't be able to test this until Monday, so please be patient.

Author

Commented:
Graham,

I had a chance to test your sub today, of course after having switched to my path and also after adjusting the centimeters to point metrics. Works! It avoids the necessity of naming the picture. Is that even possible? Why would VBA give an attribute like ("Picutre 24") and not allow the programmer to rename it to say ("Picture 25")? If you know the answer to this, please let me know. Thanks

Author

Commented:
Graham if you know the answer to how to rename inserted pictures, say from ("Picture 24") to ("Picture 25"), so that it can be selected after insertion, please let me know. Thanks. BTW, great solution to my problem.
GrahamSkanRetired
Top Expert 2012

Commented:
Well, you can rename shapes. If you have a set  of David Bailey photos (http://davidbaileyphotography.com/site/) you could do
Sub InsertPicture()
    Dim Doc As Word.Document
    Dim Sh As Shape
    Dim PicFile As String
    
    Application.ScreenUpdating = False
        Set Doc = ActiveDocument
        PicFile = "J:\My Pictures\Al\DSCF1882.jpg"
        Set Sh = Doc.Shapes.AddPicture(FileName:=PicFile, LinkToFile:=False, SaveWithDocument:=True)
        Sh.Name = "David Bailey Photo 1.jpg"
    Application.ScreenUpdating = True

End Sub

Open in new window