VBA to apply a Soft Return (or Hard Return) at end of wrapped line

D

Dennis

Office 2003
Word is a wonderful word processer. Visio, expecially, text-box
editing is cryptic at best.

Goal -> 1) Type information (For the Visio text Box) in Word
2) Manually ascertain the approx line length that Visio
will handle in that particular text box.
3) Set margins manually in Word to wrap at #2's width
4) VBA-process the word document to place a soft return at
end of the wrapped line (should this be a hard return?)
Visio seems to handle a soft Return while text editing.
(Should I process to file as a word or as a text file?
5) Copy/Paste the processed file into Visio.

Would someone please supply the VBA code for #4 or other advice to help
solve my challenge?

Dennis
 
K

Klaus Linke

Hi Dennis,

You can try the code below. Not very sophisticated, but it should hopefully insert manual line breaks at the end of all wrapped lines in the selected text.

If it turns out that Visio can't deal with the manual line breaks, try Chr(10) or Chr(13) instead of Chr(11).

Dim myRange As Range
Set myRange = Selection.Range.Duplicate
Selection.Collapse (wdCollapseStart)
While Selection.End < myRange.End
Selection.EndKey Unit:=wdLine
If Selection.Text <> vbCr Then
Selection.InsertAfter Chr(11)
End If
Selection.Collapse (wdCollapseEnd)
Selection.Move Unit:=wdCharacter, Count:=1
Wend

Abother option would be to save as "Text with layout", and re-open that text file. But that export filter has been removed in Word2003... you'll only have it if you have upgraded from an older version.

Regards,
Klaus
 
D

Dennis

Thanks Klaus,

I will try it very soon.

Please check back if there any questions?

Dennis


Klaus said:
Hi Dennis,

You can try the code below. Not very sophisticated, but it should
hopefully insert manual line breaks at the end of all wrapped lines in
the selected text.
If it turns out that Visio can't deal with the manual line breaks,
try Chr(10) or Chr(13) instead of Chr(11).
Dim myRange As Range
Set myRange = Selection.Range.Duplicate
Selection.Collapse (wdCollapseStart)
While Selection.End < myRange.End
Selection.EndKey Unit:=wdLine
If Selection.Text <> vbCr Then
Selection.InsertAfter Chr(11)
End If
Selection.Collapse (wdCollapseEnd)
Selection.Move Unit:=wdCharacter, Count:=1
Wend

Abother option would be to save as "Text with layout", and re-open
that text file. But that export filter has been removed in Word2003...
you'll only have it if you have upgraded from an older version.
 
G

Greg

Klaus,

If I can make a suggestions to dress the code up a bit. As is, if the
user selects the final paragraph mark a continous loop occurs. I added
the following lines to prevent this:

Dim i As Integer
i = Selection.StoryType
Set myRange = Selection.Range.Duplicate
If myRange.End = ActiveDocument.StoryRanges(i).End Then
myRange.MoveEnd wdCharacter, -1
End If
 
D

Dennis

Thanks Greg!

I was just going to ask about that issue.

Being a Word VBA novice, I will place the first three lines above the
loop and the remainder right before the Wend?

Dennis
 
D

Dennis

Klaus,

Excellent !! especially with Greg's help.

How could I modify the code to REMOVE any or all (in selected text):
Chr(10) and/or
Chr(11) and/or
Chr(13)

Of course I would use the revised code in a separate macro.

Dennis
 
K

Klaus Linke

You can use the macro recorder, and record a replacement
Find what: ^10
Replace with:
("Search: Down", leave "Replace with" empty to just remove the Chr(10), or type a space if you want/need to insert a space)
Then click on "Replace all" and stop the macro recorder.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top