vbams-wordanchorword-2010

Editing anchor position in header of Microsoft Word using vba


I am working on a VBA macro, which I wrote only parts of it myself, for MS-Word that changes the orientation of the page and then copies the header and footer of previous pages to the new page and some other stuff:

Selection.PageSetup.Orientation = wdOrientLandscape
ActiveDocument.Sections.Last.PageSetup.DifferentFirstPageHeaderFooter = False

ActiveDocument.Sections(ActiveDocument.Sections.Last.index - 1).Headers(wdHeaderFooterPrimary).Range.Select
Selection.Copy

ActiveDocument.Sections.Last.Headers(wdHeaderFooterPrimary).Range.Select
Selection.Paste

ActiveDocument.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).LinkToPrevious = False

formatHeader wdHeaderFooterPrimary
formatHeader wdHeaderFooterFirstPage

There is a text in a TextBox which is anchored to header. What I want to do now is to change its position on the pages with "Landscape" orientation.

How can I change the layout options (see images below)? I haven't been able to find any information.

This is how my document looks like after changing the page orientation to "Landscape":

As you see, the paragraph on the side, in the TextBox is not in the middle. so I want to move it a bit higher. You can also see the anchor in this image.

This is how I did it in Word as a user:

enter image description here enter image description here


Solution

  • The key is to set where the measurement should be taken from (RelativeHorizontalPosition) and then use the Shape's Left setting. Relative to pretty much anything except wdCharacter the horizontal position will be static on the page when the text is edited; vertically, wdLine and wdParagraph would be equivalent to using "Move with text".

    I've streamlined the code you posted somewhat by declaring and using objects for the last Section and the Shape.

    Instead of copying and pasting, this code uses Range.FormattedText to copy-transfer the content from one header to another. This is preferable to using the Clipboard for those situations where it works (between any two Word Ranges).

    Dim secLast as Word.Section
    Dim shp as Word.Shape
    
    Set secLast = ActiveDocument.Sections.Last
    secLast.PageSetup.Orientation = wdOrientLandscape
    secLast.PageSetup.DifferentFirstPageHeaderFooter = False
    secLast.Headers(wdHeaderFooterPrimary).Range.FormattedText = _
      ActiveDocument.Sections(secLast.index - 1).Headers(wdHeaderFooterPrimary).Range.FormattedText
    
    secLast.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    secLast.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    
    formatHeader wdHeaderFooterPrimary
    formatHeader wdHeaderFooterFirstPage
    
    Set shp = secLast.Range.Shapes(1)
    shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage 
    shp.Left = 33 ' Or you can use, for example CentimetersToPoints(1.4)
    shp.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    shp.Top = CentimetersToPoints(14)