?
Solved

Word 2003 VBA:  Insert WordArt into each Header in multi section document

Posted on 2007-10-16
7
Medium Priority
?
1,100 Views
Last Modified: 2008-01-09
Hi All

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:

Sub My_Test()
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
   
    rng.Collapse wdCollapseStart
   
    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)

Next

End Sub

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.

Monte
0
Comment
Question by:MonteDelMar
  • 3
  • 3
7 Comments
 
LVL 14

Assisted Solution

by:Glenn_Moore
Glenn_Moore earned 60 total points
ID: 20086814
What I would do was to Record a new macro doing your steps manually.  Tools, Macro, REcord a new Macro.
0
 

Author Comment

by:MonteDelMar
ID: 20086890
Hi Glenn
Many thanks for your response.  The solution I mentioned, the one that seems to be taking a long time to run, started out that way.  I was looking for something more 'streamlined', if it's possible.  Also, I'm curious as to why the code I included in my question works for text, picture & text boxes but not for WordArt.  
Thanks
Monte


0
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 315 total points
ID: 20087403
I doubt that a recorded macro will capture the fact that you want to insert to the primary header only.
It looks like bug to me.
As a get-round, can I suggest that you convert the floating graphic to inline, cut and paste it to where you want and the convert it back to floating.

Sub My_Test()
Dim sec As Section
Dim Head As HeaderFooter
Dim WordArt As Shape
Dim WordArtILSH As InlineShape

Dim rng As Range

For Each sec In ActiveDocument.Sections
   
    Set Head = sec.Headers(wdHeaderFooterPrimary)
       
    Set rng = Head.Range
   
    rng.Collapse wdCollapseStart
   
    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, rng)
   
    Set WordArtILSH = WordArt.ConvertToInlineShape
    WordArtILSH.Range.Cut
    rng.Paste
    Set WordArtILSH = Head.Range.InlineShapes(1)
    WordArtILSH.ConvertToShape
Next sec

End Sub
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 

Author Comment

by:MonteDelMar
ID: 20093661
Hi Graham

Thank you so much for your response.  I had some problems with the ConvertToShape part but, following your other pointers, have achieved what I wanted without needing to convert the graphic.  Unfortunately I do have to select the header but it's way better than what I had before.  Thanks too for suggesting the problem I was experiencing might be down to a bug - I thought I was missing something very obvious.  I attach the working code just in case it should be of interest to anyone else.

Many thanks again, I certainly would have got there on my own.

Monte

Code:

Sub My_Test()
Dim sec As Section
Dim Head As HeaderFooter
Dim WordArt As Shape
Dim rng As Range

For Each sec In ActiveDocument.Sections  

    'there would be additional code here to check the type of header i.e. different first page etc.
    Set Head = sec.Headers(wdHeaderFooterPrimary)

    Set rng = Head.Range
    rng.Collapse wdCollapseStart

    'select the header
    rng.Select
   
   'create the shape  
   Set WordArt = Head.Shapes.AddTextEffect(msoTextEffect1, "My Text", "Arial", 1, _
    msoFalse, msoFalse, 0, Rng)

    'Give the shape a name
    'strType is a single letter used to identify the type of header and therefore to ensure an exclusive
    'name for the shape  
    WordArt.name = "MyShape" & Sec.index & strType

   'select the shape
    WordArt.Select

    'cut it
    Selection.Cut

    'paste it into the correct range
    rng.Paste

   'get the WordArt object again & udentify it by name
   Set WordArt = Head.Shapes("MyShape" & Sec.index & strType)

    With WordArt
        'apply any required attributes
    End With

Set Sec = Nothing
Set Head = Nothing
Set WordArt = Nothing
Set rng = Nothing

End Sub
0
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 20094397
I did wonder if selecting would work, but I had already got it to work without doing that.

Anyway thank you so much for the feedback. That way we all learn.
0
 

Author Comment

by:MonteDelMar
ID: 20095119
Oh Graham, I just re-read what I wrote & I meant to say that I would certainly *not* have got there on my own!  How ungrateful I must have sounded.  Am very red in the face now.

Thanks
Monte
0
 
LVL 77

Expert Comment

by:GrahamSkan
ID: 20095492
Don't worry about it. It makes a rather strange sentence if read literally, so I must have unconsciously inserted the missing word & wouldn't have noticed if you hadn't pointed it out.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Suggested Courses

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question