Word 2003 VBA: Insert WordArt into each Header in multi section document
Posted on 2007-10-16
Please can someone tell me what I'm doing wrong here?
I am trying to insert WordArt into every header in a document. I have looked through the other questions on this site & tried to adapt the answers to ones that insert a text box or picture but, as soon as I change the code to add WordArt instead, it goes wrong.
Using the following code, I can insert new text into the primary header in each section of the document. However, if I try to add WordArt, the WordArt element is always inserted into the first page header. I know that I will have to make the final routine more complex than the one shown below (i.e. check for the type of header, whether linked to previous etc.) but I need to resolve this problem first.
The document I am working on has multiple sections and each section does not link to the previous one. At this stage, all the headers are simple ones - i.e. not different first page, not odd and even.
Here's the code:
Dim sec As Section
Dim Head As HeaderFooter
Dim WordArt As Shape
Dim rng As Range
For Each sec In ActiveDocument.Sections
Set Head = sec.Headers(wdHeaderFooterPrimary)
Set rng = Head.Range
Head.Range.Text = "Header text " & sec.Index 'this bit works correctly for each section
Set WordArt = Head.Shapes.AddTextEffect(msoTextEffect1, "Header text " & sec.Index, "Arial", 30, _
msoFalse, msoFalse, 12, 12)
I am trying to get the code to be quite 'tight' as I have a much more long-winded version that takes ages to run. That version physically opens each header, inserts the WordArt, goes back to the main document, physically goes to the next header etc, etc. I can't believe that's the smart way to do it.
All help very gratefully received. Thanks in advance.