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

I'm working on a program the plays with PowerPoint and Word, transferring info between the two. The PPT side works fine. I'm on W2k with ActiveState 6.5.1 build 629. Office 2000.

I'm trying to write stuff in two fonts in Word. I've searched SuperSearch and google and this is the best I've come up with:

use Diagnostics; use strict; use Win32::GUI; use Win32; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Win32::OLE::Const 'Microsoft PowerPoint'; my $wordApp = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', sub {$_[0]->Quit;}) or die "NO WORD!" ; $wordApp->{Visible} = 1; my $wordDoc = $wordApp->Documents->Add("/work/CG.dot"); # Go to the insertion point my $point = $wordDoc->GoTo({What => wdGoToBookmark, Name => "Start"}); my $ins_range = $point->Duplicate; #insert in default font $ins_range->InsertAfter("Hello"); $ins_range->InsertParagraphAfter(); # $ins_range->Font->Name = "Arial" gives illegal lvalue $ins_range->Font->Name("Courier New"); $ins_range->InsertAfter("GoodBye"); #Also, this fails with type mismatch in my real proggie # but not when I take it as an example; my $dst = "foo.doc"; $wordApp->ActiveDocument->SaveAs({ FileName => $dst, FileFormat => wdFormatDocument });
Unfortunately the font never changes in the document. The text is inserted correctly otherwise. I have tried (as you can see from the comments) a few things. Also, strangely, in my real program the "saveas" dies with a type mismatch, but here it does not. Any help would be appreciated. Oh, BTW, I know that saveas prompts. That is for debugging.

Thanks! --traveler

Replies are listed 'Best First'.
Re: MSWord OLE, font and saveas Problems
by c-era (Curate) on Nov 09, 2001 at 01:01 UTC
    You were so close. When writing to a property of an object with perl, you have to enclose the property name with {}.
    # $ins_range->Font->Name = "Arial" gives illegal lvalue # turns into $ins_range->Font->{Name} = "Arial";
    Since you're using OLE, you'll be working with objects a lot. Always remember that properties are enclosed in curly braces.

    As for your other problem, without knowing what you are trying to saveas, I really can't help you out.

    P.S. msdn.microsoft.com is the best place to find information on the OLE interface. If you want the examples in perl, just change all the '.'s to '->', put '{}' around the properties, change 'Dim' to 'my', and add a '$' in front of the variables ;^)

      Many thanks! I'd missed the {} around properties part! I had figured out the rest of the conversion stuff.

      As for your other problem, without knowing what you are trying to saveas, I really can't help you out.

      Well, I copied the code directly from my app so maybe I'm missing something if you cannot figure out what I'm trying to saveas, niether can Word, probably. Am I missing something?

      Thanks! --traveler

Re: MSWord OLE, font and saveas Problems
by Anonymous Monk on Dec 22, 2004 at 18:50 UTC
    Finally something that brings me closer to my goal....Can anyone help?

    I am attempting to move through a directory, pull out only files of a .fin extension (which is Word document format), save each one as .rtf (Rich Text Format). I'd also like to be able to run some predefined macros on the files, but I thought just being able to save was a good place to start.

    Currently I get an error stating 'Can't call method "SaveAs" on an undefined value'. If someone could point me to some clearer text on Win32::OLE than the stuff that ASPN, CPAN and Oreilly's Win32 or Nutshell book I'd greatly appreciate it. If you could tell me what stupid thing I am missing this time that would be great too.

    Here's the code:

    #usr/bin/perl use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $articleDirName = "c:\\work\\test\\psq\\test\\"; unless (opendir(DIR, $articleDirName)) { warn "Can't open $articleDirName\n"; closedir(DIR); exit(1); } foreach (readdir(DIR)) { next if $_ eq '.' || $_ eq '..'; $path = "$articleDirName$_"; if (-f $path){ if ($path =~ /\.fin/i){ $doc = Win32::OLE->GetObject($path); $fileName = $path . ".rtf"; print "$fileName\n"; $doc->ActiveDocument->SaveAs({FileName => $fileName, FileFo +rmat => wdFormatRTF}) } undef $doc; undef $path; undef $fileName; } }