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

insert text into Word document header

Status
Not open for further replies.

EDB2

MIS
Sep 11, 2002
36
US
I'm creating a word document with my VB6 program and I want to insert multiple lines of text into the header area of the document. I can't figure out what property/properties I need to reference. Can anyone help me out? I found some documentation on the HeaderFooter object but it's pretty brief and confused me more than it helped me
 
Try this... it should be easy for u. Have fun!

Sub Create_Word_Doc()
Dim oWordApp As Word.Application

'Start a new instance of Microsoft Word
Set oWordApp = New Word.Application

With oWordApp

'Create a new document
.Documents.Add

'Create header and footer
.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Header Text" & vbCrLf & "Hello World"
.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "Footer Text"

'Add text to the document
.Selection.TypeText Text:="one"
.Selection.TypeParagraph
.Selection.TypeText Text:="two"
.Selection.TypeParagraph
.Selection.TypeText Text:="three"

'Save the document
.ActiveDocument.SaveAs FileName:="c:\Doc1.doc", _
FileFormat:=wdFormatDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

End With

'Quit Word
oWordApp.Quit

End Sub

 
That did the trick tnguyen03, thank you!
 
OK, one more question...

Is there a way to set a portion of the text to bold? I can set font name and font size before the .header text statement but since it has to be a single statement for all lines there is no way (that I can see) to break off, switch to .boldrun, enter some text, toggle .boldrun off, and complete the rest of the header
 
Yes, it possible to bold/italic/underline some text from header/footer by using either .Words(index) or .Sentences(index) properties. Words property is applied for a particular word from the selection unlike Sentences property, it applies for the whole sentences.

For example, my header reads "Header Bold Some Text", and you want to bold the word "Bold" and "Text" then try this:

.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Words(2).Bold = True
.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Words(4).Bold = True


Have fun!

 
Thanks again tnguyen03, you're my new hero
 
mine too! but i was looking for handling paragraphs. Whats all this 'sections' etc. stuff?
 
'Sections' is an arbitrary division of a document.

Paragraphs collection lets you deal with paragraphs as a collection (or of course as members of the collection)

If you go into Word|Tools|Macros|Visual Basic Editor Then Help|Index and search for paragraphs it gives a couple of examples
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top