Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a word file that I want to output in two streams. The first is for conversion to XML but I need to maintain some partial tagging, thus I am saving as .rtf. The second stream is for future pdf creation and I need to clean the original file using a premade Word macro. It is the second stream I am having trouble with. I can get the output of the first stream fine, I can save the second stream as a word file, but I can't get the macro to work and/or the changed content to save.
#Create filenames $file =~ s/(.*)\.fin/$1/i; $file = $1; my $fileName = "$file.rtf"; my $fileName2 = "$file.doc"; #Convert to RTF my $doc = Win32::OLE->new('Word.Application', 'Quit'); $doc->Documents->Open($path); $doc->ActiveDocument->SaveAs({FileName => $fileName, FileFormat => wdF +ormatRTF}); #Close Winword $doc->Quit(); undef $doc; #Run pretty print macro and save as word doc format my $doc1 = Win32::OLE->new('Word.Application', 'Quit'); $doc1->Documents->Open($path); $doc1->ActiveDocument->Run("psqtemplate"); $doc1->ActiveDocument->SaveAs({FileName => $fileName2}); # Close Winword $doc1->Quit(); undef $doc1;

Replies are listed 'Best First'.
Re: Win32::OLE running macros
by wfsp (Abbot) on May 07, 2005 at 07:42 UTC
    try
    $doc->Run("psqtemplate");
    instead of
    $doc1->ActiveDocument->Run("psqtemplate");

    see here

    Untested

      Thanks, been there. This just seems so straight forward.
      1. Open an instance of a MS office product
      2. Open an instance of a document using that product instance
      3. Run macro on active document using product instance
      4. SaveAs new filename.
      Obviously I am not sure why it still isn't working.
      When I make the change you suggest I get a "save" dialogue rather than a "SaveAs" one.
      I guess I just keep working on it until I find the right combination...kind of like a Rubix Cube.
        This works...in case anyone cares in the future
        #Create filenames $file =~ s/(.*)\.fin/$1/i; $file = $1; my $fileName = "$file.rtf"; my $fileName2 = "$file.doc"; my $doc = Win32::OLE->new('Word.Application', 'Quit'); $doc->Documents->Open($path); $doc->ActiveDocument->SaveAs({FileName => $fileName, FileFormat => wdF +ormatRTF}); $doc->ActiveDocument->SaveAs({FileName=> $fileName2}); $doc->Run("psqtemplate"); $doc->ActiveDocument->Save; #Close Winword $doc->Quit(); undef $doc;