Word Event Model and "Update Fields"

S

Scott Numbers

Hello,

I am currently developing a Word Add-In using Visual Studio 2005 (Not
using VSTO) that creates and manages custom fields (Field Type:
wdFieldAddIn) within a Word document. As part of the user interface in
Word, the user can select one or more fields in the document and press
F9 to have the fields update themselves. Here is the problem I am
having: I am unable to locate any events within the Word Application
or Document interfaces that can be used to capture an event for
"Update Fields" or an F9 keypress/keydown/keyup. I need to be able to
update my custom fields when the F9 key is pressed and my fields are a
part of the selection.

I am not new to programmimg with Microsoft products but I am
relatively inexperienced with Office development/integration.
Everytime I have worked with Excel or Word in the past, I have felt
like the Events supported are woefully sparse. I am hoping that I am
missing some key concept that is used with the Office Object Models
that obscures these events.

I have searched through Google Groups with no luck. I have purchased
several books on Word and Visual Studio and none cover anything
besides the simple Document Open, Save and Close events. I can't
imagine that Microsoft would expect developers to write comprehensive
Add-Ins with these few Events. What am I missing?

Anyway, I would appreciate any information on this subject and if you
have any suggestions on good books on the subject of developing MS
Word Add-Ins And/Or any books on how to go about creating and
supporting a new "Field" type that would be displayed in the list
provided when the user selects "Insert / Field" from the menu.

Thank you in advance,
Scott Numbers
Scott.Numbers @ hq.doe.gov
 
D

Doug Robbins - Word MVP

In Word VBA, if you have a macro named Sub UpdateFields() that macro will
run in place of the Update Fields routine when the F9 button is pressed.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

Cindy M.

Hi Scott,

Supplementing Doug's reply...
I have searched through Google Groups with no luck. I have purchased
several books on Word and Visual Studio and none cover anything
besides the simple Document Open, Save and Close events. I can't
imagine that Microsoft would expect developers to write comprehensive
Add-Ins with these few Events. What am I missing?
Nothing, really. The Office applications truly do not have many events,
and the few there are are not very "fine-grained".
Anyway, I would appreciate any information on this subject and if you
have any suggestions on good books on the subject of developing MS
Word Add-Ins And/Or any books on how to go about creating and
supporting a new "Field" type that would be displayed in the list
provided when the user selects "Insert / Field" from the menu.
There's very little that goes into the depth you're interested in. The
Carter/Lippert book on Visual Studio Tools for Office probably has the
greatest detail at the level you're working at as far as events go.

As to fields, I know of nothing. Creating your own fields is not
something that's really supported or, in this day-and-age of viruses,
desired. The Addin field is more "legacy" than anything else. A
colleague, Peter Jamieson, has done some research into the
possibilities and his reports were rather pessimistic. But I'm sure
he'd be interested in discussing it with you - you should be able to
find him in the word.mailmerge.fields newsgroup. As best I can
remember, he tried leveraging the IncludeText fields together with a
file converter DLL. When the field code updated the file converter was
called and code in that processed the content. But there were security
concerns...

From what I understand, currently the Content Controls, in conjunction
with Custom XML Parts, are the recommended way for providing dynamic
content and interacting with users.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
S

Scott Numbers

Hi Scott,

Supplementing Doug's reply...


Nothing, really. The Office applications truly do not have many events,
and the few there are are not very "fine-grained".


There's very little that goes into the depth you're interested in. The
Carter/Lippert book on Visual Studio Tools for Office probably has the
greatest detail at the level you're working at as far as events go.

As to fields, I know of nothing. Creating your own fields is not
something that's really supported or, in this day-and-age of viruses,
desired. The Addin field is more "legacy" than anything else. A
colleague, Peter Jamieson, has done some research into the
possibilities and his reports were rather pessimistic. But I'm sure
he'd be interested in discussing it with you - you should be able to
find him in the word.mailmerge.fields newsgroup. As best I can
remember, he tried leveraging the IncludeText fields together with a
file converter DLL. When the field code updated the file converter was
called and code in that processed the content. But there were security
concerns...

From what I understand, currently the Content Controls, in conjunction
with Custom XML Parts, are the recommended way for providing dynamic
content and interacting with users.

Cindy Meister
INTER-Solutions, Switzerlandhttp://homepage.swissonline.ch/cindymeister(last update Jun 17 2005)http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)

Doug, Cindy;
Thanks for the quick replies.

I had not known that fact about the macros, but did stumble across the
"EditPaste" example in this group yesterday, Like I said before, I do
not have much experience with MS Word Macro programming, but aren't
there security implications when deploying a solution with macros? I
am in a government environment and the powers-that-be are always
implementing tighter security policies. I will need to research the
security implications of a macro solution.

I also found that if in my OnConnection code for the AddIn, I set the
"Object" property for my AddIn within the WordApp.COMAddIns
collection, I could then call my managed code from the macro. This is
necessary because I want all of the logic for the event to ba handled
in the managed code of the AddIn. I included relevant snippets from my
code below. This will invoke my Managed AddIn code when the F9 key is
pressed in Word. The problems I see is making sure that the Word
documents will have my version of "UpdateFields" macro available to
them and any security implications that this might have. I need to do
more research on best practices on implementing macros in secure
environments.

As for the book, I already have the Carter/Lippert book and it did not
have anything beyond the basic events in a COM AddIn and the ones (a
few more than COM) supported by VSTO. NO tricks on going deeper.

And the XML... Since the target audience will have Word 2003 or
higher, I could go the XML route, but from what I can see, that is a
steeper learning curve with probably fewer books and resources. But I
will look into it because I will want a solution that will go for many
years to come.

And for others researching this subject in the future, I found this
article about creating Excel functions in Managed code to be
interesting.

http://blogs.msdn.com/pstubbs/archive/2004/12/31/344964.aspx

This is a fairly clean/elegant implementation but it requires macros
as well. I was hoping for a solution not requiring macros because of
security issues.

Thank you again for your replies, and if anybody has any further ideas
or tips on this and or macro best practices, I would greatly
appreciate them.
Scott Numbers

In Visual Studio AddIn Code

Public Sub OnConnection(ByVal application As Object, ByVal
connectMode As Extensibility.ext_ConnectMode, _
ByVal addInInst As Object, ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnConnection

WordApp = application
' Set a reference to me in word app
wApp.COMAddIns.Item("MyAddIn.Connect").Object = Me
...
End Sub

Public Sub UpdateFields()

MessageBox.Show("UpdateFields Invoked")
' TODO: Add code to handle my custom Fields in the selection

End Sub


In Word Document VBA Code

Public Sub UpdateFields()
Application.COMAddIns("MyAddIn.Connect").Object.UpdateFields
End
 
C

Cindy M.

Hi Scott,
aren't
there security implications when deploying a solution with macros? I
am in a government environment and the powers-that-be are always
implementing tighter security policies.
The security "issues" with macros is no greater than with any other
kind of code. It's more a question of the security settings used by
the particular environment. The office applications (2003) have four
basic macro security types:
- Very High: No code execution allowed (not even add-ins)
- High: Execution only allowed if code is signed with a
certificate by a trusted publisher
- Middle: Manually allowed execution
- Low: all code execution allowed

In 2007 the "Middle" category no longer exists.

Exceptions to the above settings (with the exception of "Very High")
are allowed if a template containing VBA code is located in a
"Trusted location". In 2003, these are set; 2007 provides additional
flexibility.

In Word 2003, look in Tools/Macro/Macro security. In 2007 the "Word
Options"/Trust Center/Trust Center Settings.

So I imagine you could include VBA code in a template if you decide
its critical to your application. This is the *only* way to achieve a
granularity of trapping when something happens in Word. You'd
digitally sign the template VBA project, same as you do your Add-in.
Your add-in would take care of loading and unloading the template (as
a template Add-in - the Addins collection of the Application object).
I also found that if in my OnConnection code for the AddIn, I set the
"Object" property for my AddIn within the WordApp.COMAddIns
collection, I could then call my managed code from the macro.
Interesting. I wasn't aware of that. Thanks for posting the code :)
The problems I see is making sure that the Word
documents will have my version of "UpdateFields" macro available to
them
You can do that with the "template add-in" approach outlined above.
As for the book, I already have the Carter/Lippert book and it did not
have anything beyond the basic events in a COM AddIn and the ones (a
few more than COM) supported by VSTO. NO tricks on going deeper.
I didn't say it had any tricks that went deeper - there aren't any. I
only meant that it has more in-depth information on working with the
events than any other documentation or books I've encountered.
And the XML... Since the target audience will have Word 2003 or
higher, I could go the XML route, but from what I can see, that is a
steeper learning curve with probably fewer books and resources. But I
will look into it because I will want a solution that will go for many
years to come.
Content Controls aren't available until 2007, so as long as you're at
the 2003 application level, it's not going to help much.
And for others researching this subject in the future, I found this
article about creating Excel functions in Managed code to be
interesting.

http://blogs.msdn.com/pstubbs/archive/2004/12/31/344964.aspx
Mmm, apparently the msdn servers are having some problems "this
morning" (European time). I'm just getting a Web configurator log-in
when I try to access the site <sigh>

However, if we're talking UDF it can be done *without* needing any
VBA code, by creating and registering a special type of COM Add-in.
Eric Carter describes it in his book (and his blog).

Another good resource, if you don't have it already, is Andrew
Whitechapel's book ".NET Development for Microsoft Office".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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