INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...Over the past year I have found your site to be EXCELLENT. Never have I been able to find so many answers to such vast problems and it is an excellent service..."
Geography
Where in the world do Tek-Tips members come from?
|
How to code carriage return in a macro (4)
|
|
|
sinbad99 (TechnicalUser) |
25 Feb 12 13:14 |
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 ... <<<<< |
|
MazeWorX (IS/IT--Management) |
25 Feb 12 15:51 |
CODEMyHeaderText = "..........................." & 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>> |
|
|
sinbad99 (TechnicalUser) |
25 Feb 12 16:11 |
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 ... <<<<< |
|
|
sinbad99 (TechnicalUser) |
25 Feb 12 16:25 |
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 ... <<<<< |
|
fumei (TechnicalUser) |
25 Feb 12 17:05 |
Shift+Enter is a vertical Tab - Chr(11). |
|
|
fumei (TechnicalUser) |
25 Feb 12 17:12 |
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. |
|
|
sinbad99 (TechnicalUser) |
25 Feb 12 17:14 |
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 |
|
|
sinbad99 (TechnicalUser) |
25 Feb 12 17:20 |
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 |
|
|
fumei (TechnicalUser) |
25 Feb 12 21:19 |
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? CODEDim 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 |
|
|
sinbad99 (TechnicalUser) |
26 Feb 12 12:19 |
Nice one, strongm. Why not indeed? ASs my neighbours would say, There's posh for you. Many thanks indeed. |
|
|
MazeWorX (IS/IT--Management) |
26 Feb 12 13:49 |
Also see http://www.tek-tips.com/viewthread.cfm?qid=1675726 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>> |
|
|
sinbad99 (TechnicalUser) |
26 Feb 12 14:03 |
Many thanks, MazeWorkX, will do. |
|
>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. |
|
|
macropod (TechnicalUser) |
26 Feb 12 16:08 |
Quote: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] |
|
|
fumei (TechnicalUser) |
27 Feb 12 21:41 |
RE: macropod's post. Totally agree. If the issue is format (spacing) then proper use of style is the ONLY way to go. Besides, you should be using styles anyway. |
|
|
MazeWorX (IS/IT--Management) |
28 Feb 12 3:16 |
Quote:ONLY way to go
sorry but i have to jump in here. Seriously the 'only way', how many times can you honestly say there is only one way of doing something in the office suite with vba. That's what makes this suite so powerful its ability to accomplish a task in several ways playing on the strengths of the developer. Also re strongm combining several lines of code or built in functions to either enhance or lessen the lines of code required is and will always be a good idea. Granted the first line isn't necessary and it could be accomplished with one line of code. Regardless it gives the op the idea that there are options available ... Choices ... Not just one way of accomplish a task in Office HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>> |
|
>its ability to accomplish a task in several ways playing on the strengths of the developer
I'm sure that you must have seen more badly written or unneccessary Office VBA code than I have that adheres to this idea, rather than to the idea that the developer could, alternatively, play to the strengths of the software.
It wasn't all that long ago that I was asked to help someone out with some VBA that was supposed to identify section headings on some imported plaintext by finding single words alone on a line (and then discovering, amongst other issues, that not all such headings were only one word), and then making them bold and increasing the font size for each one it found. Do you think that was a good choice? Given that this was essentially a poorly thought out version of Range.AutoFormat
Having a choice is all very well. Making the right choice can be vital, and save a lot of time and pain now and in the future. And I'd hope that here in tek-tips we'd try and point people towards the good choices. And yes there will always be some argument and discussion over what the good choices are - and that's a good thing in itself.
And on that basis, I'd argue that the example we are talking about offers no more than your own example in this thread (showing how to concatenate strings with a new line), whilst at the same time introducing a poor programming practice (elimination of a meaningful variable name). To me, that means it is not a good example. |
|
|
fumei (TechnicalUser) |
28 Feb 12 21:07 |
If by meaningful variable name you mean a style name, then for sure. By stating only, I meant that if you want to identify and action paragraphs (be it spacing, font, font size or any other attribute) - and make no mistake these "lines" ARE paragraphs - then I stand by my statement. The only way to go is to use styles. Word is designed around styles. Period. It is the core of Word. To state that because there are multiple ways of doing something in Word makes those ways equivilent is absurd. For example to state that using Selection (rather than Range) to do things makes using Selection equivalent to using Range is nonsense. Note that, sure, making general comments about Office, is one thing, but we are talking about specifically Word. And in Word using styles is by far the better way of dealing with things like paragraph attributes. And even MORE so if you are using VBA. |
|
|
fumei (TechnicalUser) |
28 Feb 12 21:30 |
Actually, one thing that makes using styles properly superior is that is that if styles were used properly...in this case there would be no need for VBA at all. |
|
Just in case you thought otherwise, my comment was not aimed at you, Gerry |
|
|
fumei (TechnicalUser) |
1 Mar 12 16:50 |
|
|
 |
|