none
Find and Replace strings within all VISIO shapes via Powershell RRS feed

  • Question

  • Hallo,

    I am trying to to figure out how I can do an autmatical find & replace with powershell within a Visio file.

    The idea in the end is that I can replace all visible strings within a single visio document. I was able to find code snippets for excel and Word, but I haven't find something similiar for Visio.

    Thanks a lot and best regards,

    Fabian

    Monday, May 7, 2018 4:50 AM

Answers

  • THank you.

    The good thing is I have found a Visio Powershell Module in the PS Gallery.

    https://www.powershellgallery.com/packages/Visio/2.25.0

    Within here you can write a Script to easily get all Shapes, iterate through them, get the Shape text, do some comparisons, replace the string and change the Shapes text via

    Set-VisioShapeText -Text $replaceString -Shapes $shape

    IMO this thread can be closed!

    • Marked as answer by lenzker Wednesday, May 9, 2018 6:08 PM
    Wednesday, May 9, 2018 6:07 PM

All replies

  • Hi Fabian,

    Based on your description, I will move your thread to General Office Development forum:

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=officegeneral

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Emi


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 7, 2018 8:23 AM
  • Hello lenzker,

    As far as I know, Visio does not hava a Find/Replace VBA method. Please note I said VBA method. It does have a Find/Replace function, but if you try to record a macro for it, you will see Visio is trying to set the text one by one.

    So what we should do is trying to loop the pages and loop the shapes and then set the shape's text directly.

    Not familiar with Power Shell, here is a simply code with some comments in VBA. Hope it could help you accomplish the PowerShell Script.

    Sub Macro1()
        'declare variable
        Dim vsoPage As Visio.Page, vsoShape As Visio.Shape
        Dim vsoCharacters As Visio.Characters, vsoFindStr, vsoReplaceStr As String
        'comment: the text you want to find
        vsoFindStr = "Test"
        'comment: the text you want to use to replace original text
        vsoReplaceStr = "ReplaceText"
        'comment:loop all pages in the document
        For Each vsoPage In ThisDocument.Pages
            'comment:loop all shapes in the page
            For Each vsoShape In vsoPage.Shapes
                'comment: get Characters object of the shape
                Set vsoCharacters = vsoShape.Characters
                'comment:use replace to replace string and set back to characters
                vsoCharacters.Text = Replace(vsoCharacters.Text, vsoFindStr, vsoReplaceStr)
            Next
        Next
    End Sub

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 7, 2018 9:57 AM
  • THank you.

    The good thing is I have found a Visio Powershell Module in the PS Gallery.

    https://www.powershellgallery.com/packages/Visio/2.25.0

    Within here you can write a Script to easily get all Shapes, iterate through them, get the Shape text, do some comparisons, replace the string and change the Shapes text via

    Set-VisioShapeText -Text $replaceString -Shapes $shape

    IMO this thread can be closed!

    • Marked as answer by lenzker Wednesday, May 9, 2018 6:08 PM
    Wednesday, May 9, 2018 6:07 PM