Share via

Programmatically replacing text in powerpoint remove all hyperlinks.

Anonymous
2010-10-14T21:02:42+00:00

Hello everyone,

I'm building an application in c# that is meant to loop through all the slides of a series of powerpoints and find & replace text and hyperlinks. For example, if there is a text that says "Hello World", I want it to become "Allo World". Similarly, if I have a hyperlink that points to http://www.google.com, I want it to point tohttp://www.bing.com.

I was able to use code similar to the following to change all the hyperlink (I'm looping through all the slides of the powerpoints, etc and it's working fine)

foreach (Hyperlink hyperlink in slide.Hyperlinks)
                {
                    var address = hyperlink.Address;
                    hyperlink.Address = "http://www.bing.com/";
                }

In the similar fashion, I'm trying to use the following code to change the text.

if (shape.HasTextFrame == MsoTriState.msoTrue)
                    {
                        if (shape.TextFrame.HasText == MsoTriState.msoTrue)
                        {
                            var address = shape.ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address;
                            shape.TextFrame.TextRange.Replace("Hello", "Allo");
                        }
                    } The problem is the following. When I was testing it with a powerpoint with a single slide with a single textbox with a single word "Hello" on it. The text is properly changed to "Allo". However, if I add a hyperlink on that "Hello" word that points tohttp://www.google.com, and I run the segment of code above, the address variable remains null, the text is properly changed, however the link is gone. Essentially, the code above turns all link into simple text. Is there a way to avoid this?

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-10-15T16:08:29+00:00

You need to check the Action property for click on the textrange. If the action is is appActionHyperlink then it could be a external link in which case the Address property is populated. If the link is internal then the SubAddress property is populated so you should check both properties. Also, since there can be other action settings on the textrange you should copy those to if you want to retain the hyperlink functionality.

Some more info: http://www.pptfaq.com/FAQ00162.htm


Regards, Shyam Pillai. http://skp.mvps.org

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2010-10-14T22:32:51+00:00

Hi,

You would have to do the work if you wish to retain the hyperlink, PowerPoint won't do it for you. I would find the text to replaced but check if it contains a hyperlink. If it does then you should be able to read the information and restore it after the text is replace. The routine below changes all the occurance of the find text with the replace text. I've modified it to check if the text being replace has an actionsetting or not but not the code which does it. You should be able to use the same logic for ur C# code.

Sub ReplaceTextAndKeepHyperlinks(oShp As Shape, FindString As String, ReplaceString As String)

Dim oTxtRng As TextRange

Dim oFRng As TextRange

Dim oRRng As TextRange

Dim bHasLink As Boolean

If oShp.HasTextFrame Then

        If oShp.TextFrame.HasText Then

            Set oTxtRng = oShp.TextFrame.TextRange

            Set oFRng = oTxtRng.Find(FindWhat:=FindString, WholeWords:=True)

            Do While Not oFRng Is Nothing

                'Should also check for mouseover action which i am not here.

                If Not (oFRng.ActionSettings(ppMouseClick).Action = ppActionMixed Or oFRng.ActionSettings(ppMouseClick).Action = ppActionNone) Then

                    Debug.Print "Add code to read the actionsetting information"

                    bHasLink = True

                End If

                Set oRRng = oTxtRng.Replace(FindWhat:=FindString, _

                                              Replacewhat:=ReplaceString, _

                                              After:=oFRng.Start - 1, _

                                              WholeWords:=True)

                If bHasLink Then

                    Debug.Print "Add code to assign it back to the new textrange"

                End If

                bHasLink = False

                Set oFRng = oTxtRng.Find(FindWhat:=FindString, After:=oRRng.Start + oRRng.Length, _

                                              WholeWords:=True)

            Loop

        End If

    End If

End Sub  


Regards, Shyam Pillai. http://skp.mvps.org

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2010-10-15T16:44:36+00:00

    Oh Ok...all this time, I was probably looking at the wrong place.

    Thanks! I'll give it a try.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-10-15T15:40:55+00:00

    Hello,

    Thanks for the response. I created a simple powerpoint slide with a single hyperlink text to test the following line.

    var address = shape.ActionSettings[PpMouseActivation.ppMouseClick].Hyperlink.Address;

    If I understand it correctly, this should give me the "ActionSetting" object for a mouse click and from it, I should be able to retrieve the hyperlink address. Unfortunately, at this time, I am always getting null. Am I missing anything?

    Regards,

    Grigmin

    Was this answer helpful?

    0 comments No comments