【问题标题】:Add Hyperlinks to Powerpoint from Matlab using ActiveX使用 ActiveX 从 Matlab 将超链接添加到 Powerpoint
【发布时间】:2020-08-29 15:45:16
【问题描述】:

有谁知道我如何使用 matlab 和 activeX 将超链接添加到 powerpoint 文件?

在 MatlabCentral 上有两个有用的帖子,但它们并没有给我所需的一切。第一篇讲解如何使用matlab创建powerpoint文件:"Create Powerpoint Files with Matlab"

第二个显示如何使用 ActiveX 将超链接插入 Excel:"Add Hyperlink in Excel from Matlab"(参见 Kaustubha 的第二个答案)

我试图合并这两个答案。在 powerpoint 中,slide 对象具有 .Hyperlinks 属性,但没有像 Excel 中那样用于 .Hyperlinks.Add 方法。

这是我到目前为止的代码。我希望链接出现在表格中:

ppt = actxserver('PowerPoint.Application');
op = invoke(ppt.Presentations,'Add');

slide = invoke(op.Slides,'Add',1,1);
sH = op.PageSetup.SlideHeight;   % slide height
sW = op.PageSetup.SlideWidth;    % silde width

table = invoke(slide.Shapes, 'AddTable', 1, 3, 0.05*sW, sH*.2, 0.9*sW, sH*.60);
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Text = 'www.stackoverflow.com';

% Add hyperlink to text in table using ActiveX
% slide.Hyperlinks - this exists but there is no add feature 

invoke(op,'Save');
invoke(op,'Close');
invoke(ppt,'Quit');
delete(ppt);

【问题讨论】:

    标签: matlab hyperlink activex powerpoint


    【解决方案1】:

    Slide 对象有一个 .Hyperlinks 集合,您可以检查它以了解有多少超链接,它们指向的位置等等。要添加超链接,您必须使用单独的形状或文本范围。

    Sub AddAHyperlink()
    
        Dim oSh As Shape
    
        ' As an example we're going to add hyperlinks to the
        ' currently selected shape.
        ' You could use any other method of getting a reference to
        ' a shape that you like, however:
    
        Set oSh = ActiveWindow.Selection.ShapeRange(1)
    
        ' Add a hyperlink to the shape itself:
        With oSh
            .ActionSettings(1).Hyperlink.Address = "http://www.pptfaq.com"
            ' you can also add a subaddress if required
        End With
    
        ' Or add the hyperlink to the text within the shape:
        With oSh.TextFrame.TextRange
            .Text = "Hyperlink me, daddy, 8 to the click"
            .ActionSettings(1).Hyperlink.Address = "http://www.pptools.com"
        End With
    
    End Sub
    

    要访问表格单元格中的文本,您需要做的是:

    table.Table.Cell(1,1).Shape.TextFrame.TextRange
    

    table.Table.Cell(1,1).Shape
    

    Set oSh = table.Table.Cell(1,1).Shape
    

    然后使用我上面显示的相同代码

    【讨论】:

    • 嘿史蒂夫感谢您的回复,但是当我通过 matlab 使用 activex 时,这不起作用。这是我尝试过的:table.Table.Cell(1,1).Shape.TextFrame.TextRange.ActionSettings(1).Hyperlink.Address = 'http://wwww.pptools.com'; 这是我收到的错误消息:No appropriate method, property, or field Hyperlink for class Interface.Microsoft_PowerPoint_14.0_Object_Library.ActionSettings.
    • 唯一可用的方法是:addproperty, Application, Count, delete, deletproperty, events, get, invoke, Item, loadobj, Parent , release, saveobj, set
    • 我不是 MatLab 用户,所以我无法提供任何关于如何从中驱动 PPT 的真正帮助,但有一点值得建议:也许是语法问题。 PPT对象模型暴露了ActionSettings集合;有两个成员,所以在 VBA 中,我们指定 ActionSettings(1) 表示鼠标点击动作,集合的第一个成员。也许您需要使用不同的语法将索引传递到集合中?
    • 谢谢史蒂夫,我认为您的解决方案可以完成工作,但 matlab activex api 似乎没有提供完整的功能集。
    【解决方案2】:

    不确定这是否仍然是来自任何人的主动请求,并且程序功能可能已经改变;但对于其他可能感兴趣的人来说,似乎可以添加链接。

    这是我编写的一些代码的示例...幻灯片编号/项目引用需要针对您的任务进行更新,但我认为它涵盖了关键点。在此示例中,目标是在演示文稿中添加指向另一张幻灯片的超链接。

    hyperlink_text = sprintf('%0.0f, %0.0f, %s', Presentation.Slides.Range.Item(3+i).SlideID, Presentation.Slides.Range.Item(3+i).SlideIndex,Presentation.Slides.Range.Item(3+i).Shapes.Item(2).TextFrame.TextRange.Text);
    

    超链接文本看起来像这样,作为一个文本字符串。 '250, 4, 幻灯片标题'

    Presentation.Slides.Range.Item(3).Shapes.Item(2).Table.Cell(1+i,1).Shape.TextFrame.TextRange.ActionSettings.Item(1).Hyperlink.SubAddress = hyperlink_text;
    

    对于内部链接,Hyperlink.Address 字段可以留空。 似乎之前的答案中唯一缺少的是,当使用 Matlab 执行 powerpoint VBA 时,您需要使用 ActionSettings.Item(1) 来引用 mouseclick 动作,而不是从 basic 中显示的 ActionSettings(1) PowerPoint VBA。

    希望这对仍在寻找的其他人有所帮助。

    请注意,我目前在 Microsoft 365 ProPlus 中使用 Matlab R2017A 和 Powerpoint

    【讨论】:

      猜你喜欢
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2017-04-01
      • 2015-07-23
      • 2019-02-11
      • 2012-03-31
      • 1970-01-01
      相关资源
      最近更新 更多