Delphi 到 Microsoft PowerPoint 集成...设置粗体属性

Delphi to Microsoft PowerPoint Integration... Setting Bold attribute

Delphi 西雅图,PPT 2013。

我正在使用 Delphi 以编程方式构建 PPT 平台。我有一个正在使用的PPT模板。我提取作为资源嵌入到可执行文件中的 PPT 文件,我启动 PPT,打开文件,转到幻灯片,然后填充内容。我目前正在解决的问题与字体属性有关。我有一个文本框。我想在那个由 CR/LF 分隔的文本框中写入多行。 PPT 显示这些是单独的项目符号,这正是我想要的。每行的第一个词是一个类别,所以我只想用粗体显示那个词。它最终应该看起来像这样...

在代码中,我将第一行添加到文本框,然后说...从字符 1-9 将其设置为粗体。现在将第 2 行添加到文本框,并将第 20 个字符到接下来的 9 个字符设置为粗体...等。这是一个 4 行操作。添加第一行,设置粗体,添加第二行,设置粗体。第 1 行和第 2 行有效,但只要我添加第二行文本,所有内容都会变为粗体。如果我更改代码的顺序,添加第 1 行,添加第 2 行,将第一个子字符串设置为粗体,将第二个子字符串设置为粗体,这会起作用,但根据我做其他各种事情的方式,这会更加困难。

我试过添加第 1 行,将字符 1 到 9 设置为粗体,设置字符 10 不加粗,现在添加第 2 行...但这似乎没有任何区别。

这是一个代码片段。

uses ... Office2000, msppt2000;

    var
  lFinalDeck: PowerPointPresentation;
  lApplication: PowerPointApplication;
  sl: _Slide;
  sh1 : Shape;
  FoundIt : Boolean;
  i : Integer;
  CRLF : String;
  PPTFileName : String;
begin

lApplication := CoPowerPointApplication.Create;
...
// Open File
lFinalDeck := lApplication.Presentations.Open(PPTFileName, msoFalse, msoFalse, msoFalse);
// The second parameter specifies whether the presentation should be opened in read-only mode.
// If the third parameter is True, an untitled copy of the file is made.
// The last parameter specifies whether the opened presentation should be visible.

 // Get a handle to my slide and go to it.
 sl := lFinalDeck.Slides.Item(2);
 sl.Select;

 FoundIt := False;
 CRLF := #13#10;

 // Now look for the textframe that has the TEXT 'TEXTBOX1'
 for i := 1 to sl.Shapes.Count  do
    begin
    sh1 := sl.Shapes.Item(i);
    if sh1.HasTextFrame = msoTrue then
      if sh1.TextFrame.TextRange.Text = 'TEXTBOX1' then
      begin
       FoundIt := True;
       sh1.TextFrame.TextRange.Text := '';
       Break;
      end;
    end; 

    // Put data in the textframe, setting first 5 characters of each line bold
    sh1.TextFrame.TextRange.Text := '1234567890' + CRLF;
    sh1.TextFrame.TextRange.Characters(1,5).Font.Bold := msoTrue;


    sh1.TextFrame.TextRange.Text := sh1.TextFrame.TextRange.Text + 'ABCDEFGHIJKLMNOP' + CRLF;
    sh1.TextFrame.TextRange.Characters(13,5).Font.Bold := msoTrue;
end;

有什么解决办法吗?

这是在 VBA 中实现它的一种方法,它应该足以让您继续转换为 Delphi 或其他方法。

形状的 TextFrame.TextRange 表示形状中的整个文本字符串。 您可以遍历其 .Paragraphs 集合以访问每个单独的段落(本质上,每个文本字符串都以 CRLF 结尾)。 每个段落都有一个单词集合,通过引用第一个单词,您可以将该单词变为粗体。 如果您需要将一定数量的字符设为粗体,请使用

Set oRng = .Paragraphs(x).Characters(1, 12) ' 12 chars

而不是

Set oRng = .Paragraphs(x).Words(1)

以下:

Sub BoldFirstWord(oSh As Shape)
' Bold the first word of each paragraph in the shape

    Dim x As Long
    Dim oRng as TextRange

    With oSh.TextFrame.TextRange

        For x = 1 To .Paragraphs.Count

            Set oRng = .Paragraphs(x).Words(1)
            oRng.Font.Bold = True

        Next

    End With

End Sub

' To test the above, select the text shape
' you want to work with and run this:
Sub TestBFW()
    Dim oSh As Shape
    Set oSh = ActiveWindow.Selection.ShapeRange(1)
    Call BoldFirstWord(oSh)
End Sub