View Full Version : Solved: How to paste pictures without staggering?
Gavint
08-03-2007, 12:20 AM
I'm using VBA to paste a letterhead picture into the header of multiple sections within a document. However, when consecutive pictures are pasted in the seperate sections, Word staggers their placement slightly. I need the picture placement to be precise. Anyone know how to get around this?
More detail:
You can see what I mean by copying then pasting the same pic in a document several times. Rather than pasting in the exact same spot, Word staggers each consecutuve paste.
As there are multiple sections in my documents, a single paste into one header is not sufficient. So I'm calling a paste function several times. See below (where sec = current section number)
currentDoc1.Sections.Item(sec).Headers(wdHeaderFooterFirstPage).Range.Paste
Even though the consecutive pastes are in completely seperate sections, Word see's the picture as the same and staggers them. Letterhead one looks great, but letterhead two onward are out of alignment.
I need a way to either:
1. Force the paste into the same relative location on every section
2. Somehow identify the picture as it's placed and then move it to the correct location
Thanks.
fionabolt
08-03-2007, 01:07 AM
How about something along these lines:
Dim oj As shape 'Object which is the Shape to use
dim ix as long, iy as long 'values for the positioning
dim YP as long, XP as long
dim PX as long, PY as long
ix = 1 'a figure for the width
iy = 2 'a figure for the height
YP = 1 'a figure for the vertical position
XP = 2 'a figure for the horizontal position
PX = 1 'a figure for the left position
PY = 2 'a figure for the top position
Application.ScreenUpdating = False
Set oj = ActiveDocument.Shapes.AddPicture _
(FileName:="filename", linktofile:=False, _
savewithdocument:=True) 'Add text textbox
oj.Name = "nameof the object" 'Set the textbox name
Rem --Set shape values
oj.LockAspectRatio = False 'Don't bother with aspect ratio
oj.Width = ix 'Set the width
oj.Height = iy 'Set the height
oj.RelativeVerticalPosition = YP 'Positioned here
oj.RelativeHorizontalPosition = XP 'Positioned here
oj.Line.Visible = False 'No line visible
oj.Fill.Visible = False 'Completely transparent
oj.TextFrame.MarginLeft = 0 'Internal left margin
oj.TextFrame.MarginRight = 0 'Internal right margin
oj.TextFrame.MarginTop = 0 'Internal top margin
oj.TextFrame.MarginBottom = 0 'Internal bottom margin
oj.WrapFormat.Type = 'Set wrapping
oj.WrapFormat.DistanceLeft = 0 'External left margin
oj.WrapFormat.DistanceRight = 0 'External right margin
oj.WrapFormat.DistanceTop = 0 'External top margin
oj.WrapFormat.DistanceBottom = 0 'External bottom margin
oj.Left = PX 'Set the X-position
oj.Top = PY 'Set the Y-position
Application.ScreenUpdating = False
Gavint
08-03-2007, 03:54 PM
The specific problem I'm having is identifying the object as it is pasted.
Word will generate a picture number (which could be anything), which I need to be able to define to use in the position of the picture.
I see in you code where you "set obj =", but this seems to be with reference to a known filename. Can you highlight where it can identify the specific picture from a paste?
I can use this code, if picture number was known
doc1.Shapes("Picture " & PICTURE_NUMBER).Select
Selection.ShapeRange.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = CentimetersToPoints(HEADER_LOCATION_RIGHT)
Selection.ShapeRange.Top = CentimetersToPoints(HEADER_LOCATION_BELOW)
fionabolt
08-06-2007, 12:23 AM
Gavin
Using VBA to paste would be a slow process. The example code I have supplied would be much more efficient.
The first line adds the picture from a known location "filename", the second line applies a name to it.
Set oj = ActiveDocument.Shapes.AddPicture _
(FileName:="filename", linktofile:=False, _
savewithdocument:=True) 'Add text textbox
oj.Name = "nameof the object" 'Set the textbox name
Any code following these two lines can then be applied to this named shape.
Regards,
F
fumei
08-06-2007, 03:30 AM
Could you explain why you are pasting at all????
Where is it copied from? More importantly...WHY are you doing this? What is the reason?
Why are these not in the header anyway? Why is this not in a template?
Why are you using Selection?
You can put content directly into headers without ever using Selection, or View.
Gavint
08-26-2007, 11:22 PM
Still working on this one, so I'll outline with more detail!
As mentioned in my original post, there are many sections in the document. Some sections get the letterhead graphic (eg letter to client), some don't (eg fax cover page). The number of sections varies between documents.
I have a macro which moves through each section in the document calling various functions as it goes (eg set the margin function, insert the letter head logo function) and so on.
The "add picture" code mentioned above works great, except that every picture is placed on the first page of the document (even if a particular section is selected before hand). Hence the moving (cut/paste) of the graphic to the correct section.
So, the question still remains; is there a way to move (cut/paste?) a picture to a specific location without the staggering problem. Alternatively, is there a method to insert a picture at a specific section and at an exact location on the page?
Gavint
08-27-2007, 12:06 AM
Found a solution, thanks anyway.
Here's the crux of the code that will place a picture anywhere and any size on your document
Dim logo As Shape, rng As Range
' Set range for first insertion
Set rng = Selection.Range.GoTo(wdGoToSection, wdGoToAbsolute, 2)
' Add shape
Set logo = ActiveDocument.Shapes.AddPicture _
("c:\my pictures\logo.jpg", False, True, , , , , rng)
With logo
.LockAnchor = True
.WrapFormat.AllowOverlap = False
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = wdShapeCenter ' horizontal position
.Top = CentimetersToPoints(0.5) ' vertical position
.LockAspectRatio = msoTrue
.Height = "55" 'pixels
End With
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.