Solved

Setting Header of the Word Document with vba code

Posted on 2011-02-15
3
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

628 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