Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 609
  • Last Modified:

Perl script to open document in Word

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
altrnt
Asked:
altrnt
  • 2
1 Solution
 
clockwatcherCommented:
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
 
zlobchoCommented:
Do you need something like this:

http://www.adp-gmbh.ch/perl/word.html
0
 
altrntAuthor Commented:
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
 
clockwatcherCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now