Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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; } }

Comment on WIN32::OLE Word
Download Code
Re: WIN32::OLE Word
by Erez (Curate) 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
Re: WIN32::OLE Word
by NetWallah (Abbot) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674201]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-11-28 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (197 votes), past polls