Solved

Perl script to open document in Word

Posted on 2009-07-11
5
552 Views
Last Modified: 2012-05-07
Dear reader,

if Microsoft Word is already open on my desktop, does a script excist to open a document in word from a certain part on my HD? Also is it possible to copy a certain word in Word with perl?

Thanks in advance.
0
Comment
Question by:altrnt
  • 2
5 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 24831487
Win32::OLE's GetActiveObject method can return a reference to the currently running word application.  The code snippet below is an example of opening a document within the currently running word app.

As far as copying a word goes, what word are you trying to select?  You can do anything that Word's object model supports and copying a word is definitely part of it.  But in order to give you an example that would be of any help, you'd need to describe how to go about finding the word you want to copy (e.g., "want to select the word(s) that are between some other specific words" or "the second word of the third paragraph").
use Win32::OLE;
 

my $wd = Win32::OLE->GetActiveObject("Word.Application") || die Win32::OLE->LastError();
 

if ($wd) {

  $wd->Documents->Open("c:\\some\\path\\somewhere\\document.doc");

}

Open in new window

0
 
LVL 6

Expert Comment

by:zlobcho
ID: 24832428
Do you need something like this:

http://www.adp-gmbh.ch/perl/word.html
0
 

Author Comment

by:altrnt
ID: 24834142
clockwatcher:

for example I want to select the word: "typing" in the first and only sentence.
So the sentence in the word document would be:
I am currently typing in word.

And then I would like to copy the word "typing"..
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 250 total points
ID: 24846599
I'm still not clear what you're really trying to do within Word.  By copying, do you mean selecting and copying to the clipboard or do you mean placing inoto a perl variable or something altogether different?

The following code dumps the words out of the document that you loaded into your active word app.
use Win32::OLE;
 

my $wd = Win32::OLE->GetActiveObject("Word.Application") || die Win32::OLE->LastError();
 

$doc = $wd->Documents->Open("c:\\mypath\\mydocument.doc");
 

my $words = $doc->Words->{Count};

for ($i = 1; $i <= $words; $i++) {

    print $i,": ",$doc->Words->Item($i)->Text,"\n";

}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now