Solved

Setting Header of the Word Document with vba code

Posted on 2011-02-15
3
344 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Outlook Free & Paid Tools
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now