gpt4 book ai didi

delphi - 通过Delphi XE5生成word文件的标题

转载 作者:行者123 更新时间:2023-12-01 18:28:13 25 4
gpt4 key购买 nike

我想调整用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

最佳答案

以下内容适用于使用 D7 和 Word 2007 的我,但也适用于两者的更高版本。

我不确定你是如何得到你的代码的,但我创建了我的部分,通过在 MS Word 中录制宏来插入和格式化标题,然后将其“翻译”到 Delphi,编辑掉一些路上多余的“绒毛”。我的代码使用“后期绑定(bind)”(即它通过变体访问 MS Word 对象),但我认为使用 Word2000 单元中定义的接口(interface)对象(即早期绑定(bind))重写它会很简单。

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;

关于delphi - 通过Delphi XE5生成word文件的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34630053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com