Inserting Text from another file

L

LA Lawyer

I am using VBA in Word 2007 to incorporate text onto a blank document from
another document, i.e., a letterhead.

This is my code:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=
_
"INCLUDETEXT ""n:\\wordforms\\letterhead.docx"" ",
PreserveFormatting:= _
True

This works OK except that the incorporated material get reformatting by
becoming double spaced instead of single spaced. I have tried changing the
default paragraph to single lines. That didn't work. I also tried changing
the PreserveFormatting between True and False. That didn't work either. I
also tried playing with the switches but none of them seemed to help.

What I am doing wrong? What is the fix?
 
D

Doug Robbins - Word MVP

It would be better to create a template with your letter head in it and then
use File>New and select that template when you want to create the "blank
document" that you mention.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Graham Mayor

You probably need a charformat switch

Selection.Fields.Add Range:=Selection.Range, _
Type:=wdFieldIncludeText, _
Text:="""n:\\wordforms\\letterhead.docx"" \*charformat", _
PreserveFormatting:=False

however as Doug says, the best way to deal with this is to save your
letterhead document as a template and use it to create new letters.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

If you format your letterhead with a style in letterhead.docx, there is no
need for a template, and the following macro will insert the text of
letterhead.docx and format it with the style in the original doc.

Sub AddLetterhead()

Const Error_FileNotFound = 5174
Dim fileName As String
Dim pos1 As Integer
Dim textRange As Range

' Save current position.
pos1 = Selection.Start
On Error Resume Next
Selection.InsertFile fileName:="C:\\wordforms\\letterhead.docx", _
ConfirmConversions:=False
If Err.Number = Error_FileNotFound Then
MsgBox "The file specified was not found.", vbOKOnly, "File Not Found"
Exit Sub
End If
On Error GoTo 0
Set textRange = ActiveDocument.Range(Start:=pos1, End:=Selection.Start -
1)
textRange.Style = ActiveDocument.Styles("Letterhead")
End Sub

If your letterhead has parts with different styles, you'll need to adjust
this macro to handle them.
 

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