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

标签 delphi ms-word header delphi-xe5

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

相关文章:

python - 使用 Perl/Batch/Python 更新 MS Word 文档的详细信息 "content status"

vb.net - 如何通过Word Interop专门对齐每个段落?

Delphi:如何在 TEdit/TMaskEdit 中设置文本而不调用 onchange 事件

delphi - 如何将结构内的 C 联合转换为 Delphi

Delphi - E2010 不兼容类型 : 'Integer' and 'Char' - Any ideas

C++头文件编译不通过?

C++ 头文件链接器错误

delphi - 如何创建一个不慢的环绕/滚动平铺区域?

vba - 如何使用 VBA 清除 Office 剪贴板

cocoa - <Cocoa/Cocoa.h> 位置