Solved

Setting Header of the Word Document with vba code

Posted on 2011-02-15
3
352 Views
Last Modified: 2012-05-11
Dear all,

I wish to design a macro for setting the header of the document with the code below but the result ended up with all 3 lines on the left. Can any help to resolve the problem?

Line 1 on the Right
Line 2 on the Left
Line 3 on the Left

Thanks very much

   

With ActiveDocument.sections(1).Headers(wdHeaderFooterPrimary).Range
    
        .Text = "Line 1 on the Right" & Chr(10)
        .Paragraphs.Alignment = wdAlignParagraphRight
    
        .InsertAfter "Line 2 on the Left" & Chr(10)
        .Paragraphs.Alignment = wdAlignParagraphLeft
    
        .InsertAfter "Line 2 on the Left"
        .Paragraphs.Alignment = wdAlignParagraphLeft
    
    
    End With

Open in new window

0
Comment
Question by:Lawlords
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34897442
Try as below ... setting the cursor after the text inserted at the rignt.

Chris
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    
    .Paragraphs.Alignment = wdAlignParagraphRight
    .Text = "Line 1 on the Right" & Chr(10)
    .Collapse wdCollapseEnd

    .Paragraphs.Alignment = wdAlignParagraphLeft
    .InsertAfter "Line 2 on the Left" & Chr(10)
    .InsertAfter "Line 2 on the Left"
    
    
End With

Open in new window

0
 

Author Closing Comment

by:Lawlords
ID: 34897468
Thanks very much Chris, you gave me a great help.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34897541
Word can be very finicky with the methods used for populating text so you are not alone, (I struggle too).

Thank you for the prompt closure and grade.

Chris
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

790 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