Visio Guy

Visio Discussions => Programming & Code => Topic started by: eggmatters on June 09, 2008, 07:43:33 PM

Title: How do I use "search and replace" programmatically?
Post by: eggmatters on June 09, 2008, 07:43:33 PM
Hello all, I am scripting some seemingly trivial Visio automation in Perl. I have a module that but calls the same object library as VB, just a slightly different calling syntax. I merely want to write some code that calls the find and replace functionality in Visio to do just that. I've found a code sample in word but it doesn't work in Visio. I tried to record a new macro but it just did a character by character replacement without calling find. The following (in Perl) is what all I have to work with:


my $Visio = Win32::OLE->new('Visio.Application', 'Quit');
my $VDocs = $Visio->Documents;
my $VDoc = $VDocs->Open("$path$vfile");

my $findText = "Text to find";
my $replaceText "Text to replace it with";

my $search = $VDoc->Content->Find;
my $replace = $search->Replacement;

$search->{Text} = $findText;
$replace->{Text} = $replaceText;
$search->Execute({Replace => wdReplaceAll});



In VB, the "->" are dots, the my's are dims, the $ sign tells PERL it's a variable, and the '{}' brackets tell Perl that this data is pointed to by an object (as a hash) if that helps at all.

The OLE complains, of course, when it encounters wdReplaceAll since that is specific to a word document. vsoReplaceAll or visReplaceAll don't exist and  my object browser can't find anything remotely near 'Execute() although the OLE parser doesn't seem to mind (It will run if I pass no arguments to execute)

Any help would be appreciated. Code samples in a more Microsoft friendly language like VB or C# are, of course, entirely acceptable.
Title: Re: How do I use "search and replace" programmatically?
Post by: Visio Guy on June 09, 2008, 09:07:24 PM
Hi eggmatters,

The automation model between Word and Visio can be quite different. I don't think that Visio exposes its search/replace functionality to programmers.

Visio does have an .xml file format option (.vdx) which you might be able to exploit, however.

- Chris
Title: Re: How do I use "search and replace" programmatically?
Post by: eggmatters on June 09, 2008, 11:46:30 PM
Hmm, I'm curious because that kind of violates the whole Component Object Model type of thing. Also, you can do it from the front end, why not the back? I was trying to use a Perl regex for this but thought a simple OLE call would be easier. Funny you should mention the .xml schema since that was where I started. I had some really volatile code with that though and COM was much cleaner to at least get the data (Text values in shapes) and reduced my code by 100 lines. I am now in the process of translating this guy's: http://www.eggheadcafe.com/software/aspnet/30355600/visio-2003--custom-prope.aspx (http://www.eggheadcafe.com/software/aspnet/30355600/visio-2003--custom-prope.aspx)

code into Perl. My little object browser no longer works now though. I think there's a brute force way of doing it by setting character by character. We'll see. That will be a process I'd rather avoid. Thanks for your feedback. I'll let you know what I find. Do you guys a need for any Perl crossover for Visio development? I'm going to be doing a lot of it in the next few months
Title: Re: How do I use "search and replace" programmatically?
Post by: Visio Guy on June 10, 2008, 07:28:44 AM
Hi em,

Well the code-link that you sent shows walking the shapes in a Visio page and analyzing their text. This isn't a "search capability" per-se, but it will get the job done.

Keep in mind that shapes can have various levels of nesting. A shape can be a group that contains shapes...that contain more shapes...

To look inside a shape, just look at its .Shapes property:


If shp.Shapes.Count > 0 Then

  For Each shpSub as Visio.Shape in shp.shapes

    '...do something

  Next shpSub

End If


I'm curious, why are you writing perl code for Visio? (I don't know much about perl...)