简体   繁体   English

在 VBA 中创建指向特定 powerpoint 幻灯片的单个超链接而不覆盖

[英]Creating single hyperlinks to specifc powerpoint slides in VBA without overwritting

So I have an excel sheet with hyperlinks to other sheets before --> hyperlinks所以我有一个 excel 表,之前有指向其他表的超链接 -->超链接

I have created a powerpoint generator in vba, where this hyperlinks also should be transfered to the created powerpoint.我在 vba 中创建了一个 powerpoint 生成器,该超链接也应该转移到创建的 powerpoint。 I looked here for some suggestions and that's what I've created so far:我在这里寻找一些建议,这就是我到目前为止所创建的:

Sub pptGenerator()

Application.ScreenUpdating = False

Dim oRng As TextRange, i As Integer, j As Integer, pptC As Integer, wsCnt As Long
Dim indexText As String, indexText2 As String, t As Table, lRow As Long, lCol As Long
Dim PowerPointApp As PowerPoint.Application, myPresentation As PowerPoint.Presentation

wsCnt = ThisWorkbook.Worksheets.Count

DestinationPPT = Application.ActiveWorkbook.Path & "\PPT_template_16_9.pptx"
Set PowerPointApp = CreateObject("PowerPoint.Application")
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)


For pptC = 1 To 2

    For i = 1 To wsCnt
        Sheets(i).Activate
        lRow = Cells(Rows.Count, 1).End(xlUp).Row
        lCol = Cells(1, Columns.Count).End(xlToLeft).Column

        For j = 3 To lRow 'counter begins after the subtitle

            'Link to the indices point before
            If Range("A2").Value = "Subtitle" Then
                indexText2 = ActiveSheet.Range("B" & j).Offset(0, -1).Value & vbCrLf
                'Stop

                If Range("A" & j).Value = "Hyperlink 1" Then
                    Set t = PowerPointApp.ActivePresentation.slides(i).shapes(1).Table.Cell(4, 1)                                                                                                                                       
                    With t.Cell(4, 1).shape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink
                        .TextToDisplay = "01 Hyperlink"
                        .SubAddress = PowerPointApp.ActivePresentation.slides(2).SlideNumber _
                            & ". " & PowerPointApp.ActivePresentation.slides(2).Name
                    End With

                End If
            End If
        Next j

        PowerPointApp.ActivePresentation.slides(i - 1).shapes(1).TextFrame.TextRange.InsertAfter indexText2

    Next i

Next pptC

Application.ScreenUpdating = True
MsgBox "Done!"

End Sub

Now I have the problem that Excel shows me the runtime error 80004005 where the method Table for the object Shape failed.现在我遇到的问题是 Excel 向我显示运行时错误 80004005 ,其中 object Shape的方法Table失败。 Otherwise, instead of using a table, if I'll do it like this:否则,如果我这样做,而不是使用表格:

Set oRng = PowerPointApp.ActivePresentation.slides(i - 1).shapes(1).TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink
With oRng
    .SubAddress = PowerPointApp.ActivePresentation.slides(2).SlideNumber _
        & ". " & PowerPointApp.ActivePresentation.slides(2).Name
End With

The last Hyperlink will taken for whole sheet, so it will be overwritten.最后一个超链接将用于整个工作表,因此它将被覆盖。

How can I solve this problem?我怎么解决这个问题?

I fixed the problem by using this oRng instead of using a table object我通过使用这个oRng而不是使用表 object 解决了这个问题

If Range("A" & j).Value = "Hyperlink 1" Then
   Set oRng = PowerPointApp.ActivePresentation.slides(i - 1).Shapes(1).TextFrame.TextRange.InsertAfter(indexText2)
   With oRng.ActionSettings(ppMouseClick).Hyperlink
            .SubAddress = PowerPointApp.ActivePresentation.slides(2).SlideNumber _
             & ". " & PowerPointApp.ActivePresentation.slides(2).Name
   End With
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM