我正在尝试将文本粘贴到 PowerPoint 幻灯片中的文本框,然后从 Excel 中另一个单元格的内容添加指向该文本的链接。我似乎无法正确获得对象定义。我从启用宏的 Excel 文件开始,并打开一个 ppt 文件并打开一张空白幻灯片。
我不断收到此错误:
运行时错误'-2147188160(80048240)':
ActionSettings.Item:整数超出范围。 0 不在 Index 的有效范围 1 到 2 内。
这是我使用的 Excel VBA 代码。我为此测试打开了一个 ppt 空白文件。
Sub Add_Hyperlink_to_Text_in_Powerpoint()
' to add the hyperlink to the actual text not the frame.
Dim sh As Object
Dim pp, ppApp, pptPres As Object
Const leftcm As Single = 0, topcm As Single = 0 'just for test
'define ppt application and file
Set ppApp = CreateObject("PowerPoint.Application")
Set pptPres = ppApp.ActivePresentation
'create shape to paste text and link
Set sh = pptPres.Slides(1).Shapes.AddShape(Type:=msoShapeRectangle, Left:=leftcm, Top:=topcm, Width:=260, Height:=35)
'paste text in textbox
sh.TextFrame.textRange.Text = "This is a Link"
'paste link in text of textbox
With sh.TextFrame.textRange.ActionSettings(ppMouseClick)
.Action = ppActionHyperlink
'.Hyperlink.Address = "https://www.google.com"
.Hyperlink.SubAddress = "73. PowerPoint Presentation"
.Hyperlink.TextToDisplay = sh.TextFrame.textRange.Text
End With
End Sub
如果代码在 Excel/Word VBA 中执行,则所有 PPT VBA 常量都应替换为其值。
'paste link in text of textbox
With sh.TextFrame.TextRange.ActionSettings(1) ' ppMouseClick = 1
.Action = 7 ' pActionHyperlink = 7
.Hyperlink.Address = "https://www.google.com"
.Hyperlink.SubAddress = "73. PowerPoint Presentation"
.Hyperlink.TextToDisplay = sh.TextFrame.TextRange.Text
End With