PDA

View Full Version : Macro to insert image



derekmhart
02-11-2015, 08:24 PM
I have an image I would like to insert at the top of every page and the bottom of every page. Not in the header/footer. Just in the same exact position at the top and bottom. If I record a macro, I cannot seem to select the object and move it with arrows. Much appreciated if somebody could give a small sample of a macro that inserts an image at a specific location, and perhaps how to grab that inserted image somehow in vba and move it and size it properly.

gmayor
02-12-2015, 02:56 AM
If you were going to insert the image with vba, you wouldn't need to select it and move it. You would just place it where you require it. The more pressing matter relates to the placement as there are no 'pages' as such in a Word document, a page is a volatile entity formed by formatting text flowed between the current margins. For this reason when you want a graphic (or anything else for that matter) to be repeated on each 'page' it is best located in the header/footer. Why is this not acceptable?

However the following might work for you. Enter the path and filename of the image you want to insert, and the height and width (here shown in centimeters)


Sub InsertImage()
Dim oShape As Shape
Dim oRng As Range
Dim iPageCount As Integer
Dim i As Long
Set oRng = ActiveDocument.Range
oRng.Collapse 0
iPageCount = oRng.Information(wdActiveEndPageNumber)
For i = 1 To iPageCount
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, name:=i
Set oShape = ActiveDocument.Shapes.AddPicture(Filename:="C:\Path\Filename.ext")
With oShape
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.TopRelative = 0
.LeftRelative = 0
.Width = CentimetersToPoints(3.28)
.Height = CentimetersToPoints(3.28)
.WrapFormat.Type = 0
End With
Next i
lbl_Exit:
Exit Sub
End Sub

derekmhart
02-12-2015, 12:40 PM
Thank you so much for answering. I am trying to alter this in a specific way with the following code. The first part works great, where I can place the image on the TOP of every page, except that it pushes down all the text on the pages, and messes up the document. Is there a way to place this without messing up the document in that way? Just place the image there, without affecting the layout? I guess the problem is that I couldn't figure out how to do that manually either. Maybe code is the same issue?!?

I also want to place the image, the same exact size, but on the bottom of the page. How can I place this on the bottom of the same page? Am I approaching it correctly. Is there something else that has to happen to set the range again? But if the above item cannot be solved, then this one can't work either.


Sub InsertImage()
Dim oShape As Shape
Dim oShape2 As Shape
Dim oRng As Range
Dim iPageCount As Integer
Dim i As Long
Set oRng = ActiveDocument.Range
oRng.Collapse 0
iPageCount = oRng.Information(wdActiveEndPageNumber)
For i = 1 To iPageCount
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=I
Set oShape = ActiveDocument.Shapes.AddPicture(FileName:="C:\Users\Derek\Desktop\Blue Line.png")
With oShape
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.TopRelative = 0
.LeftRelative = 0
.Width = CentimetersToPoints(21)
.Height = CentimetersToPoints(0.75)
.WrapFormat.Type = 0
End With

Set oShape2 = ActiveDocument.Shapes.AddPicture(FileName:="C:\Users\Derek\Desktop\Blue Line.png")
With oShape2
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.TopRelative = 25
.LeftRelative = 0
.Width = CentimetersToPoints(21)
.Height = CentimetersToPoints(0.75)
.WrapFormat.Type = 0
End With
Next i
lbl_Exit:
Exit Sub
End Sub

gmaxey
02-12-2015, 03:26 PM
Change the wrap type. You don't need two Shape variables

Sub InsertImage()
Dim oShape As Shape
Dim oRng As Range
Dim iPageCount As Integer
Dim i As Long
Set oRng = ActiveDocument.Range
oRng.Collapse 0
iPageCount = oRng.Information(wdActiveEndPageNumber)
For i = 1 To iPageCount
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=i
Set oShape = ActiveDocument.Shapes.AddPicture(FileName:="D:\PicName.jpg")
With oShape
.Width = CentimetersToPoints(21)
.Height = CentimetersToPoints(0.75)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = 0
.Left = 0
.WrapFormat.Type = 5
End With
Set oShape = ActiveDocument.Shapes.AddPicture(FileName:="D:\PicName.jpg")
With oShape
.Width = CentimetersToPoints(21)
.Height = CentimetersToPoints(0.75)
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = ActiveDocument.PageSetup.PageHeight - .Height
.Left = 0
.WrapFormat.Type = 5
End With

Next i
lbl_Exit:
Exit Sub
End Sub

derekmhart
02-12-2015, 06:00 PM
OK this is working really well! Except the PageHeight gives this value. So it is out of range. How can I properly detect the PageHeight?

ActiveDocument.PageSetup.PageHeight = 9999999

gmaxey
02-13-2015, 07:23 AM
Well that is odd. .PageHeight should return 792 for a standard U.S. letter size page.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
With ActiveDocument.PageSetup
.PageHeight = InchesToPoints(11)
.PageWidth = InchesToPoints(8.5)
MsgBox .PageHeight
End With
End Sub