Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

WIN32::OLE Word

by fox8267 (Initiate)
on Mar 14, 2008 at 13:44 UTC ( #674201=perlquestion: print w/replies, xml ) Need Help??
fox8267 has asked for the wisdom of the Perl Monks concerning the following question:

I am on window xp I am trying to save a doc file in .htm format,below code is working but with some issues It close all the word file open in the system. is there any way to open and close only single doc
---------------------------------------- #!perl -w use strict; use File::Find; find(\&save_as_html, "C:\\abc"); sub save_as_html { use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; my $filename = $File::Find::name; my $savename = "$filename".".htm"; if (( -f $filename ) && (rindex (substr ($filename,-4,4),".doc") = += 0 )) { print "Starting word\n"; my $Word = Win32::OLE->GetActiveObject('Word.Application') or Win32::OLE->new('Word.Application', 'Quit'); $Word->{Visible}= 0; print "Opening $filename \n"; $Word->Documents->Open( $filename ) or die("Unable to open document ", Win32::OLE->LastError() +); print "Saving $filename "."as $savename\n"; $Word->ActiveDocument->SaveAs({ FileName => $savename, FileFormat => 8}); print "Closing document\n"; $Word->ActiveDocument->save( $filename ); $Word->ActiveDocument->save( $savename ); #$Word->Quit; } }

Replies are listed 'Best First'.
Re: WIN32::OLE Word
by NetWallah (Canon) on Mar 14, 2008 at 16:00 UTC
    You may want to consider moving the instantiation of the $Word object Outside the sub. This should improve efficiency considerably, since you would use the same instance of Word to open all files.

    Of course, if you are opening only one file, it will not matter, but, as a matter of style, you may want to do that anyway, to help someone maintaining your program, who may consider using it differently.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: WIN32::OLE Word
by Erez (Priest) on Mar 14, 2008 at 14:42 UTC

    File::Find will recourse through each and every directory/file you have on the machine, and apply the code whenever it meets the conditions you set. What you set is "work on every file that ends with '.doc'.

    As Perl's DWIM mechanism isn't telepathic (not yet, that is), you need to specify more. Either put more direction in the code, such as what file to work on (file name, contents to look for), or a more explicit path.
    You can say return unless at the start of your sub to make it only work on the exact file.

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

      find will work only on given dirs. given sub should not close allready open docs from other dirs.. I wrote below mecro in word to save as htm and it does the same (closed all the file) as soon I exe it. is there any way we can control what doc we are closing.
      Sub Macro1() ' ' Macro1 Macro ' Macro recorded 3/14/2008 by amresh.dubey ' ChangeFileOpenDirectory "D:\Documents and Settings\amresh.dubey\De +sktop\" ActiveDocument.SaveAs FileName:="Testing cv.htm", FileFormat:=wdFo +rmatHTML _ , LockComments:=False, Password:="", AddToRecentFiles:=True, _ WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFo +nts:=False, _ SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsA +OCELetter:= _ False ActiveWindow.View.Type = wdWebView ActiveWindow.Close Application.Quit End Sub

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674201]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-06-23 18:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.