trouble creating MS Word fields with applescript

S

spaschke

Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Power PC I have here a sample script that creates a field in a word document. At the creation of the field, according to the little documentation I could find, you can set the properties of the field, including field code, result range and field text.
1. The field text is not supposed to show. - it does. It shows as part of the field code. I did not include this in my sample. I don't really need it anyway.

2. In the statement creating the field, the field code doesn't get set as you can see by running the script. I have to set it later.

3. The result range also doesn't get set at field creation. Setting the result range later also does NOT put it in the field like Word of Windows does, it puts it as static text in the document.

I really need the result range to work. Am I doing something wrong here? I have tried it many many ways and can't get it to work as documented.

tell application "Microsoft Word"
        collapse range text object of selection direction collapse start
        set myrange to text object of selection
        set insertionPoint to start of content of myrange --save this point
        set myField to make new field at character insertionPoint of active document with properties ¬
                {text range:myrange, field type:field addin, field code:"ADDIN {{the original field code}}", show codes:true, result range:"{{first setting of the result range}}"}
         
        display dialog "check the field"
         
        set content of (field code of field 1 of active document) ¬
                to " ADDIN {{The field code}} "
         
        set content of (result range of field 1 of active document) ¬
                to "{{the result range reset}} "
         
         
end tell

Thanks for taking a look
Steve
 
J

John McGhie

Hi Steve:

Well, I don't know AppleScript, however, Field CODE and Field RESULT should
be mutually exclusive. If you set the code, it will produce a result. If
you set the result, you should wipe out the field.

There's a great "get you started" article by Word MVP Cindy Meister here:
http://msdn.microsoft.com/en-us/library/aa163918(office.10).aspx

The definitive documentation for the Field object is in the MSDN Library:
http://msdn.microsoft.com/en-us/library/aa221460(office.11).aspx

The Field Range, the Code, and the Result are all "ranges". Change one and
you change all three, they're not separate.

The Field "code" represents anything between but not including the curly
braces. The field "Result" is the range that contains the displayed result
of the field code.

I suggest that it's highly risky to use curly braces in the literals for
fields :)

Hope this helps


Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Power PC I
have here a sample script that creates a field in a word document. At the
creation of the field, according to the little documentation I could find,
you can set the properties of the field, including field code, result range
and field text.
1. The field text is not supposed to show. - it does. It shows as part of
the field code. I did not include this in my sample. I don't really need it
anyway.

2. In the statement creating the field, the field code doesn't get set as
you can see by running the script. I have to set it later.

3. The result range also doesn't get set at field creation. Setting the
result range later also does NOT put it in the field like Word of Windows
does, it puts it as static text in the document.

I really need the result range to work. Am I doing something wrong here? I
have tried it many many ways and can't get it to work as documented.

tell application "Microsoft Word"
collapse range text object of selection direction collapse start
set myrange to text object of selection
set insertionPoint to start of content of myrange --save this point
set myField to make new field at character insertionPoint of active
document with properties ¬
{text range:myrange, field type:field addin, field code:"ADDIN
{{the original field code}}", show codes:true, result range:"{{first setting
of the result range}}"}

display dialog "check the field"

set content of (field code of field 1 of active document) ¬
to " ADDIN {{The field code}} "

set content of (result range of field 1 of active document) ¬
to "{{the result range reset}} "


end tell

Thanks for taking a look
Steve

--

The email below is my business email -- Please do not email me about forum
matters unless I ask you to; or unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410 | mailto:[email protected]
 
S

spaschke

Thank You John, you have dispelled a misconception I had. However, When I set the code in the Mac Word version, the result is unaffected. That is the problem. Keep in mind I am dealing only with ADDIN fields, which work a little different from others in some cases. But I was under the impression the result should have a value when I set the code with more than the field type. Also, If I set the result to a string, the code would be affected. But that is not the case with word 2008.
 
J

John McGhie

Ah hah! No... When you get into coding this, you need to know that there
are THREE "kinds" of fields:

HOT fields update instantly and dynamically. Change the Code and the result
updates in real time.

WARM fields update only at defined document events (usually: File>Open...)

COLD fields do not update until you explicitly update them.

Can you discover which is which? No! It's not documented.

Given that "ADDIN" is a field object that you are creating, you can be
fairly sure it's a COLD field. Whether there will be any result at all
would depend on the code you have written.

(I.e.: This is not a Word field, it's yours, it will do whatever you coded
it to do... )

Hope this helps


Thank You John, you have dispelled a misconception I had. However, When I
set the code in the Mac Word version, the result is unaffected. That is the
problem. Keep in mind I am dealing only with ADDIN fields, which work a
little different from others in some cases. But I was under the impression
the result should have a value when I set the code with more than the field
type. Also, If I set the result to a string, the code would be affected. But
that is not the case with word 2008.

--

The email below is my business email -- Please do not email me about forum
matters unless I ask you to; or unless you intend to pay!

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410 | mailto:[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top