Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to code carriage return in a macro 4

Status
Not open for further replies.

sinbad99

Technical User
Feb 16, 2012
34
GB
In a Word macro for a header and footer, I want to have two lines on the header. I need in some way to declare char(10).

This is what I have, but the second line - the fullpoints as a dotted rule - doesn't appear.
Code:
 Sub HeaderFooterObjectBest_English()
  Dim MyText As String
    MyHeaderText = "..........................."
  MyHeaderText = "Paul | new words for the week | Best-English.org | 1 of 1"
  MyFooterText = "Best-English dot org"
  Selection.Font.Name = "Courier New"
      Selection.Font.Size = 9
      With ActiveDocument.Sections(1)
    .Headers(wdHeaderFooterPrimary).Range.Text = MyHeaderText
    .Footers(wdHeaderFooterPrimary).Range.Text = MyFooterText
  End With
End Sub

Be very grateful for some advice.
 
Try that ...

Dim MyText As String
MyHeaderText = "..........................." & vbCrLf & "Paul | new words for the week | Best-English.org | 1 of 1"

etc.

Kind regards
Mirko
--------------------------------------
>>>>>> ... I am sure, some supportissues are a matter of PEBKAC ... <<<<<
 
Code:
MyHeaderText = "..........................." & vbNewLine _
             & "Paul | new words for the week | Best-English.org | 1 of 1"


HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
Thanks very much, kermit01de and MazeWorX. That very nearly does the trick, only perhaps I was wrong to call it a carriage return. What I need is the space that comes with using shift and enter together.

I thought MazeWorX's 'vbNewLine' would do it, but that makes the gap too big, too.

Is there a way to say a non-paragraph line break, please?
 
Then try vbCr or vbLf instead

One of them should do what you expect

Kind regards
Mirko
--------------------------------------
>>>>>> ... I am sure, some supportissues are a matter of PEBKAC ... <<<<<
 
Thanks very much, Mirko, and yes, you'd think that one of the two should work, but strange to relate, no.

Could it be something on the page that is stopping the macro from obliging? But, no, because if I do an Enter-Shift non-paragraph line break in the header space, it works just as required.

So presumably, there must be some separate code for a non-paragraph line break???
 
Hmmm, what exactly do you expect as result?

For what should it be? - Just for me to understand better.

Kind regards
Mirko
--------------------------------------
>>>>>> ... I am sure, some supportissues are a matter of PEBKAC ... <<<<<
 
Shift+Enter is a vertical Tab - Chr(11).

 
or of course the VBA constant - vbVerticalTab.

Although I must say, WHY are you do it this way? Why are you making it a non-paragraph line break? Generally, this is a bad thing to do, and unless you have a really good reason to do so, should be avoided. So...what is your reason? Just curious.

 
First prize, fumei! And I've found the code - vbVerticalTab.

Thank you very much - and thanks, too, to Mirko and kermit01de and MazeWorX. Excellent, gentlemen, and I'm very grateful. Cheers paul
 
Hello fumei - Your code for the vertical tab arrived after I sent the previous message (though it appears here above it).

I want it because I like to have two lines in the header of some documents. I have the message and the page number of the number of pages. And then it looks good to have a rule of dots underscoring it.

I use this style in my resources for English language students, and for manuscripts. It is just for style - or what perhaps foolishly I take for style - fumei. Now instead of typing it each time, I can have a macro performing it.
Cheers paul
 
But if you want two lines why not HAVE two lines?? I do not understand why the non-paragraph break. The reason it is not a good idea is that if you ever want to use VBA to change the text it makes it much harder. If you want two lines then have two lines.

 
With two lines, Fumei, we have a considerable space between the lines. Of course - at least, I'm sure - one could code to reduce the space - but using 'vertical tab' means the lines are close together, which looks good.

So it is just a matter of aesthetics, though I note well your comments,and thank you for the warning. All the best paul
 
Why not just border the header and footer?
Code:
[blue]Dim MyHeaderText As String
Dim MyFooterText As String

MyHeaderText = "Paul | new words for the week | Best-English.org | 1 of 1"
MyFooterText = "Best-English dot org"

With ActiveDocument.Sections(1)
    With .Headers(wdHeaderFooterPrimary)
        .Range.Font.Name = "Courier"
        .Range.Font.Size = 9
        .Range.Text = MyHeaderText
        .Range.Borders(wdBorderBottom).LineStyle = wdLineStyleDot
    End With
    With .Footers(wdHeaderFooterPrimary)
        .Range.Font.Name = "Courier"
        .Range.Font.Size = 9
        .Range.Text = MyFooterText
        .Range.Borders(wdBorderTop).LineStyle = wdLineStyleDot
    End With
End With[/blue]
 
Nice one, strongm. Why not indeed? ASs my neighbours would say, There's posh for you. Many thanks indeed.
 
Also see regarding carriage return. There is a good example by TheAceman1 using Constants and SkipVought has some good insight regarding "Carriage return"

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
>a good example

I'm afraid I'd have to disagree. This may seem pedantic, but in that example there is frankly no need to (re)declare vbNewLine as NL. vbNewline is already a perfectly global constant, and has a more meaningful name, thus helping to document the code.
 
With two lines, Fumei, we have a considerable space between the lines. Of course - at least, I'm sure - one could code to reduce the space - but using 'vertical tab' means the lines are close together, which looks good.
In that case, and assuming the result is intended to remain in Word rather than in a text file, you should apply an appropriate Style to the header paragraphs. Your workaround is a kludge. If the result is being exported to a plain text file, the formatting in Word is of no consequence and you can safely use paragraph breaks.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top