Consulting

Results 1 to 10 of 10

Thread: Use Regex in VBA to replace text between tags in PowerPoint (multiple occurences)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Use Regex in VBA to replace text between tags in PowerPoint (multiple occurences)

    Hi,

    I am struggling with the following problem:

    My goal is to replace some text in the slide notes of a PowerPoint file using VBA. If the text in the slide notes looks like this (single occurence of the tag to be deleted) my code below works. However, for multiple occurences of the tags it does not work correctly.

    The input in this case looks like this:

    This is a first sentence. 
    <code1>This second sentence needs to be deleted.</code1>
    Here is a third sentence. This one should be kept.
    <code1>This fourth sentence needs to be deleted as well.</code1>
    And the wrong output like this:

    This is a first sentence.
    Actually, I want to have this:

    This is a first sentence. 
    Here is a third sentence. 'This one should be kept.
    I would appreciate any advice on how to change the code below.

    Sub sync_text()
    input_text = ActivePresentation.Slides(1).NotesPage.Shapes(2).TextFrame.TextRange.Text
    ' Delete code1
    Set regX_delete = CreateObject("vbscript.regexp")
    With regX_delete
         .Global = True
         .Pattern = "<code1>(.+)</code1>"
    End With
    output_text = regX_delete.Replace(input_text, " ")
    ActivePresentation.Slides(1).NotesPage.Shapes(2).TextFrame.TextRange.Text = output_text
    End Sub
    Thanks for your help!
    Last edited by Aussiebear; 04-09-2023 at 07:54 PM. Reason: Reduced the whitespace

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •