Find and Replace Shape Text in VBA

Started by jogjer, December 24, 2008, 10:05:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Does anyone know how to create a VBA macro to do find and replace

No I can't
3 (42.9%)
No I can't
4 (57.1%)

Total Members Voted: 7

jogjer

I once had a macro that did 10 find and replace of shape text of flowcharts
that was written in Visio 2000 (but also worked in 2003). I lost the file
that had this macro and I can't remember how it was created.

A Microsoft technician helped me create it in 2001. I could recreate the user form with textboxes for txtFind1- txtFind10 and corresponding txtReplace1-txtReplace10 with a command button to start the macro's finding and replacing. Does anyone know how to create a macro like this?  I think it used two for next or for each loops.  One for the page and one for shapes.  I tried to recreate it but I couldn't get it to work.

Here is what I tried:


Dim vsoPages As Visio.Pages
Dim vsoPage As Visio.Page
Dim vsoShape As Visio.Shape
Dim vsoShapes As Visio.Shapes

Set vsoPages = Active.Document.Pages
Set vsoShapes = vsoPages.Shapes
Dim x As String

For Each vsoPage In vsoPages

    For Each vsoShape In vsoShapes
        startPos = FIND(txtFind1, Shape.Text)
        x = Replace(Shape.Text, startPos, Len(txtReplace1), txtFind1, txtReplace1)
        Shape.Text = x
    Next
   
Next

Paul Herber

#1
What are you trying to do that isn't more than a couple of minutes work with Edit -> Replace?

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

jogjer

#2
I am trying to recreate a macro that does 10 find and replaces at once.  When I created this macro I was reusing flowcharts for reoccuring projects that had certain information change like week numbers, various codes, project numbers, etc.  Since there were many things that changed at once it was much more convenient to change them all at once.  I don't remember exactly how it was written and I lost the copies I had of it.

Paul Herber

#3
Try putting these week numbers etc into custom properties (shape data) in the document or page ShapeSheet, then link the shapes that need them to those properties. It's then just a matter of changing the data in the custom properties.

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

jogjer

#4
For one I don't know how to do that and for the other that limits you to three custom variables and you have to do that for each shape, which would be very time consuming and not something most visio users would be able to do.  I wanted something that anyone could run easily without having to set up custom variables for each shape.

wapperdude

#5
Not exactly sure what you're after, but, this macro has the basic construct of your example.  See attached visio drawing, and text file, which constains the macro.  It will change "MyText" with "NewText" in the shape.

HTH
Wapperdude
Visio 2019 Pro

jogjer

#6
The change macro looks like it will change the text for one shape, but I want to change the text for all the shapes and all the pages on the document.

In my sample I included only one replace because I was trying to get the one to work (which I wasn't successful at). I want to have 10 replaces in this macro with the find conditions called txtFind1 thru txtFind10 and the replace conditions called txtReplace1 thru txtReplace10.

I don't understand what the swap macro is doing.  I know the old macro had two for loops and / or for each loops (one for page and the other for shapes) and some sort of replace function but I couldn't remember how it selected the text for each shape or how the replace was constructed. 

Using characters would be difficult because you have to number each shape's character.  If you don't know how many shapes you have that would be difficult to get it to work.  Is there a way to do a replace using the shape.text instead of character.text?

I have attached the file (drawing1.vsd) that has the form I created (userform1) and the few tries I took to attempt to recreate this macro.

wapperdude

#7
Ah.  Guess I didn't remove the extra macro.   ::)  The text file had the example in question, but it doesn't do the loops.   :(  Try the following:

Sub ChgText()
    Dim vsoPage As Visio.Page, vsoShape As Visio.shape
    Dim vsoCharacters1 As Visio.Characters, vsoStrng As String
   
    For Each vsoPage In ThisDocument.Pages
        For Each vsoShape In vsoPage.Shapes
            Set vsoCharacters1 = vsoShape.Characters
            vsoStrng = vsoCharacters1.Text [/color]                 [color=green]'This step not necessary, vsoCharacters1.Text could be placed directly in the formula on the next line.[/color]
            [color=blue]vsoCharacters1.Text = Replace(vsoStrng, "MyText", "NewText")
        Next
    Next
End Sub

The outer loop should do every page in your Visio document.  The inner loop will do every shape on the page.  You will need to set up your criteria for substitution.

I haven't looked at your drawing yet, wanted to clarify your questions first.  Shape.text just fetches the entire text.  The characters let's you do replacements.  In this case, it is an entire string.  So vsoCharacters1.text is all of the shape text.  Within that body of text, this macro replaces the string MyText with the string NewText.  You can setup as many of these fetch and replace statements as needed.

Sorry for the confusion.   :'(

I'll take a look at your drawing next, but hopefully, there's enough in the above macro to get you on your way.

Wapperdude

Visio 2019 Pro

jogjer

#8
I made a few substitutions to fit my form's names but I got an error message with the replace:  compiler error: wrong number of arguments or invalid property assignment.



Private Sub butSubmit_Click()
Dim vsoPage As Visio.Page, vsoShape As Visio.Shape
    Dim vsoCharacters1 As Visio.Characters, vsoStrng, vsoFind, vsoReplace As String
   
    For Each vsoPage In ThisDocument.Pages
        For Each vsoShape In vsoPage.Shapes
            Set vsoCharacters1 = vsoShape.Characters
            vsoStrng = vsoCharacters1.Text
            vsoFind = txtFind1.Text
            vsoReplace = txtReplace1.Text
            vsoCharacters1.Text = Replace(vsoStrng, vsoFind, vsoReplace)
        Next
    Next

wapperdude

#9
txtFind1 and txtReplace1 haven't been defined, need to declare them in Dim statements.
Visio 2019 Pro

jogjer

#10
They are defined in the userform1 why would it need to be in dim statements?

wapperdude

#11
Step thru the macro in debug mode (F8).  After each line is executed, you can check the values of the variables.  You can also see where the macro barfs.  I don't think the userform passes the variable types into the macro.  Left hand and right hand type thing.  Anyway, it's easy enough to try.  Afraid I have to call it a night.  I'll check back tomorrow.  Perhaps the European "early risers" will have jumped in before then.
Visio 2019 Pro

wapperdude

#12
When you assign your text to the find and replace variables, are they enclosed with double quotes?   ???
Visio 2019 Pro

jogjer

#13
It is assigned when the form is run.  So no it is not assigned in double quotes it is typed into the textboxes txtFind1 .. txtFind10, txtReplace1 .. txtReplace10.  Why won't it accept the data typed into the form in this replace statement?

jogjer

#14
And by the way it won't let me step through the macro it only gives me the error message:
"compile error: wrong number of arguments or invalid property assignment" in the replace function call.