water Posted July 29, 2019 Share Posted July 29, 2019 The following example (created quick&dirty ) replaces all occurrences of $sFindWord to $sReplaceWord in the presentation ($sPresentation). Could you place play a bit with this code? Func FindReplaceAll($oPresentation, $sFindWord, $sReplaceWord) Local $oSlide, $oShape, $oShapeText, $oTempText ; Loop through each slide in Presentation For $oSlide In $oPresentation.Slides For $oShape In $oSlide.Shapes ; Store shape text into a variable $oShapeTxt = $oShape.TextFrame.TextRange ; Ensure There is Text To Search Through If $oShapeTxt <> "" Then ; Store text into a variable $oShapeTxt = $oShape.TextFrame.TextRange ; Find First Instance of "Find" word ; FindWhat: The text to search for ; ReplaceWhat: The text you want to replace the found text with ; After: The position of the character (in the specified text range) after which you want to search for the next occurrence of FindWhat. ; MatchCase: Determines whether a distinction is made on the basis of case. ; WholeWords: Determines whether only whole words are found. $oTempText = $oShapeTxt.Replace($sFindWord, $sReplaceWord, Default, Default, True) ; Find Any Additional instances of "Find" word (if exists) While IsObj($oTempText) $oShapeTxt = $oShapeTxt.Characters($oTempText.Start + $oTempText.Length, $oShapeTxt.Length) $oTempText = $oShapeTxt.Replace($sFindWord, $sReplaceWord, Default, Default, True) WEnd EndIf Next Next EndFunc ;==>FindReplaceAll My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
water Posted August 4, 2019 Share Posted August 4, 2019 (edited) Enhanced version of the findreplace function: expandcollapse popup#include <PowerPoint.au3> Global $sFindText = "Test", $sReplaceText = "xxxx" Global $oPPT = _PPT_Open() Global $oPresentation = _PPT_PresentationOpen($oPPT, @ScriptDir & "\Presentation_Replace.pptx", True) Global $iReplaceCount = _PPT_PresentationFindReplace($oPresentation, $sFindText, $sReplaceText, Default) MsgBox(0, "_PPT_PresentationFindReplace", "Number of replacements: " & $iReplaceCount) ; #FUNCTION# ==================================================================================================================== ; Name...........: _PPT_PresentationFindReplace ; Description ...: Find & replace text throughout entire PowerPoint presentation ; Syntax.........: _PPT_PresentationFindReplace($oPresentation, $sFindText, $sReplaceText[, $vSlides2Process = Default, $bMatchCase = False, $bWholeWords = False) ; Parameters ....: $oPresentation - Object of the presentation to modify. ; $sFindText - The text to be searched for. ; $sReplaceText - The replacement text. To delete found text use an empty string (""). ; $vSlides2Process - [optional] Slides to process - either a single index, or a range string (see remarks for details) (default = 0). ; $bMatchCase - [optional] Determines whether a distinction is made on the basis of case (default = False). ; $bWholeWords - [optional] Determines whether only whole words are found (default = False). ; Return values .: Success - the number of performed replacements. ; Failure - 0 and sets @error. ; |1 - $oPresentation is not an object or not a presentation object ; |2 - $sFindText is empty ; |3 - $vSlides2Process is invalid. The range consists of one value and this value is not numeric. ; |4 - $vSlides2Process is invalid. The range consists of two values and value 1 needs to be > 0 and value 2 either "*" or numeric. ; Author ........: water ; Modified ......: ; Remarks .......: $vSlides2Process can be a string containing the slides to process. ; It can be a single number or a range denoted by the first and last slide separated by a hyphen (-). ; 0 as a single number denotes to process all slides. ; * as the last slide number lets the function calculate the last slide number. ; Related .......: ; Link ..........: https://www.thespreadsheetguru.com/the-code-vault/find-and-replace-all-powerpoint-vba-macro ; Example .......: Yes ; =============================================================================================================================== Func _PPT_PresentationFindReplace($oPresentation, $sFindText, $sReplaceText, $vSlides2Process = 0, $bMatchCase = False, $bWholeWords = False) Local $oSlide, $oShape, $oShapeTextFrame, $sShapeText, $oTempText, $iReplaceCount = 0, $iSlideCount = 0, $aSlides2Process, $bSlideRange = False If $bMatchCase = Default Then $bMatchCase = False If $bWholeWords = Default Then $bWholeWords = False If $vSlides2Process = Default Then $vSlides2Process = 0 If Not IsObj($oPresentation) Or ObjName($oPresentation, 1) <> "_Presentation" Then Return SetError(1, 0, 0) If StringStripWS($sFindText, $STR_STRIPALL) = "" Then Return SetError(2, 0, 0) $aSlides2Process = StringSplit($vSlides2Process, "-", $STR_NOCOUNT) If @error Then ; Only $iStartSlide provided If IsNumber($aSlides2Process[0]) Then $bSlideRange = True If $aSlides2Process[0] = 0 Then $iStartSlide = 1 $iEndSlide = $oPresentation.Slides.Count Else $iStartSlide = $aSlides2Process[0] $iEndSlide = $aSlides2Process[0] EndIf Else Return SetError(3, 0, 0) EndIf Else $bSlideRange = True If $aSlides2Process[0] > 0 Then ; Two values provided $iStartSlide = $aSlides2Process[0] If $aSlides2Process[1] = "*" Then ; * = process all remaining slides $iEndSlide = $oPresentation.Slides.Count Else $iEndSlide = $aSlides2Process[1] EndIf Else Return SetError(4, 0, 0) EndIf EndIf ; Loop through each slide in presentation For $oSlide In $oPresentation.Slides $iSlideCount += 1 If $bSlideRange And ($iSlideCount < $iStartSlide Or $iSlideCount > $iEndSlide) Then ContinueLoop ; Current slide outside range For $oShape In $oSlide.Shapes ; Store shape text into a variable. If the shape contains no textframe continue with the next shape $oShapeTextFrame = $oShape.Textframe If @error Then ContinueLoop ; Get text of the textframe. If there is an error continue with the next shape $oTextRange = $oShapeTextFrame.TextRange If @error Then ContinueLoop $sShapeText = $oTextRange.Text If @error Then ContinueLoop ; Ensure there is text to search through If $sShapeText <> "" Then ; Store text into a variable $oTextRange = $oShapeTextFrame.TextRange ; Find first instance of "FindText" $oTempText = $oTextRange.Replace($sFindText, $sReplaceText, Default, $bMatchCase, $bWholeWords) If IsObj($oTempText) Then $iReplaceCount += 1 ; Find any additional instances of "Findtext" (if exists) While IsObj($oTempText) $oTextRange = $oTextRange.Characters($oTempText.Start + $oTempText.Length, $oTextRange.Length) $oTempText = $oTextRange.Replace($sFindText, $sReplaceText, Default, $bMatchCase, $bWholeWords) If IsObj($oTempText) Then $iReplaceCount += 1 WEnd EndIf Next Next Return $iReplaceCount EndFunc ;==>_PPT_PresentationFindReplace Edited August 5, 2019 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now