【问题标题】:Delphi to Microsoft PowerPoint Integration... Setting Bold attributeDelphi 到 Microsoft PowerPoint 集成...设置粗体属性
【发布时间】:2018-04-05 05:16:38
【问题描述】:

德尔福西雅图,PPT 2013。

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

  • 类别 1 - 产品 1、产品 2
  • Category2 - product3、product4

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

我尝试添加第 1 行,将字符 1 到 9 设为粗体,SET CHARACTER 10 NOT BOLD,现在添加第 2 行……但这似乎没有任何区别。

这是一个代码 sn-p。

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;

任何想法如何解决这个问题?

【问题讨论】:

  • 我相信这就是 Powerpoint 和办公程序的一般行为方式。当您添加一个新段落时,它会采用前一段的字体,这有时会有点麻烦,即使您是手动进行的。我可以建议这样做的唯一方法是将文本选择设置为您计划插入新参数的位置并将字体显式设置为粗体关闭,或者添加您的选择并设置粗体关闭,也许在一个单独的过程中以简化事情当你多次这样做时。
  • so I want JUST THAT WORD in bold - 既然您使用的是 Microsoft Office,那么几乎所有这些问题都有一个通用答案:只需阅读 office 是如何做到的。开始“录制 Visual Basic 宏”。然后选择单词并将其设为粗体并取消选择它。然后“停止录制宏”。然后打开宏的管理器,找到你的新宏并进入它的编辑器。您将看到执行此操作的 Visual Basic 代码。将其从 Basic 转换为 Pascal。 PS。我不知道那些与宏相关的命令在 Office 2013 中隐藏在哪里。如果找不到,请尝试经典菜单 for Office 试用
  • 录制宏功能在 MOST Office 产品中,但在 Powerpoint 中没有。 Powerpoint 2007 是拥有它的最后一个版本...

标签: vba delphi com ms-office powerpoint


【解决方案1】:

这是在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2016-05-14
    相关资源
    最近更新 更多