How to single space a document in code?

H

Hollis D. Paul

I am dropping the body of an Outlook message into a Word Document using
the following code:

MyDoc.Content.InsertAfter o_item.Body

The proper text gets there, but it is double-spaced. Is there a
property I can set to get text to be formatted as single space?

Or, would it be easier to set this in the template document which I
invoke, and how would I do that? I couldn't find anything near the top
level of the Format menu that would let me set a single-space style?

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Helmut Weber

Hi Hollis,

this one sets all paragraphs of the main story to wdLineSpaceSingle
' wdLineSpaceSingle = 0
' only to keep code lines shorter, just a matter of taste
ActiveDocument.Range.ParagraphFormat.LineSpacingRule = 0

If it should be applied only to the inserted text,
you could use something like this:

Dim oRng As Range
Set oRng = Selection.Range
oRng.start = ActiveDocument.Range.End
' some sample text, here it is paragraph(1)
ActiveDocument.Range.InsertAfter ActiveDocument.Paragraphs(1).Range
oRng.End = ActiveDocument.Range.End
With oRng.ParagraphFormat
.LineSpacingRule = wdLineSpaceSingle
End With

Unless, of course, the seemingly double spacing results from double
paragraph marks.

Have a nice day.
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Hollis D. Paul

Helmut Weber said:
Unless, of course, the seemingly double spacing results from double
paragraph marks.
That may be the problem. I did manage to open up the template, and the
default setting there is to single spacing. I will try this and see if
it makes a difference.

Thanks for the suggestion and the code.

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 
H

Helmut Weber

Hi Hollis,
this is a sample, how to get rid of empty paragraphs
in the range of the inserted text:

Sub test555()
Dim rTmp1 As Range ' the text to be inserted
Dim rTmp2 As Range ' the range of the inserted text
Dim oPrg As Paragraph
Set rTmp1 = Selection.Range
Set rTmp2 = Selection.Range
rTmp1.start = 0
rTmp1.End = ActiveDocument.Paragraphs(8).Range.End
' rTmp1 contains some empty paragraphs
rTmp2.start = ActiveDocument.Range.End
' some sample text, here it is paragraph(1)
ActiveDocument.Range.InsertAfter rTmp1
rTmp2.End = ActiveDocument.Range.End
For Each oPrg In rTmp2.Paragraphs
If oPrg.Range.Text = vbCr Then oPrg.Range.Delete
Next
End Sub

Just replacing two paragraph marks by one,
doesn't work that well under certain circumstances.
You may have to try vbCrLf in addition to vbCr.

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Helmut Weber

Oops,
' some sample text, here it is paragraph(1)
only a remainder from the first posting.

Greetings from Bavaria, Germany
Helmut Weber, MVP Word VBA
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Hollis D. Paul

Helmut Weber said:
Unless, of course, the seemingly double spacing results from double
paragraph marks.
I discovered that the double spacing was being generated because the
default format of the Outlook message was HTML. I changed it to Plain
Text and the extra line-feeds went away.

Isn't computing wonderful?

Hollis D. Paul [MVP - Outlook]
(e-mail address removed)
Using Virtual Access, Windows 2000 build 2600
http://search.support.microsoft.com/kb/c.asp?FR=0&SD=TECH&LN=EN-US

Mukilteo, WA USA
 

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