I want to adjust the header of a word file generated with Delphi such that the first line is bold and the second line is not bold.
But since the string for the header is one string I cannot seem to get the second line normal.
How can I ensure that the second line in the header of a word file is not bold?
procedure Print;
var v:olevariant;
procedure HeaderandFooter;
var adoc:olevariant;
begin
v.Selection.Font.Bold:=1;
adoc:= v.Documents.Add(EmptyParam, EmptyParam);
adoc.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Text :=
'Line one of the header which is bold' +#13
+ 'Line two of the header which is normal';
end
The following works for me using D7 and Word 2007, but should work fine with later versions of both.
I'm not sure how you arrived at your code, but I created the part of mine which inserts and formats the header by recording a macro to do it in MS Word, then "translating" it to Delphi, editing out some superfluous "fluff" on the way. My code uses "late binding" (i.e. it accesses the MS Word objects via variants) but I think it would be straightforward to re-write it using the interfaced objects defined in the Word2000 unit (i.e. early binding).
uses ... ComObj, Word2000 ...;
procedure TForm1.MakeDocWithHeader;
var
MSWord,
Document : OleVariant;
AFileName,
DocText : String;
begin
MSWord := CreateOleObject('Word.Application');
MSWord.Visible := True;
Document := MSWord.Documents.Add;
// First, insert some text into the new document's body
DocText := 'Hello Word!';
MSWord.Selection.TypeText(DocText);
// Next, make the Header window the active one
if MSWord.ActiveWindow.View.SplitSpecial <> wdPaneNone then
MSWord.ActiveWindow.Panes(2).Close;
if (MSWord.ActiveWindow.ActivePane.View.Type = wdNormalView) or (MSWord.ActiveWindow.ActivePane.View.Type = wdOutlineView) then
MSWord.ActiveWindow.ActivePane.View.Type := wdPrintView;
MSWord.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
// Now, add three lines of text to the header
MSWord.Selection.TypeText( Text:='Header line 1');
MSWord.Selection.TypeParagraph;
MSWord.Selection.TypeText( Text:='Header line 2');
MSWord.Selection.TypeParagraph;
MSWord.Selection.TypeText( Text:='Header line 3');
// Next, make the first line bold
MSWord.Selection.HomeKey( Unit:=wdStory);
MSWord.Selection.EndKey( Unit:=wdLine, Extend:=wdExtend);
MSWord.Selection.Font.Bold := True;
MSWord.Selection.HomeKey (Unit:=wdLine);
// Finally, return the caret to the main body of the document
MSWord.Selection.GoTo(What:=wdGoToPage, Which:=wdGoToNext, Count:=1);
AFileName := 'd:\aaad7\officeauto\worddocwithheader.docx';
Document.SaveAs(AFileName);
ShowMessage('Paused');
Document.Close;
end;
Update: I've added a Delphi implementation of Cindy Meister's solution.
procedure TForm1.MakeDocWithHeader2;
var
MSWord,
Document,
rngDocument,
rngHeade,
Headers : OleVariant;
DocText : String;
begin
MSWord := CreateOleObject('Word.Application');
MSWord.Visible := True;
Document := MSWord.Documents.Add;
DocText := 'Hello Word!'#13;
// Following is a Delphi adaptation of the implementation in Cindy Meister's answer.
rngDocument := Document.Content;
rngDocument.Text := DocText;
Headers := Document.Sections.Item(1).Headers;
rngHeader := Headers.Item(wdHeaderFooterPrimary).Range;
rngHeader.Text := 'Header Line 1'#13;
rngHeader.Font.Bold := True;
rngHeader.Collapse(wdCollapseEnd);
rngHeader.Text := 'Header Line 2';
rngHeader.Font.Bold := False;
end;