trouble creating MS Word fields with applescript

  • Follow


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. <br>
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. <br><br>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. <br><br>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. <br><br>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. <br><br>tell application &quot;Microsoft Word&quot; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;collapse range text object of selection direction collapse start <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set myrange to text object of selection <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set insertionPoint to start of content of myrange --save this point <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set myField to make new field at character insertionPoint of active document with properties � <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{text range:myrange, field type:field addin, field code:&quot;ADDIN  {{the original field code}}&quot;, show codes:true, result range:&quot;{{first setting of the result range}}&quot;} <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display dialog &quot;check the field&quot; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set content of (field code of field 1 of active document) � <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to &quot; ADDIN {{The field code}} &quot; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set content of (result range of field 1 of active document) � <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to &quot;{{the result range reset}} &quot; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
end tell <br><br>Thanks for taking a look <br>
Steve
0
Reply spaschke 5/3/2010 3:37:19 PM

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%28office.10%29.aspx

The definitive documentation for the Field object is in the MSDN Library:
http://msdn.microsoft.com/en-us/library/aa221460%28office.11%29.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


On 4/05/10 1:37 AM, in article 59bb7d97.-1@webcrossing.JaKIaxP2ac0,
"spaschke@officeformac.com" <spaschke@officeformac.com> wrote:

> 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:john@mcghie.name


0
Reply John 5/4/2010 10:40:27 AM


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.
0
Reply spaschke 5/4/2010 1:02:32 PM

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


On 4/05/10 11:02 PM, in article 59bb7d97.1@webcrossing.JaKIaxP2ac0,
"spaschke@officeformac.com" <spaschke@officeformac.com> wrote:

> 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:john@mcghie.name


0
Reply John 5/4/2010 9:12:47 PM

3 Replies
340 Views

(page loaded in 0.102 seconds)

Similiar Articles:
















7/24/2012 2:06:21 PM


Reply: