0

I am trying to write a VBA script to convert a selection in a PowerPoint presentation into a Hyperlink that references the relevant slide. The text has at the end of each line a page number which is equal to the slide number + 15. The selection function seems to truncate before the end of the selection and does not put an end quote at the end of the string resulting in the error:

Run-time error

Run-time error '-2147188160 (80048240)': Hyperlink (unknown member): Invalid request. This kind of object cannot have text associated with it as a hyperlink

at the .Hyperlink.TextToDisplay line, code listed below.

Sub ConvertTextToHyperlink()
    Dim objShape As Shape
    Dim strText As String
    Dim strSubLink As Integer

    'Get the selected shape
    Set objShape = ActiveWindow.Selection.ShapeRange(1)

    'Get the text from the shape
    strText = objShape.TextFrame.TextRange.Text

    'Get the slide number
    strSubLink = (Right(strText, 3) + 15)

    'Add hyperlink
    With objShape.ActionSettings(1)  'Assuming a mouse click triggers the hyperlink
        .Action = ppActionHyperlink
        .Hyperlink.TextToDisplay = strText 'Display the text
        .Hyperlink.Address = Null  'Set the link address
        .Hyperlink.SubAddress = strSubLink 'Set the Slide number
    End With

End Sub

Additionally, the text being selected has been imported from a pdf, so that may be where the issue is, but I am unsure. The Text in question takes the format of [section title] ..................................... [page number]

I have tried to adjust the strText variable thinking there might be some disconnect between its definition and objShape's definition, but nothing seems to have changed.

1
  • The error message suggests that there's something odd about the selected shape. If you do this, what does it tell you: MsgBox ActiveWindows.Selection.ShapeRange(1).Type Commented May 2 at 13:08

1 Answer 1

0

Instead of using Hyperlink.TextToDisplay, try using objShape.TextFrame.TextRange.Text = strText.

Sub ConvertTextToHyperlink()
    Dim objShape As Shape
    Dim strText As String
    Dim strSubLink As Integer

    'Get the selected shape
    Set objShape = ActiveWindow.Selection.ShapeRange(1)

    'Get the text from the shape
    strText = objShape.TextFrame.TextRange.Text

    'Get the slide number
    strSubLink = (Right(strText, 3) + 15)
    
    'Set the display text
    objShape.TextFrame.TextRange.Text = strText

    'Add hyperlink
    objShape.TextFrame.TextRange.ActionSettings(ppMouseClick).Action = ppActionHyperlink
    objShape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink.Address = ""
    objShape.TextFrame.TextRange.ActionSettings(ppMouseClick).Hyperlink.SubAddress = strSubLink

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.