Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-04-2014, 06:18 AM
Smallweed Smallweed is offline Position shapes correctly in Word 2010 with VBA- tip Windows 7 64bit Position shapes correctly in Word 2010 with VBA- tip Office 2010 32bit
Novice
Position shapes correctly in Word 2010 with VBA- tip
 
Join Date: Aug 2013
Posts: 13
Smallweed is on a distinguished road
Default Position shapes correctly in Word 2010 with VBA- tip


Useful tip I hope! I've been converting some 2003 templates to 2010 and have wasted days trying to get my old VBA to correctly position logos in the header on the front page. It turns out the problem is that Word 2010 VBA has a bug that means when adding a shape it will always anchor to the page even if you specify otherwise (i.e. it ignores the fact you're trying to anchor to column, margin, etc). I finally found a posting (elsewhere) that has solved the problem. The solution is to insert as an inline shape and then convert it to a shape (sample code below).

Note that this bug only seems to affect the first page of a document. Also, 2003 documents opened in compatibility mode are not affected by the anchor bug and continue to work correctly even if saved as 2010 format so I've had to come up with two parallel solutions for existing and for new documents (just to add to the fun!).

I expect someone will come back and say I should ALWAYS use inline shapes instead of shapes unless I want to use Word's text flow/desktop publishing options - and you'd be right except the problem I have is that, to be positioned correctly, the logos have to sit in the margin outside of any existing text.
Code:
Set inlshpCompanyLogo = ActiveDocument.InlineShapes.AddPicture _
(FileName:=strLogoName, _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range)
Set shpCompanyLogo = inlshpCompanyLogo.ConvertToShape
With shpCompanyLogo
  .LockAnchor = True
  .Left = CentimetersToPoints(snglLeftOffset)
  .Top = CentimetersToPoints(snglTopOffset)
  etc...
End With

Last edited by macropod; 02-04-2014 at 06:59 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 02-04-2014, 07:12 PM
macropod's Avatar
macropod macropod is offline Position shapes correctly in Word 2010 with VBA- tip Windows 7 32bit Position shapes correctly in Word 2010 with VBA- tip Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The following works for me:
Code:
Dim Rng As Range
With ActiveDocument
  Set Rng = .Sections.First.Headers(wdHeaderFooterPrimary).Range.Characters.First
  Set shpCompanyLogo = .Shapes.AddPicture(FileName:=strLogoName, LinkToFile:=False, SaveWithDocument:=True, _
    Anchor:=Rng, Left:=CentimetersToPoints(snglLeftOffset), Top:=CentimetersToPoints(snglTopOffset))
End With
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
picture shapes in word 2010 sgrund Word 5 01-30-2014 11:45 PM
Position shapes correctly in Word 2010 with VBA- tip PPT 2010 automatically rotating shapes pptdiva PowerPoint 1 10-01-2013 05:14 AM
Position shapes correctly in Word 2010 with VBA- tip Word 2010 Shapes namedujour Word 0 08-16-2012 12:57 PM
Position shapes correctly in Word 2010 with VBA- tip word 2010 not display math symbols correctly sharks Word 1 09-08-2011 04:24 AM
Paste not working correctly Word 2010 wapanap Word 0 09-13-2010 06:29 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft