Word Macro to Objrexx

  • Follow


I have a word macro I am trying to convert to Object Rexx.
The macro code is:

for each item in activedocument.tables
    if left(item.range,2)="AR" then
        call deleteTable(item)
        exit for
    end if
next item

I tried:

do item over wrdApp~tables
    if substr(item~range,1,2)='AR' then do
        call deleteTable(item)
        leave
        end
    end

I got a syntax error on the "do item over" line.


0
Reply jerry 11/19/2004 12:30:18 AM

Jerry:

> The full text is:
> 
> rc=97 <Object method not found>

There should be more information (with a subnumber to 97) available, which pinpoints at the problem. 
If you are intercepting exceptions yourself, look at the Condition()-BIF which allows you to get at 
the full message.

>>>I tried:
>>>
>>> myvar = item~range

What does "item~range~value" return?

Regards,

---rony
0
Reply rony 11/18/2004 5:17:50 PM


Marc Vincent Irvin wrote:
> What  is going on here?  The user has described one of the easiest 
> problems to handle in REXX.

In REXX: yes, in Object REXX: yes, but not in the context of driving Microsoft Word (with *any* 
scripting language). The truth is, all of Microsoft's Office applications are *very* complex to be 
scripted/automated/remote controlled. (Actually, the same can be said of Open Office.)

> This users problem does not need an ObjRexx kind of solution.  Why not 
> let him do what ObjRexx is good at -- plain REXX?

If automating Windows applications (i.e. OLE/ActiveX objects) then you have two choices: using 
"plain" Rexx and external functions to interface with the Windows OLE/ActiveX programs, or using 
Object Rexx employing the message operator "~" to send the Windows OLE/ActiveX programs the 
appropriate messages, which I think is the easiest possible interaction.

> Put  the words in a var or stem var (either way is clean) then do the 
> lookups using the WORDPOS() or PARSE commands.   What am I missing?   

You cannot interface with Windows (applications) this way.

> I'd rather code his whole program rather than have the user go away 
> thinking REXX is too much trouble, because of the obfuscatiive (new 
> word) nature of object code design.

This has nothing to do with "object code design", but with a problem the OLE/ActiveX interface 
poses. Jerry expects a return value of type string, but obviously does not get it. Now, it is 
interesting to find out, what he gets, hence the question of the full error message, because Object 
Rexx is rather good (many times *much* better then classic Rexx) in telling the cause of a problem.

Again, driving Word, Excel etc. is *not* an easy task at all. This has nothing to doe with Rexx or 
Object Rexx, but with Microsoft's extremely complicated interface, which takes quite some efforts to 
get accustomed to.

---rony

P.S.: See the other post about getting at all of the interesting information about exceptions, if 
you use Object Rexx instead of classic Rexx.

0
Reply rony 11/18/2004 11:25:52 PM

Hi Jerry,

> My syntax routine is:
> 
> Syntax:
>   Say 'Syntax raised in line' sigl
>   Say sourceline(sigl)
>   Say 'rc='rc '('errortext(rc)')'
>   say 'condition' condition()

I see, this doesn't tell a lot (just as of yet). May I suggest you to use the following exception 
subroutine (applicable for *all* exceptions, not only Syntax exceptions):

----------------- cut here -----------------
SIGNAL ON ANY		-- active exception andling, branch to the "ANY" label

	... your code goes here...

    -- exception handling code
any:
    d=condition("O")  -- get the exception object (a directory object)

    do index over d   -- iterates over the directory object
         /* show index and associated/stored item */
       say "--->" pp(index)~right(14)":" pp(d~at(index))
    end
    say

    say "Additional:" -- show additional exception information
    a=d~additional    -- get the entry with index "ADDITIONAL" (an array-object)
    do item over a    -- iterates over array object
       say "   " pp(item)  -- show stored item
    end
    say

    say "Traceback:"  -- show the traceback (list of Rexx statements)
    l=d~traceback     -- get the entry with index "TRACEBACK" (a list-object)
    do item over l    -- iterates over list object
       say "   " pp(item)  -- show stored item
    end
    exit -1
----------------- cut here -----------------

The above lists all information available for an exception. Here you can really get at everything 
available to Rexx.

Please give us the output of the directory entry "CODE" and the directory entry "MESSAGE", and the 
information in the "ADDITIONAL"-array (most of the time the same as MESSAGE) and "TRACEBACK"-list. 
This should tell a lot more about the error.

Regards,

---rony
0
Reply rony 11/18/2004 11:41:10 PM

Oops, forgot to show function pp(), which is just:

	 pp: return "[" || arg(1) || "]" /* put argument into square brackets */

Just put this after the end of the exception subroutine (after "exit -1").

Sorry,

---rony


> ----------------- cut here -----------------
> SIGNAL ON ANY        -- active exception andling, branch to the "ANY" label
> 
>     ... your code goes here...
> 
>    -- exception handling code
> any:
>    d=condition("O")  -- get the exception object (a directory object)
> 
>    do index over d   -- iterates over the directory object
>         /* show index and associated/stored item */
>       say "--->" pp(index)~right(14)":" pp(d~at(index))
>    end
>    say
> 
>    say "Additional:" -- show additional exception information
>    a=d~additional    -- get the entry with index "ADDITIONAL" (an array-object)
>    do item over a    -- iterates over array object
>       say "   " pp(item)  -- show stored item
>    end
>    say
> 
>    say "Traceback:"  -- show the traceback (list of Rexx statements)
>    l=d~traceback     -- get the entry with index "TRACEBACK" (a list-object)
>    do item over l    -- iterates over list object
>       say "   " pp(item)  -- show stored item
>    end
>    exit -1
> ----------------- cut here -----------------
> 
> The above lists all information available for an exception. Here you can 
> really get at everything available to Rexx.
> 
> Please give us the output of the directory entry "CODE" and the 
> directory entry "MESSAGE", and the information in the "ADDITIONAL"-array 
> (most of the time the same as MESSAGE) and "TRACEBACK"-list. This should 
> tell a lot more about the error.
> 
> Regards,
> 
> ---rony
0
Reply rony 11/18/2004 11:46:04 PM

Jerry:

> I got a syntax error on the "do item over" line.

Please give the full error message, as it will hint at the cause (speculating, that you do not get a 
collection of tables back indicated by .nil; this may happen, if there are no tables in your Word 
document, but to be sure you need to look up the Word documentation).

Also, "deleteTable()" is probably defined as a function/method in the activeDocument, so you need to 
send a message by that name to it, e.g. "activeDocument~deleteTable(item)'".

Also, extracting the first two characters from the left in a string can be done with the left() BIF 
in Rexx, e.g. "left(item~range, 2)" or in Object Rexx syntax "item~range~left(2)".

---rony
0
Reply rony 11/19/2004 7:47:51 AM

ActiveDocument is the current Word.Document, wdrApp is probably the 
Word.Application.

"jerry chapman" <jerryc314@sbcglobal.net> wrote in message 
news:u8bnd.23074$6q2.870@newssvr14.news.prodigy.com...
>I have a word macro I am trying to convert to Object Rexx.
> The macro code is:
>
> for each item in activedocument.tables
>    if left(item.range,2)="AR" then
>        call deleteTable(item)
>        exit for
>    end if
> next item
>
> I tried:
>
> do item over wrdApp~tables
>    if substr(item~range,1,2)='AR' then do
>        call deleteTable(item)
>        leave
>        end
>    end
>
> I got a syntax error on the "do item over" line.
>
> 


0
Reply Mark 11/19/2004 12:17:56 PM

On Fri, 19 Nov 2004 00:30:18 GMT, "jerry chapman"
<jerryc314@sbcglobal.net> wrote:

>I have a word macro I am trying to convert to Object Rexx.
>The macro code is:
>
>for each item in activedocument.tables
>    if left(item.range,2)="AR" then
>        call deleteTable(item)
>        exit for
>    end if
>next item
>
>I tried:
>
>do item over wrdApp~tables
>    if substr(item~range,1,2)='AR' then do
>        call deleteTable(item)
>        leave
>        end
>    end
>
>I got a syntax error on the "do item over" line.
>
Yes, you probably do.

Change your "do over" line to match the VBA "for each" line:

do item over activedocument~tables

0
Reply Lee 11/19/2004 12:54:23 PM

I changed wdrApp to wrddoc and the do item command worked, but the
item~range~left(2) command got a syntax error:
    rc=97 (object method not found)

do item over wrddoc~tables
    say item~range~left(2)

"Mark Yudkin" <myudkinATcompuserveDOTcom@boing.org> wrote in message
news:cnko8g$sk$1@ngspool-d02.news.aol.com...
> ActiveDocument is the current Word.Document, wdrApp is probably the
> Word.Application.
>
> "jerry chapman" <jerryc314@sbcglobal.net> wrote in message
> news:u8bnd.23074$6q2.870@newssvr14.news.prodigy.com...
> >I have a word macro I am trying to convert to Object Rexx.
> > The macro code is:
> >
> > for each item in activedocument.tables
> >    if left(item.range,2)="AR" then
> >        call deleteTable(item)
> >        exit for
> >    end if
> > next item
> >
> > I tried:
> >
> > do item over wrdApp~tables
> >    if substr(item~range,1,2)='AR' then do
> >        call deleteTable(item)
> >        leave
> >        end
> >    end
> >
> > I got a syntax error on the "do item over" line.
> >
> >
>
>


0
Reply jerry 11/19/2004 1:17:30 PM

Maybe someone more learned than I can explain why that doesn't work,
but in the mean time, try this.

myvar = item~range
if myvar~left(2) = ........



On Fri, 19 Nov 2004 13:17:30 GMT, "jerry chapman"
<jerryc314@sbcglobal.net> wrote:

>I changed wdrApp to wrddoc and the do item command worked, but the
>item~range~left(2) command got a syntax error:
>    rc=97 (object method not found)
>
>do item over wrddoc~tables
>    say item~range~left(2)
>
>


0
Reply Lee 11/19/2004 1:33:21 PM

I tried:

 myvar = item~range
 say myvar~left(2)

The second line got the same syntax error as below.

"Lee Peedin" <lee@DONOTSPAMMEsafedatausa.com> wrote in message
news:vdtrp0dagsso8990suugh9nt6uk75mkruj@4ax.com...
> Maybe someone more learned than I can explain why that doesn't work,
> but in the mean time, try this.
>
> myvar = item~range
> if myvar~left(2) = ........
>
>
>
> On Fri, 19 Nov 2004 13:17:30 GMT, "jerry chapman"
> <jerryc314@sbcglobal.net> wrote:
>
> >I changed wdrApp to wrddoc and the do item command worked, but the
> >item~range~left(2) command got a syntax error:
> >    rc=97 (object method not found)
> >
> >do item over wrddoc~tables
> >    say item~range~left(2)
> >
> >
>
>


0
Reply jerry 11/19/2004 11:37:53 PM

Jerry:

> I tried:
> 
>  myvar = item~range
>  say myvar~left(2)
> 
> The second line got the same syntax error as below.

What's the (full) error message?

Without it, it is hard and cumbersome to try to help as there are many possibilities leading to 
syntax errors, of which there are quite a few...

---rony
0
Reply rony 11/20/2004 7:17:07 AM

The full text is:

rc=97 <Object method not found>

"rony" <Rony.Flatscher@wu-wien.ac.at> wrote in message
news:cnmr1i$28u3$1@trane.wu-wien.ac.at...
> Jerry:
>
> > I tried:
> >
> >  myvar = item~range
> >  say myvar~left(2)
> >
> > The second line got the same syntax error as below.
>
> What's the (full) error message?
>
> Without it, it is hard and cumbersome to try to help as there are many
possibilities leading to
> syntax errors, of which there are quite a few...
>
> ---rony


0
Reply jerry 11/20/2004 3:38:11 PM

My syntax routine is:

Syntax:
  Say 'Syntax raised in line' sigl
  Say sourceline(sigl)
  Say 'rc='rc '('errortext(rc)')'
  say 'condition' condition()

The text from condition() is SIGNAL

"rony" <Rony.Flatscher@wu-wien.ac.at> wrote in message
news:cnnu7u$2h87$1@trane.wu-wien.ac.at...
> Jerry:
>
> > The full text is:
> >
> > rc=97 <Object method not found>
>
> There should be more information (with a subnumber to 97) available, which
pinpoints at the problem.
> If you are intercepting exceptions yourself, look at the Condition()-BIF
which allows you to get at
> the full message.
>
> >>>I tried:
> >>>
> >>> myvar = item~range
>
> What does "item~range~value" return?
>
> Regards,
>
> ---rony


0
Reply jerry 11/20/2004 7:59:43 PM

What  is going on here?  The user has described one of the easiest 
problems to handle in REXX. 

This users problem does not need an ObjRexx kind of solution.  Why not 
let him do what ObjRexx is good at -- plain REXX?

Put  the words in a var or stem var (either way is clean) then do the 
lookups using the WORDPOS() or PARSE commands.   What am I missing?   
I'd rather code his whole program rather than have the user go away 
thinking REXX is too much trouble, because of the obfuscatiive (new 
word) nature of object code design.
     

jerry chapman wrote:

>My syntax routine is:
>
>Syntax:
>  Say 'Syntax raised in line' sigl
>  Say sourceline(sigl)
>  Say 'rc='rc '('errortext(rc)')'
>  say 'condition' condition()
>
>The text from condition() is SIGNAL
>
>"rony" <Rony.Flatscher@wu-wien.ac.at> wrote in message
>news:cnnu7u$2h87$1@trane.wu-wien.ac.at...
>  
>
>>Jerry:
>>
>>    
>>
>>>The full text is:
>>>
>>>rc=97 <Object method not found>
>>>      
>>>
>>There should be more information (with a subnumber to 97) available, which
>>    
>>
>pinpoints at the problem.
>  
>
>>If you are intercepting exceptions yourself, look at the Condition()-BIF
>>    
>>
>which allows you to get at
>  
>
>>the full message.
>>
>>    
>>
>>>>>I tried:
>>>>>
>>>>>myvar = item~range
>>>>>          
>>>>>
>>What does "item~range~value" return?
>>
>>Regards,
>>
>>---rony
>>    
>>
>
>
>  
>

0
Reply Marc 11/20/2004 8:39:50 PM

Here is what I got:

--->  [INSTRUCTION]: [SIGNAL]
--->    [CONDITION]: [LOSTDIGITS]
--->  [DESCRIPTION]: [1073741823]
--->   [PROPAGATED]: [0]

Additional:
--->       [RESULT]: [The NIL object]
--->  [INSTRUCTION]: [SIGNAL]
--->      [PROGRAM]: [C:\Documents and Settings\jerryc\My Documents\My
Files\objrexx\ole\word1\updatetable.REX]
--->    [CONDITION]: [SYNTAX]
--->      [MESSAGE]: [Unable to convert object "The NIL object" to a
single-dimensional array value]
--->  [DESCRIPTION]: []
--->   [PROPAGATED]: [1]
--->   [ADDITIONAL]: [an Array]
--->     [POSITION]: [2077]
--->    [TRACEBACK]: [a List]
--->         [CODE]: [98.913]
--->       [SOURCE]: [The NIL object]
--->           [RC]: [98]
--->    [ERRORTEXT]: [Execution error]

Additional:
    [The NIL object]

Traceback:
    [  2077 *-*   do item over a    -- iterates over array object]

That was very helpful. I added numeric digits 10, and then got another
error:

--->       [RESULT]: [The NIL object]
--->  [INSTRUCTION]: [SIGNAL]
--->      [PROGRAM]: [C:\Documents and Settings\jerryc\My Documents\My
Files\objrexx\ole\word1\updatetable.REX]
--->    [CONDITION]: [SYNTAX]
--->      [MESSAGE]: [Object "an OLEOBJECT" does not understand message
"LEFT"]
--->  [DESCRIPTION]: []
--->   [PROPAGATED]: [0]
--->   [ADDITIONAL]: [an Array]
--->     [POSITION]: [20]
--->    [TRACEBACK]: [a List]
--->         [CODE]: [97.1]
--->       [SOURCE]: [The NIL object]
--->           [RC]: [97]
--->    [ERRORTEXT]: [Object method not found]

Additional:
    [an OLEOBJECT]
    [LEFT]

Traceback:
    [    20 *-*   say myvar~left(2)]

Since in this case I only have one table I commented out this check and
called my detete and add routines with the only "item" value, and everything
worked perfectly.

"rony" <Rony.Flatscher@wu-wien.ac.at> wrote in message
news:cnokvv$2nop$1@trane.wu-wien.ac.at...
> Oops, forgot to show function pp(), which is just:
>
> pp: return "[" || arg(1) || "]" /* put argument into square brackets */
>
> Just put this after the end of the exception subroutine (after "exit -1").
>
> Sorry,
>
> ---rony
>
>
> > ----------------- cut here -----------------
> > SIGNAL ON ANY        -- active exception andling, branch to the "ANY"
label
> >
> >     ... your code goes here...
> >
> >    -- exception handling code
> > any:
> >    d=condition("O")  -- get the exception object (a directory object)
> >
> >    do index over d   -- iterates over the directory object
> >         /* show index and associated/stored item */
> >       say "--->" pp(index)~right(14)":" pp(d~at(index))
> >    end
> >    say
> >
> >    say "Additional:" -- show additional exception information
> >    a=d~additional    -- get the entry with index "ADDITIONAL" (an
array-object)
> >    do item over a    -- iterates over array object
> >       say "   " pp(item)  -- show stored item
> >    end
> >    say
> >
> >    say "Traceback:"  -- show the traceback (list of Rexx statements)
> >    l=d~traceback     -- get the entry with index "TRACEBACK" (a
list-object)
> >    do item over l    -- iterates over list object
> >       say "   " pp(item)  -- show stored item
> >    end
> >    exit -1
> > ----------------- cut here -----------------
> >
> > The above lists all information available for an exception. Here you can
> > really get at everything available to Rexx.
> >
> > Please give us the output of the directory entry "CODE" and the
> > directory entry "MESSAGE", and the information in the "ADDITIONAL"-array
> > (most of the time the same as MESSAGE) and "TRACEBACK"-list. This should
> > tell a lot more about the error.
> >
> > Regards,
> >
> > ---rony


0
Reply jerry 11/21/2004 3:22:19 PM

Accordingly to the Word documentation, the Range object doesn't have a Left 
method. The OO-REXX Error 97 confirms that the documentation is correct.

In your original code: left(item.range,2)="AR", left is a (global) function 
application of the string object.

"jerry chapman" <jerryc314@sbcglobal.net> wrote in message 
news:Knmnd.23308$6q2.15946@newssvr14.news.prodigy.com...
>I changed wdrApp to wrddoc and the do item command worked, but the
> item~range~left(2) command got a syntax error:
>    rc=97 (object method not found)
>
> do item over wrddoc~tables
>    say item~range~left(2)
>
> "Mark Yudkin" <myudkinATcompuserveDOTcom@boing.org> wrote in message
> news:cnko8g$sk$1@ngspool-d02.news.aol.com...
>> ActiveDocument is the current Word.Document, wdrApp is probably the
>> Word.Application.
>>
>> "jerry chapman" <jerryc314@sbcglobal.net> wrote in message
>> news:u8bnd.23074$6q2.870@newssvr14.news.prodigy.com...
>> >I have a word macro I am trying to convert to Object Rexx.
>> > The macro code is:
>> >
>> > for each item in activedocument.tables
>> >    if left(item.range,2)="AR" then
>> >        call deleteTable(item)
>> >        exit for
>> >    end if
>> > next item
>> >
>> > I tried:
>> >
>> > do item over wrdApp~tables
>> >    if substr(item~range,1,2)='AR' then do
>> >        call deleteTable(item)
>> >        leave
>> >        end
>> >    end
>> >
>> > I got a syntax error on the "do item over" line.
>> >
>> >
>>
>>
>
> 


0
Reply Mark 11/21/2004 3:43:54 PM

> Since in this case I only have one table I commented out this check and
> called my detete and add routines with the only "item" value, and everything
> worked perfectly.

Congratulations!
:)

---rony

P.S.: It is sometimes very confusing, what Windows programs return. This has to do with the strange 
way "defaults" are defined and implemented (e.g. "default attribute", "default function/method", 
"default value", etc.). Instead of making coding/programming easier, these concepts many times make 
it harder, because they may create so much confusion. Therefore it is so important to learn what the 
exact cause of an error was, hence employing the condition-object.
0
Reply rony 11/21/2004 3:44:40 PM

I do have another word document that has multiple tables. So I would still
like to solve to problem with the Left operand.

"rony" <Rony.Flatscher@wu-wien.ac.at> wrote in message
news:cnqd5c$14j$1@trane.wu-wien.ac.at...
>
> > Since in this case I only have one table I commented out this check and
> > called my detete and add routines with the only "item" value, and
everything
> > worked perfectly.
>
> Congratulations!
> :)
>
> ---rony
>
> P.S.: It is sometimes very confusing, what Windows programs return. This
has to do with the strange
> way "defaults" are defined and implemented (e.g. "default attribute",
"default function/method",
> "default value", etc.). Instead of making coding/programming easier, these
concepts many times make
> it harder, because they may create so much confusion. Therefore it is so
important to learn what the
> exact cause of an error was, hence employing the condition-object.


0
Reply jerry 11/21/2004 4:15:40 PM

Jerry:

> I do have another word document that has multiple tables. So I would still
> like to solve to problem with the Left operand.

In the meantime I did research that aspect of MS Word a little bit, here are my coarse findings:

- the Word "range" object has a default method called "text", rendering all the cells into one 
string: hence, you need to send the "text" message to the range object to get the string 
represenation of all cells of a table (each cell has a trailing "0d07"x, the last cell in a row has 
two of these strings)

- if you address a specific cell in the table, then you need to get its "range" attribute (dubbed 
"property" in the MS-world) and then send it the "text" message

With these findings one can devise a little program which accepts the name of a Word document as 
argument and which iterates over all tables in that document, if there are any. Just take a look at 
the code and run the program, and I am sure you will be able to achieve whatever you wish:

-------------- cut here ---------------
/* demo accessing tables in MS Word */
parse arg fn .
if fn="" then fn="test1.doc"
fn=stream(fn, "c", "query exists")  -- get fully qualified path

wrdApp =.oleObject~new('Word.Application')   -- create an instance of MS Word
wrdApp~visible=.true       -- make document visible

wrdDoc = wrdApp~Documents~Open(fn)  -- load a Word document

tables=wrdDoc~tables       -- get tables if any
if tables~count <> 0 then  -- if any tables
do
    i=0
    say "# of tables in document:" tables~count
    do table over tables
       i=i+1
       say "table #" i":" table
       -- each cell's text has the trailing characters "0d07"x (CR, BELL)
       say "      table~range~text~left(2):" "["table~range~text~left(2)"]"
       say "                    range~text:" pp(table~range~text)
       say "                range~text~c2x:" pp(table~range~text~c2x)
       say
       cell=table~range~cells(1)  -- get first cell in range
       say "range~cell(1)~range~text~left(2):" "["cell~range~text~left(2)"]"
       say "  range~cell(1)~range~text:      " pp(cell~range~text)
       say "  range~cell(1)~range~text~c2x:  " pp(cell~range~text~c2x)
       say
       say "retrieving row=3, column=2:"

       say "   " pp(table~cell(3,2)~range~text)
       say "---"
    end
end
else
do
    say "No tables in Word document!"
end

wdDoNotSaveChanges=0             -- Word constant
wdSaveChanges=-1                 -- Word constant
wrdDoc~close(wdDoNotSaveChanges) -- close document
wrdApp~quit                -- quit application
exit

::routine edit -- replace "0d07"x
   tmp=changestr("0d070d07"x, arg(1), "0d0a"x)   -- next row, hence CR-LF
   tmp=changestr("0d07"x, tmp, "09"x)            -- next cell, hence TAB
   return tmp~strip("T", "09"x)                  -- strip trailing TAB

::routine pp   -- enclose in square brackets
   return "[" || edit(arg(1)) || "]"
-------------- cut here ---------------

Hope that helps,

---rony
0
Reply rony 11/21/2004 9:26:38 PM

19 Replies
270 Views

(page loaded in 0.165 seconds)

Similiar Articles:







7/23/2012 4:02:43 PM


Reply: