Word Automation through Win32::OLE

Long time lurker, first time poster...please be gentle.

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, 
FileFormat => wdFormatRTF})
       }
       undef $doc;
       undef $path;
       undef $fileName;
   }
}



0
cutter1994
12/22/2004 7:16:17 PM
perl.beginners 29388 articles. 4 followers. Follow

0 Replies
870 Views

Similar Articles

[PageSpeed] 44
Get it on Google Play
Get it on Apple App Store

Reply:

Similar Artilces:

Word Automation through Win32::OLE #2
Long time lurker, first time poster...please be gentle. 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 Win3...

WORD OLE automation
The following code works perfectly with WORD6/Windows95/PB6, but does not work for WORD 97/NT. OLEObject lole_word string ls_mytext = "Trial text" li_rc = lole_word = CREATE OLEObject li_rc = lole_word.ConnectToNewObject("word.basic") // in NT WORD is opened INVISIBLY // It appears as a process in the Task Manager // li_rc = 0 li_rc = lole_word.FileNew("normal", 0) // Returns li_rc = NULL li_rc = lole_word.Insert(ls_mytext) // Not recognised by WORD97 li_rc = ...

MS-Word & Powerpoint automation, using OLE automation [Edit]
Hello, Here is what I need to do, from my Delphi application : starting from a MS-Word document, create a PPT presentation simply made of a series of embedded MS-Word objects. There would be 1 slide for each page of the source document (assuming same page size & orientation on both sides). This requires to control both a WordApplication and a PowerpointApplication in parallel, and proceed basically as follows : - in Word, open the source file - In Powerpoint, create a new presentation - For each page of the Word doc, |- in Word, select from start to end of current page, then &quo...

Word OLE automation
Would like to open word document using OLE after populating word using OLE Automation. The document should open up in print preview mode,and then using either VBA, or other code print or close from print preview. I want to supress all menus and commandbars from the word document. What code can i run from PB script to open in print preview and turn off menus and command bars, and put in two of my own command buttons. ...

Word OLE automation
I am about to embark on a PB 5.0 project that will require dynamic creation of documents for use with Word 97's mail merge capability. Being that this is my first time using OLE, as well as my first time programming Word, could anybody point me in the right direction (books, articles, web sites, etc.) to get me started? I've experimented a little with what I could find on Microsoft's and Sybase's sites, but I am looking for something more comprehensive. =============== Daniel Buki Software Developer Butte County (CA) Migrant Education cerkit@ncal.net In article...

Word 2010 OLE automation
Hello there I user Delphi 2010 with Office 2010. When i try to open word document from my delphi application it open the file but the word stayes minimized. this is my code: procedure OpenOleWordFile(Var MsWord:Variant;WordFileName:String); Var WordDoc,Mdoc:Variant; begin try MsWord := GetActiveOleObject('Word.Application'); except try MsWord := CreateOleObject('Word.Application'); except ShowMessage('Error ....cannot open WORD'); exit; end; end; MsWord.Visible:=true; MsWord.Application.WindowState := 1...

Excel and/or Word OLE Automation
I found out that the client machines that will be running my app will have office 95 on them. So I decided to use the spell checking example that Powersoft provied to work with Excel, instead of using the VS ocx. I found out that I handed in one set of problems for yet another set of problems. I took the example from PS and changed the mle to be dw column. The text in the column has hard returns in it, so when I set the text in excel I get | where the hard returns are. This is only a minor problem, that I could live with. However the big problem is that one of two things happen...

OLE Automation with Word 7
All my users and fellow developers are on Word v7.0a (which does not work) and only I happen to have 7.0 (which works). I've tried background printing on and off in Word. The routine loops through multiple records as shown beloew. The first run through is fine. It fails on the second record at the FileOpen/FileNew. I tried adding wait loops of 10 seconds (!) after the fileprint and the fileclose - to no avail. The error is "Error calling external object function fileopen...". I tried upgrading to the latest PB5.0.04 maint release - still no dice. Does Powerb...

Word 6 OLE automation
Hi, I'm using OLE automation with Word 6 to perform a mail merge. Most of the methods work OK (FileOpen, FilePrint etc.) but some of them don't (FileClose). PB says : Error calling external object function blablabla ... The function's name is OK, and the parameters also. I've debugged the externalexception event, and the OLE server tells me that I've tried to handle an element like a function when it isn't. The bizarre thing is that, calling a macro with that instruction (FileClose) works. Perhaps the solution is putting all the code in the macro ? ...

OLE automation with word 97
Dear Sir /Madam I like to ask about how to use the oleobject with Word 97, On word 95 and its predecessor, its clear to use "word.basic" but the speed is very slow compare with other OLE connectivity, So I try with word 97, but the format and function of word 97 is very different from the previous ones, they use VBA (for visual Basic Command), So I like to ask if to use OLE with word97 to connect and merge letter with data supplied by PowerBuilder, like code below, what is the corresponding code in word 97?, or Does PowerBuilder 5.0 does not support Word 97 or vice-versa. ...

Win32::OLE Word Macro Conversion
Hi there all, I'm writing a simple script to batch convert word documents into HTML. However, I need also to disable the impossibly stupid "Smart Tags" that get embedded. The VBA macro below contains the important lines ( .EmbedSmartTags = False, .LabelSmartTags = False). Could anyone give me a clue to getting that functionality into the script at the bottom? Very many thanks! Richard ###########MACRO Sub yadda() ' ' yaddaMacro ' Macro recorded 23/11/2004 by Richard Barrett-Small ' With Options .LocalNetworkFile = False ...

OLE Automation with Delphi and MS Word
Hi, I'm working with Delphi for a while now, but it's the first time that I need to export in Word some data taken from a sqlite database. With SQLite there are no problems, everything works as it should. With Word, also because of the lack of good part of documentation, I'm currently having troubles, in particular, in the moment when i go to split a cell of a table, depending on the number of occurrences found with a SQLite query (1 occurrence = do nothing, 2 occurrences = split the cell into two rows, 15 occurrences = split the cell into 15 rows and so on). When i create ...

OLE Word control
I am trying to bring up a window that lets the user view a Microsoft Word document. From that window, the user can click a button called "OK TO PRINT" and from there, the word document is printed. Some users do not have Word on their machines so using an embedded OLE object is the way to go. In my window, I placed an ole control of type "Microsoft Word Document". The display type is "Displayasactivexobject!" Then I went to the open event for the window and put in the following code: w_newnumber.ole_newnumber.Activate(InPlace!) --> because I ...

Using OLE Automation with Word 8
We're working on converting an application that uses Word.Basic OLE Automation with Word 7 to use Word.Application OLE Automation with Word 8. We've applied the 5.0.03 patches. For some reason, we can open a document, but we can't save or saveas. The following script is being used: long i string ls_filename, ls_test_filename integer li_contact_id setpointer(hourglass!) //retrieve datawindow dw_quote_rpt.retrieve(dw_1.getitemnumber(1, "quotejob_id")) //create ole object and connect to MSWord iole_quote = create oleobject if iole_quote.connecttonewobj...