Word automation, etc.

W

wilsond

Hi all,

I've been tasked with writing a transcription program. I have to use Word. I
have some questions about programming in Word. I've done a bit of automation
in the past. The basics are that the application will be a fat client
residing on the users machine. The app starts with a login screen. The user
types in a user name/password and clicks a button. The username and password
are submitted to a webserver for authentication and profile retrieval. Upon
successful logon, the app will start an instance of Word. There will be a
custom toolbar with all the buttons the user will need to request new jobs,
etc. All data needs to be transferred back and forth through an SSL
connection to the web server. Will be utilizing Word 2003. I apologize in
advance for my lack of knowledge in Word terminology. Anyhoo...

Toolbars. Can I create a custom toolbar (with my own custom actions that
will be performed upon clicking) that will only be available (visible?) under
certain circumstances. Say, perhaps, when a certain template was loaded? I
understand I can put the toolbars in a template document ( .dot file). Is
this correct? I don't particularly want to do that. There will be a "generic"
template but there will also be at least 4 different templates for each
client. The templates can not be stored on the local drive either (they must
be "fed" to the application upon user request). I was thinking an Add-in
here. But, I don't want the toolbar to load if say, the user starts Word the
"normal" way to type a letter to grandma, etc.

Can I load a document template from a url? Or I guess maybe the correct
question would be: Can I create a new document from a template that is stored
on a webserver? I need to do this without
"prompting" the user at all. The template, I suppose, could be saved
temporarily on the users hard-drive and then loaded from the hard-drive. I
would like to load it from the url and not have to save it to the hard-drive
first.

How can I save a document back to a web server? I cannot save it back to a
drive letter:\path because there won't be any mapped network shares. I cannot
allow the user to save the document on their local hard-drive either. I do
not want to "prompt" the user.

The document templates will have "fields" or "tags". For example, lets say
there are 5 tags: tag1, tag2, ...tag5. Let's say the cursor is in between
tag2 and tag3. Is there anyway I can capture the "tab" key such that when the
user presses it, it tabs between tags? For instance, say the cursor is in the
above mentioned place, the user presses the tab key and the cursor moves to
the "start" of the next tag (tag3). If the user presses "shift + tab" the
cursor moves back to the previous tag (tag1).

That's all I have for now. I'm sure I'll be back. Thanks a bunch for any
suggestions, hints.

David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?d2lsc29uZA==?=,

I think the first thing we have to know is, which version of Word are you
targeting?

Then, the programming environment in which you'll be working?

More inline...
Toolbars. Can I create a custom toolbar (with my own custom actions that
will be performed upon clicking) that will only be available (visible?) under
certain circumstances. Say, perhaps, when a certain template was loaded? I
understand I can put the toolbars in a template document ( .dot file). Is
this correct? I don't particularly want to do that. There will be a "generic"
template but there will also be at least 4 different templates for each
client. The templates can not be stored on the local drive either (they must
be "fed" to the application upon user request). I was thinking an Add-in
here. But, I don't want the toolbar to load if say, the user starts Word the
"normal" way to type a letter to grandma, etc.
Traditionally, if you want tools to only be available in certain documents (or
groups of documents) you place the toolbar there. In a template from which the
documents are going to be created, for example. WHY would you not want to do
that? Is it because the tools are common to all the templates?

You can use an Addin (either the COM or template kind). However, the Addins
"visibility" would have to be controlled by Events (such as DocumentChange).
What you might do would be to have a "central" Addin that's always loaded. The
user requests a document type through it, and the toolbars are created, on the
fly, in the document, linked to events in the Addin. If you create them with the
Temporary parameter set to "True", they'll disappear when the document is
closed.
Can I load a document template from a url? Or I guess maybe the correct
question would be: Can I create a new document from a template that is stored
on a webserver? I need to do this without
"prompting" the user at all. The template, I suppose, could be saved
temporarily on the users hard-drive and then loaded from the hard-drive. I
would like to load it from the url and not have to save it to the hard-drive
first.
Default action when clicking a URL is to open that file. There's no way a URL
can create a new document; this would have to be automation code. (If we're
talking Word 2003 there are other possibilities)

If the template isn't going to provide any tools (everything in the Addin), you
could have the URL simply link to a document with all the necessary
boiler-plate.
How can I save a document back to a web server? I cannot save it back to a
drive letter:\path because there won't be any mapped network shares. I cannot
allow the user to save the document on their local hard-drive either. I do
not want to "prompt" the user.
Let's hope your target version is Word 2003... Although most recent versions of
Word do support saving to "http" targets. Have you tested saving back to a web
server, yet, using File/Save As?
The document templates will have "fields" or "tags". For example, lets say
there are 5 tags: tag1, tag2, ...tag5. Let's say the cursor is in between
tag2 and tag3. Is there anyway I can capture the "tab" key such that when the
user presses it, it tabs between tags? For instance, say the cursor is in the
above mentioned place, the user presses the tab key and the cursor moves to
the "start" of the next tag (tag3). If the user presses "shift + tab" the
cursor moves back to the previous tag (tag1).
Word only supports this with formfields (see the Forms toolbar) and ActiveX
controls (see the Controls toolbox) with form field protection in force.

Using KeyBindings you can reassign most key presses, but only to VBA code within
the current context (a macro in the template). You can use a callback to make it
execute code in a DLL, but that callback still has to be in a local VBA project.

A possibility could be to use "orphaned" macrobutton fields and have the users
press F11 to move from field to field (Shift+F11 to move backwards).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
W

wilsond

Hi Cindy,

Sorry, I cut and pasted this post and I left off the Word version. We will
be using Word 2003. The development environment is .NET (2003) and C#.

Yes, the tools will be common to all templates, only the layout of the
templates will be different. I'm not up on my Word terminology so I may be
using the wrong word here.

I like the idea of a "central" Addin...

The question I posed about loading a template from a URL - the action will
be the user clicks a button on a custom made toolbar to "request" a new job.
What I was hoping I could do is submit the request to a webserver and the
webserver send the template (or something to this effect. Maybe use file|open
functionality or LoadDocument?) .

I haven't tried "save as" back to the webserver yet. I will try it...

I have to check with the super on the tab key issue. I don't know if she
will allow me to use anything other than tab. If that's the case, I'll
probably go the keybindings/macro route...

Thank you so much for your input. You have no idea how much I appreciate it!

David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?d2lsc29uZA==?=,
We will
be using Word 2003. The development environment is .NET (2003) and C#.
OK, this opens up additional possibilities for you. One is XML. You could,
for example, dump all the document content and formatting into a "blank"
document in the form of XML. This won't carry across everything (some of
the page layout stuff, for example), but it would certainly be faster than
automating the creation of a document from scratch. (This in answer to
creating a new document from a URL, via d/l from a Web Server).

You can also extract the content of a document the same way. Or, if you
don't need the entire document, just certain parts, you can parse the XML
and extract only what you need.
I have to check with the super on the tab key issue. I don't know if she
will allow me to use anything other than tab. If that's the case, I'll
probably go the keybindings/macro route...
Word 2003 provides a special kind of document protection: you can
lock/unlock specific document ranges for editing. There's a task pane that
lets the user move from editable area to editable area.

There's also the possibility of protecting a document as a form and using
Form Fields (then you can TAB). And there are ActiveX controls that can be
used with forms protection. The biggest issue with forms protection comes
if the user needs to do more than just enter text content. Formatting,
headers, footers and graphics (basically) are locked out with forms
protection active.

What might be very interesting for you would be VSTO 2005 (for that, you
need VS 2005), where you can use data-caching, create custom task panes and
provide a wide range of controls (including Windows controls you create
yourself).

SmartDocument solutions might also be something for you to consider
(especially if VSTO 2005 isn't an option). There you place XML tags in your
document. You then define what should appear in a special task pane to aid
the user with the expected in-put. (Including displaying lists from
databases, and even sending data back through the list.)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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 :)
 
W

wilsond

Hi Cindy,

Thanks so much for your input. I appreciate it!

Off the current subject, I was having a bit of trouble closing out the word
instance. I don't understand why it didn't work the way I had it and after a
modification to the code it does work now. Basically, when the application
starts it creates an instance of word. If I then quit the application before
a document was loaded, the word instance wouldn't die. If a document is
loaded first then the app is quit, the word instance goes away.

Anyway, here is the previous code (that wouldn't kill the word instance):

public void CloseControl()
{
object dummy = System.Reflection.Missing.Value;
object dummy2 = (object)false;

try
{
document.Close( ref dummy2, ref dummy, ref dummy );
wd.Quit( ref dummy2, ref dummy, ref dummy );
}
catch {}
}

This would give the "The Object invoked has disconnected from the clients"
error when I would quit the app without loading a document.

When I changed the code such that the .Close and .Quit methods were in
separate try/catch blocks, the instance of word is killed and I don't get the
error.

public void CloseControl()
{
object dummy = System.Reflection.Missing.Value;
object dummy2 = (object)false;

try
{
document.Close( ref dummy2, ref dummy, ref dummy );
}
catch {}

try
{
wd.Quit( ref dummy2, ref dummy, ref dummy );
}
catch{}
}

Any ideas why this happens?

David
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?d2lsc29uZA==?=,

Interesting. Personally, I wouldn't try to manipulate an uninstantiated document
object. For one thing, invoking the error handler would slow things down; it
would be faster (and more correct) to test if (wdapp.Documents.Count >0) and
only close the document if that evaluates to true.

Keep in mind: If document.close throws an error, your code will jump straight to
the catch. That's why the Word instance isn't being ended. You could conceivably
do this (pseudocode), although it would still not be as efficient:

try
document.close
catch
finally
wdapp.Quit
Off the current subject, I was having a bit of trouble closing out the word
instance. I don't understand why it didn't work the way I had it and after a
modification to the code it does work now. Basically, when the application
starts it creates an instance of word. If I then quit the application before
a document was loaded, the word instance wouldn't die. If a document is
loaded first then the app is quit, the word instance goes away.

Anyway, here is the previous code (that wouldn't kill the word instance):

public void CloseControl()
{
object dummy = System.Reflection.Missing.Value;
object dummy2 = (object)false;

try
{
document.Close( ref dummy2, ref dummy, ref dummy );
wd.Quit( ref dummy2, ref dummy, ref dummy );
}
catch {}
}

This would give the "The Object invoked has disconnected from the clients"
error when I would quit the app without loading a document.

When I changed the code such that the .Close and .Quit methods were in
separate try/catch blocks, the instance of word is killed and I don't get the
error.

public void CloseControl()
{
object dummy = System.Reflection.Missing.Value;
object dummy2 = (object)false;

try
{
document.Close( ref dummy2, ref dummy, ref dummy );
}
catch {}

try
{
wd.Quit( ref dummy2, ref dummy, ref dummy );
}
catch{}
}

Any ideas why this happens?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
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