seeking Win32::OLE example

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default seeking Win32::OLE example

    I was trying to put together a quick hack to extract review comments
    (using the Comments property) from a Word document to jump start
    compiling review minutes, but I found the MSDN Word object model
    documentation surprisingly opaque. For example, the docs didn't say
    which property held the comment tag (e.g., [b1]), and the empirical
    measure of iterating over the keys didn't bear fruit.

    I played with the code in <36182bc8.8113706@news2.ibm.net> and found
    that it made a difference whether I called Documents->Open or
    GetActiveObject -- although the two seem equivalent in the docs.

    Can people recommend a good resource? I read Jan's TPJ article and
    the various pods, but I was frustrated with my low progress.

    Thanks,
    Greg
    --
    Even the most mighty government, operating with the utmost severity,
    cannot succeed in endeavors that are contrary to what has been called
    "economic law."
    -- Ludwig von Mises
    Greg Bacon Guest

  2. Similar Questions and Discussions

    1. Dave Roth's site (Win32::AdminMisc, Win32::ODBC, etc.) not available.
      Does anyone know of an alternate method to contact Dave Roth (other then rothd@roth.net )? It appears that his entire domain is unavailable...
    2. Seeking a programmer
      Hello. I need to develop a multiuser application using sliders. The application is designed and ready to be implemented. Is there a programmer here...
    3. Win32-PerfMon on Win32
      Windows 2000(SP4) ActivePerl 5.8.3 I found this escapade rather confusing, I'm warning you now. I cannot install this Win32-PerfMon module, I...
    4. Seeking solutions
      Kam, What Colin is saying is, in order to save as BMP, File|Save As, then choose BMP as the file type. But the question was, from Colin, why in...
    5. To All Those Seeking XML & PHP Wisdom!
      Found a brand new tutorial... just came out today... http://www.sitepoint.com/article/1165 Joe Harman Have you ever noticed? Anybody going...
  3. #2

    Default Re: seeking Win32::OLE example

    [email]gbacon@hiwaay.net[/email] (Greg Bacon) wrote:

    : I was trying to put together a quick hack to extract review comments
    : (using the Comments property) from a Word document to jump start
    : compiling review minutes, but I found the MSDN Word object model
    : documentation surprisingly opaque. For example, the docs didn't say
    : which property held the comment tag (e.g., [b1]),

    What is your example "[b1]" meant to show? Is that the expected content
    of the document's comments property?

    : and the empirical
    : measure of iterating over the keys didn't bear fruit.

    A splash of code will better show what you are talking about, and will
    probably show where the difficulty lies.

    : I played with the code in <36182bc8.8113706@news2.ibm.net> and found
    : that it made a difference whether I called Documents->Open or
    : GetActiveObject -- although the two seem equivalent in the docs.

    Which docs were you reading that suggested that the
    Win32::OLE->GetActiveObject method is similar to the Word application's
    Documents->Open method?

    Are you perhaps thinking of the Win32::OLE->GetObject method when you
    say GetActiveObject?

    : Can people recommend a good resource?

    A resource on using Win32::OLE? The pods that come with ActivePerl are
    pretty much it, and pretty much all that is needed. Win32::OLE is not
    much more than a way for Perl to poke and prod at OLE libaries.

    A resource on how any of those libraries responds to being poked and
    prodded? That's a job for the library's author.

    The OLE Browser included with ActivePerl is a decent tool for navigating
    the various libraries and the classes within those libraries. Be sure
    to notice the clickable help icon in the bottom frame.

    The object browser in the Visual Basic Editor (included with any Office
    installation) does mostly the same job, but also has a very useful
    search tool.

    As for your original quick hack program, see if this does not fill the
    need.

    #!perl
    use warnings;
    use strict;
    use Win32::OLE;
    use Win32::OLE::Const 'Microsoft Word';
    print
    Win32::OLE
    ->GetObject('foo.doc')
    ->BuiltinDocumentProperties(wdPropertyComments)
    ->Value;

    Jay Tilton Guest

  4. #3

    Default Re: seeking Win32::OLE example

    In article <3f5fbdd5.696097308@news.erols.com>,
    Jay Tilton <tiltonj@erols.com> wrote:

    : [email]gbacon@hiwaay.net[/email] (Greg Bacon) wrote:
    :
    : : I was trying to put together a quick hack to extract review comments
    : : (using the Comments property) from a Word document to jump start
    : : compiling review minutes, but I found the MSDN Word object model
    : : documentation surprisingly opaque. For example, the docs didn't say
    : : which property held the comment tag (e.g., [b1]),
    :
    : What is your example "[b1]" meant to show? Is that the expected content
    : of the document's comments property?

    No, not the comment itself, the comment *tag*. I'm talking about the
    tags identify each comment and show up in the document as hidden text.
    The leader seems to be the author's initials because in a dummy doc I
    just created, Word tagged my comments as [geb1] and so on.

    The comment text itself is straightforward:

    print "$_->{Range}{Text}\n" for in $Doc->{Comments};

    : : and the empirical
    : : measure of iterating over the keys didn't bear fruit.
    :
    : A splash of code will better show what you are talking about, and will
    : probably show where the difficulty lies.

    Here's the code I've been using to explore:

    #! perl

    use strict;
    use Win32::OLE qw/ in /;

    sub usage { "Usage: $0 [--active | --open]\n" }

    ## main
    die usage unless @ARGV;

    Win32::OLE->Option(Warn => 2);

    my $Word;
    my $Doc;

    my $arg = shift;
    if ($arg eq '--active') {
    $Doc = Win32::OLE->GetActiveObject('Word.Application', 'Quit');
    }
    elsif ($arg eq '--open') {
    $Word = Win32::OLE->new('Word.Application', 'Quit');

    # requires absolute path?
    $Doc = $Word->Documents->Open('c:\temp\foo.doc');
    }

    #$Doc->{Visible} = 1;

    foreach my $Property (in $Doc->BuiltinDocumentProperties) {
    my $Name = $Property->Name;
    local $Win32::OLE::Warn = 0;
    my $Value = $Property->Value;

    $Value = '***Error***' if Win32::OLE->LastError;
    $Value = '<undef>' unless defined $Value;
    printf "%s %s = %s\n", $Name, '.' x (40-length($Name)), $Value;
    }

    foreach my $c (in $Doc->{Comments}) {
    print $c->{Reference}{BookmarkID}, " - ", $c->{Range}{Text}, "\n";
    }

    #$Doc->{Saved} = 1;

    In the loop over BuiltinDocumentProperties, I'd tried iterating
    over keys %$Property and printing out the values, but the comment
    tag didn't appear.

    : : I played with the code in <36182bc8.8113706@news2.ibm.net> and found
    : : that it made a difference whether I called Documents->Open or
    : : GetActiveObject -- although the two seem equivalent in the docs.
    :
    : Which docs were you reading that suggested that the
    : Win32::OLE->GetActiveObject method is similar to the Word application's
    : Documents->Open method?
    :
    : Are you perhaps thinking of the Win32::OLE->GetObject method when you
    : say GetActiveObject?

    I see that I was unclear. I should have written "equivalent, assuming
    the document we want is open". Using the code above with the --active
    option, I get the following error:

    Win32::OLE(0.1603) error 0x80020011: "Does not support a collection"
    in METHOD/PROPERTYGET "" at props line 29
    Can't call method "Name" on an undefined value at props line 30.

    I obviously read more into the docs than was there, and I'm happy to
    take correction. Where can I find a good orientation to thinking OLE?

    : : Can people recommend a good resource?
    :
    : A resource on using Win32::OLE? The pods that come with ActivePerl are
    : pretty much it, and pretty much all that is needed. Win32::OLE is not
    : much more than a way for Perl to poke and prod at OLE libaries.
    :
    : A resource on how any of those libraries responds to being poked and
    : prodded? That's a job for the library's author.
    :
    : The OLE Browser included with ActivePerl is a decent tool for navigating
    : the various libraries and the classes within those libraries. Be sure
    : to notice the clickable help icon in the bottom frame.

    Didn't notice the help icon before. Thanks for the pointer. I'm
    closer:

    foreach my $c (in $Doc->{Comments}) {
    my $tag = $c->{Initial} . $c->{Index};
    print "$tag - ", $c->{Range}{Text}, "\n";
    }

    Now I'm trying to get at the highlighted portion of the document
    associated with a given comment. Strike that:

    foreach my $c (in $Doc->{Comments}) {
    my $tag = $c->{Initial} . $c->{Index};
    print $tag, " - ", $c->{Range}{Text}, "\n";
    print $c->{Scope}{Text}, "\n";
    }

    : [...]
    : As for your original quick hack program, see if this does not fill the
    : need.
    :
    : #!perl
    : use warnings;
    : use strict;
    : use Win32::OLE;
    : use Win32::OLE::Const 'Microsoft Word';
    : print
    : Win32::OLE
    : ->GetObject('foo.doc')
    : ->BuiltinDocumentProperties(wdPropertyComments)
    : ->Value;

    That produced no output. (Yes, I used the appropriate path.)

    A couple of remaining questions:

    Why does iterating over the BuiltinDocumentProperties make the
    application think I have changes that need saving? (I see that setting
    the Saved property says nothing needs saving.)

    What the difference between getting at the document via Documents->Open
    versus GetActiveObject? I'd like to avoid typing long paths.

    Greg, drifting off-topic :-(
    --
    Bad terminology is the enemy of good thinking.
    -- Warren Buffett, 2001 Chairman's letter
    Greg Bacon Guest

  5. #4

    Default Re: seeking Win32::OLE example

    [email]gbacon@hiwaay.net[/email] (Greg Bacon) wrote:

    : In article <3f5fbdd5.696097308@news.erols.com>,
    : Jay Tilton <tiltonj@erols.com> wrote:
    :
    : : [email]gbacon@hiwaay.net[/email] (Greg Bacon) wrote:
    : :
    : : : I was trying to put together a quick hack to extract review comments
    : : : (using the Comments property) from a Word document
    :
    : No, not the comment itself, the comment *tag*. I'm talking about the
    : tags identify each comment and show up in the document as hidden text.
    : The leader seems to be the author's initials because in a dummy doc I
    : just created, Word tagged my comments as [geb1] and so on.

    I'm hip. You want the comments embedded within the document's body, not
    the "comment" property of the document object.

    : Here's the code I've been using to explore:

    [snipped here, but mostly retained in comments below]

    : : : I played with the code in <36182bc8.8113706@news2.ibm.net> and found
    : : : that it made a difference whether I called Documents->Open or
    : : : GetActiveObject -- although the two seem equivalent in the docs.
    : :
    : I see that I was unclear. I should have written "equivalent, assuming
    : the document we want is open". Using the code above with the --active
    : option, I get the following error:
    :
    : Win32::OLE(0.1603) error 0x80020011: "Does not support a collection"
    : in METHOD/PROPERTYGET "" at props line 29
    : Can't call method "Name" on an undefined value at props line 30.

    I see now. No, the GetActiveObject method cannot be used to hook into
    an already-open document in one step. It can be used to connect with a
    running Word application, then that application object can return the
    open document.

    #! perl
    use strict;
    use Win32::OLE qw/ in /;
    sub usage { "Usage: $0 [--active | --open]\n" }
    die usage unless @ARGV;
    Win32::OLE->Option(Warn => 2);
    my $Word;
    my $Doc;
    my $arg = shift;
    if ($arg eq '--active') {
    $Word = Win32::OLE->GetActiveObject('Word.Application');
    $Doc = $Word->Documents->Item(1);
    }
    elsif ($arg eq '--open') {
    $Word = Win32::OLE->new('Word.Application', 'Quit');
    $Doc = $Word->Documents->Open('c:\temp\foo.doc');
    }

    : I obviously read more into the docs than was there, and I'm happy to
    : take correction. Where can I find a good orientation to thinking OLE?

    I don't know. Familiarity with the various forms of Visual Basic is
    helpful. Drafting a program in VBScript and running it with the Windows
    Script Host (cscript.exe) is often useful since you won't need to
    translate sample code into its Perl equivalent.

    : Now I'm trying to get at the highlighted portion of the document
    : associated with a given comment. Strike that:
    :
    : foreach my $c (in $Doc->{Comments}) {
    : my $tag = $c->{Initial} . $c->{Index};
    : print $tag, " - ", $c->{Range}{Text}, "\n";
    : print $c->{Scope}{Text}, "\n";
    : }

    That seems to work well. Does it need more tweaking?

    : : As for your original quick hack program, see if this does not fill the
    : : need.
    :
    [code snipped]
    :
    : That produced no output. (Yes, I used the appropriate path.)

    Just as well. You want the inline comments, not the document's Comments
    property. These aren't the droids you're looking for.

    : A couple of remaining questions:
    :
    : Why does iterating over the BuiltinDocumentProperties make the
    : application think I have changes that need saving? (I see that setting
    : the Saved property says nothing needs saving.)

    I don't know. Reasonably, a simple peek at an object's properties
    should not alter the object. It is quite a nuisance.

    : What the difference between getting at the document via Documents->Open
    : versus GetActiveObject? I'd like to avoid typing long paths.

    See above.

    Jay Tilton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139