Event for shape text changing due to Replace

Started by ImAPictureFormat, December 16, 2019, 08:24:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ImAPictureFormat

Hi,

I've been levering the Document.ShapeExitedTextEdit event to update shape data when the user modifies the text in a shape, but I discovered today that that event does not fire when the user uses the Replace dialog to change a shape's text, which makes the shape's text and it's shape data go out of sync.

I tried using the Application.TextChanged event, but it doesn't look like it fires in that case either.

Is there an event I could use in this case or is this something that can't be done?

Thank you!

Nikolay

It looks like the "TextChanged" is fired? When you close the "Find / Replace" dialog. Checked with Visio 2010

bwharrington

#2
Try hooking up the TextChanged event to the shape your trying to change instead of the document object and see if that works.

shape.TextChanged += Shape_TextChanged;

If that does not work you will probably need to use the CellChanged event and detect that the change was specific to the shape and the cell Prop.Name.

shape.CellChanged += Shape_CellChanged;

If this works you will need to hook up the event to the shape every time a shape is created.

Hooking them directly to the document object could lead to them being fired in a lot of cases you do not want them to. Which effects performance dramatically and you will have to write more code to narrow down what your looking at within those event if your hooked up to the document property. Its worth investing your time to understand the differences when hooking up events. You might have to experiment as these things are not well documented.

ImAPictureFormat

Quote from: Nikolay on December 16, 2019, 10:16:45 PM
It looks like the "TextChanged" is fired? When you close the "Find / Replace" dialog. Checked with Visio 2010

Sorry, I realize I didn't explain myself fully in my original post. What I would like to see is an event that fires for each shape where text is replaced. When I tried the TextChanged event I only saw it fire for the last shape. Here's what I tried. I have a class module set up to sink events like so:
Public WithEvents appevent As Application

Public Sub appevent_TextChanged(ByVal shp As Visio.Shape)
    Debug.Print shp.Name
End Sub


Here's my test process:
1. I placed two shapes on the page that both contain the word "Apple".
2. Opened the "Replace" dialog, found the first instance of "Apple" and replaced it with "Pear".
3. Found the next instance of "Apple" and replaced it.
4. Closed the dialog. Note that at this point the second shape was still in text edit.

After closing the dialog, I didn't see any indicator of the TextChanged event until after I clicked out of the second shape, at which point I saw the name of the second shape in the Immediate window. So, it looks like the event only fired for the last shape.

ImAPictureFormat

Quote from: bwharrington on December 17, 2019, 09:33:26 PM
Try hooking up the TextChanged event to the shape your trying to change instead of the document object and see if that works.

shape.TextChanged += Shape_TextChanged;

Apologies, I'm actually using VBA. Do you know how I would do that in VBA? I tried doing the following, but to no avail, and finding examples on google is like pulling teeth sometimes.
Public Sub Shape_TextChanged(shp As Visio.Shape)
' handle the event
End Sub

Nikolay

#5
Sorry I just checked for me the even "TextChanged" fires for ALL shapes where the text is changed:

Maybe you have some additional code (or extension) that somehow interferes with this behavior?
Could you try that on a clean Visio installation?

ImAPictureFormat

Just getting back to work after the holidays, so I'm looking at this again. Unfortunately I can't do a test with a clean install, I'm on a work computer. However, I did the same test that you did in the gif and it works as it did for you. As you said, there must be something interfering. I will investigate what in my setup might be affecting it.

ImAPictureFormat

I'm not sure what I was doing wrong before, but it works now. Thanks for your help!