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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CHR(13) does not work sometimes

Status
Not open for further replies.
Sep 16, 2009
15
CA
chr(13) (and "^p") in a VBA macro is sometimes skipped when the macro is run. The macro is intended to insert various predefined strings into a word doc.

Example:
Code:
istring(1) = "This is some text"
istring(2) = "This is more text that appears in a new paragraph"

Set objselection = wdAPP.Selection
With objselection
       .EndKey wdStory, wdMove
       .Font.Size = 10
       .TypeText Chr(13) & istring(1) & Chr(13) & Chr(13) & istring(2) & Chr(13)
End With

Does anyone know why this may be happening?

Here is a sample of the desired result:
Code:
This is some text

This is more text that appears in a new paragraph

Here is an example of when the chr(13) is skipped:
Code:
This is some textThis is more text that appears in a new paragraph
 
Typing Chr(13) does not insert a paragraph break - only a character that looks like one. To insert a paragraph break you should use vbCr or vbCrLf.

Cheers
Paul Edstein
[MS MVP - Word]
 
Er ... vbCR is CHR(13)

>Typing Chr(13) does not insert a paragraph break

Does for me. Has done for years.

As does Chr(10). And CHR(13) & CHR(10). (vbLf and vbCrLf)

However, .TypeText seems to do odd things with control characters on rare occassion (I suspect because it is playing around with the keyboard buffer). I prefer to use .TypeParagraph to reliably insert a paragraph break. Or, given that in this case we are collapsing a selction into an insertion point, we could just use .Text, which seems happier with control characters, instead of .TypeText
 
How about using
Code:
.TypeParagraph
to insert a paragraph marker?


“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Sorry folks, I was thinking more in terms of what happens when one uses when one inserts ^13 Chr(13) as the replacement text via Find/Replace. I saw the OP's reference to ^p and made the wrong association.

Cheers
Paul Edstein
[MS MVP - Word]
 
Argh! Should have read your post with more attention. [blush]
Anyhow, vbNewLine should do the trick too.
There are so many ways to make a cat meow. [tongue]

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top