我想调整用Delphi生成的word文件的标题,这样第一行是粗体,第二行不是粗体。
但由于标题的字符串是一个字符串,我似乎无法使第二行正常。
如何确保word文件标题中的第二行不是粗体?
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
答案 0 :(得分:2)
以下适用于我使用D7和Word 2007,但应该可以与以后版本的两者兼容。
我不确定你是如何得到你的代码的,但我创建了我的部分,通过录制宏来在MS Word中进行插入和格式化标题,然后"翻译"它到德尔福,编辑出一些多余的"绒毛"在途中。我的代码使用"后期绑定" (即它通过变体访问MS Word对象)但我认为使用Word2000单元中定义的接口对象(即早期绑定)重新编写它是很简单的。
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;
更新:我已经添加了Cindy Meister解决方案的Delphi实现。
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;
答案 1 :(得分:2)
我无法为您提供Delphi代码,但我可以为您提供VBA代码,其方法与Martyn提出的方法略有不同。
宏录制器的一个缺点是它模仿用户操作,而不是直接使用对象模型。这通常较慢,导致屏幕闪烁并且不太可靠,因为用户可以点击屏幕上的某个位置,从而更改选择。
使用RANGE对象更加可靠,特别是因为它避免了SeekView,这绝对是错误的。这是一个你可以比较的例子;请注意,该方法与原始方法更类似于建议的答案:
Dim rngDoc as Word.Range
Dim rngHeader as Word.Range
Set rngDoc = Document.Content
Set rngHeader = Document.Sections(1).Headers(Word.WdHeaderIndex.wdHeaderFooterPrimary).Range
rngDoc.Text = DocText
rngHeader.Text = "Header Line 1" & Chr(13)
rngHeader.Font.Bold = -1
rngHeader.Collapse Word.WdCollapseDirection.wdCollapseEnd£
rngHeader.Text = "Header Line 2" & Chr(13) & "Header Line 3"
rngHeader.Font.Bold = 0
&#34;技巧&#34;用这种方法就是'崩溃&#34;应用格式后,在继续使用新文本之前,将范围设置为其终点。