Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA - placing a shape at the end of a header

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
GB
I need to place a shape at the end of a header (which usually but not always contains a table) using VBA. I can define objects for my shape and header and a range in which to place it, like this:

Code:
    ' The header where we want to place the shape
    Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
    
    ' The range is used as the anchor point for the shape
    Set rRange = oHeader.Range.Paragraphs.Last.Range
    
    ' Place the shape
    Set oShape = oHeader.Shapes.AddTextEffect(msoTextEffect2, tText, "Arial Black", 72#, _
        msoFalse, msoFalse, 163.25, 60.25, rRange)

However, the only way I can find of moving the shape to the end of the header is by moving the selection into the header then cutting and pasting like this:

Code:
    ' Move the shape to the end of the header, otherwise it won't be
    ' centred properly (Can't get the Range of this point for some reason)
    With Selection

        .HeaderFooter.Shapes(oShape.Name).Select
        .Cut
        .EndKey Unit:=wdStory
        .Paste

    End With

That's a horrible way to do it, for lots of reasons, not the least of which is that the clipboard will contain a copy of the shape.

Is there any way to do this properly? How do I find the range right at the end of the header? The code won't know in advance whether the header contains tables, text, or nothing.

Thanks in advance!

Nelviticus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top