newbie to word automation looking for samples

G

Guest

Anybody know where to find some code or script examples for automating Word?
Basically I'm interested in creating a custom mail-merge like application in
c#. I have a custom .doc or .dot file, set up like a form (that a customer
will fill out on paper after its been printed), and I want to add text in
one particular section of the form, either a field or addressblock (or
other, not sure what to do). It would be nice for some c# samples, but any
code or script in any language will be greatly appreciated.
Thanks
gv
 
C

Cindy M -WordMVP-

Anybody know where to find some code or script examples for automating Word?
Basically I'm interested in creating a custom mail-merge like application in
c#. I have a custom .doc or .dot file, set up like a form (that a customer
will fill out on paper after its been printed), and I want to add text in
one particular section of the form, either a field or addressblock (or
other, not sure what to do). It would be nice for some c# samples, but any
code or script in any language will be greatly appreciated.
Have you been looking at Word's form fields (Forms toolbar)? These are what
I'd use, based on what you've told us.

The VBA syntax to put anything in a text form field is (in VBA-speak):
doc.Formfields("TheName").Result = szMyString

The only thing to watch out for is that you can pass a max. of 255 characters
using this method.

Form fields can be positioned in tables or frames if you need any text wrap
formatting. You find much more on how to create such forms at mvps.org/word,
with links to various sites.

Another possibility, since you really want to fill in only the one piece of
information before printing? Would be to Insert/Bookmark and simply write the
text to the bookmark
doc.Bookmarks("TheName").Range.Text = szMyString

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

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

Guest

Either option should work, but I need to open the file. And the
Documents.Open method wants many parameters that are not passed in all the
vb samples.
Here is the description from the object browser
public abstract virtual Word.Document Open ( System.Object FileName ,
System.Object ConfirmConversions , System.Object ReadOnly , System.Object
AddToRecentFiles , System.Object PasswordDocument , System.Object
PasswordTemplate , System.Object Revert , System.Object
WritePasswordDocument , System.Object WritePasswordTemplate , System.Object
Format , System.Object Encoding , System.Object Visible , System.Object
OpenAndRepair , System.Object DocumentDirection , System.Object
NoEncodingDialog )
Member of Word.Documents

And when you look at the member window it shows that each parameter are
reference objects. And I can't figure out what it wants for paramters.

Here is my code
Word.Application wordApp = new Word.Application();
Word.Document wordDoc;
wordDoc = wordApp.Documents.Open(fileName,false, false, true, "", "", false,
"", "", "", "", true, "", "", "");

I realize that the .Open method is not passing reference object types, any
idea on what it wants for parameters? Am I using the correct classes above?

Thanks

gv
 
J

Jonathan West

Either option should work, but I need to open the file. And the
Documents.Open method wants many parameters that are not passed in all the
vb samples.
Here is the description from the object browser
public abstract virtual Word.Document Open ( System.Object FileName ,
System.Object ConfirmConversions , System.Object ReadOnly , System.Object
AddToRecentFiles , System.Object PasswordDocument , System.Object
PasswordTemplate , System.Object Revert , System.Object
WritePasswordDocument , System.Object WritePasswordTemplate , System.Object
Format , System.Object Encoding , System.Object Visible , System.Object
OpenAndRepair , System.Object DocumentDirection , System.Object
NoEncodingDialog )
Member of Word.Documents

And when you look at the member window it shows that each parameter are
reference objects. And I can't figure out what it wants for paramters.

Here is my code
Word.Application wordApp = new Word.Application();
Word.Document wordDoc;
wordDoc = wordApp.Documents.Open(fileName,false, false, true, "", "", false,
"", "", "", "", true, "", "", "");

I realize that the .Open method is not passing reference object types, any
idea on what it wants for parameters? Am I using the correct classes
above?

I think C# allows you to use names arguments in a method call. If you do use
named arguments, then you simply omit the optional arguments that you don't
want to set. They will get given default values as described in the Word VBA
help file.

if you can't or don't want to use named arguments, then you need a means of
communicating that you are not passing a value to that argument. I don't
know the syntax for that in C#, but for VBScript, the trick is to have
nothing between the commas, like this

wordDoc = wordApp.Documents.Open(fileName,false, false, true, , , false, , ,
, , true, , , )
 
C

Cindy M -WordMVP-

Ah. It does help to mention the specific problem :) Have you tried searching
the Knowledge Base in the msdn library at microsoft.com? And looked through
what's offered on the dotnet website?

One sample article I turned up is HOWTO: Automate Word to Perform a Mail
Merge from C# .NET [301659]

The data types for the parameters you'll find in the Word help for a
particular method. If you're not getting into these Help files from within
the NET environment, open Word, press Alt+F11, type the phrase in question,
then press F1 to open the Help files.

FileName is required, of type String, ConfirmConversions is optional, type
Boolean, etc.
Either option should work, but I need to open the file. And the
Documents.Open method wants many parameters that are not passed in all the
vb samples.
Here is the description from the object browser
public abstract virtual Word.Document Open ( System.Object FileName ,
System.Object ConfirmConversions , System.Object ReadOnly , System.Object
AddToRecentFiles , System.Object PasswordDocument , System.Object
PasswordTemplate , System.Object Revert , System.Object
WritePasswordDocument , System.Object WritePasswordTemplate , System.Object
Format , System.Object Encoding , System.Object Visible , System.Object
OpenAndRepair , System.Object DocumentDirection , System.Object
NoEncodingDialog )
Member of Word.Documents

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

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

Guest

Yea, you were close, the article from Cindy notes that you do need values
there, and I figured there were built in enums or static values but I
couldn't find them. The following made it work.

object oMissing = System.Reflection.Missing.Value;
....
wordDoc = wordApp.Documents.Open(ref filename,ref oMissing,
ref oMissing, ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing,ref oMissing,ref oMissing,ref oMissing,
ref oMissing);

Thanks for helping.
gv
 
G

Guest

Well at first I didn't know where to start, however after a little digging,
I figured that it could be done.
Thanks, that article helped me do everything I have to do.
gv



Cindy M -WordMVP- said:
Ah. It does help to mention the specific problem :) Have you tried searching
the Knowledge Base in the msdn library at microsoft.com? And looked through
what's offered on the dotnet website?

One sample article I turned up is HOWTO: Automate Word to Perform a Mail
Merge from C# .NET [301659]

The data types for the parameters you'll find in the Word help for a
particular method. If you're not getting into these Help files from within
the NET environment, open Word, press Alt+F11, type the phrase in question,
then press F1 to open the Help files.

FileName is required, of type String, ConfirmConversions is optional, type
Boolean, etc.
Either option should work, but I need to open the file. And the
Documents.Open method wants many parameters that are not passed in all the
vb samples.
Here is the description from the object browser
public abstract virtual Word.Document Open ( System.Object FileName ,
System.Object ConfirmConversions , System.Object ReadOnly , System.Object
AddToRecentFiles , System.Object PasswordDocument , System.Object
PasswordTemplate , System.Object Revert , System.Object
WritePasswordDocument , System.Object WritePasswordTemplate , System.Object
Format , System.Object Encoding , System.Object Visible , System.Object
OpenAndRepair , System.Object DocumentDirection , System.Object
NoEncodingDialog )
Member of Word.Documents

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

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