Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word.Application in C#

Status
Not open for further replies.

AtariJag64

Programmer
Mar 4, 2002
8
US
I'm trying to programmatically edit a Word Document in C#.

I want to open the file, edit the file via Bookmarks, and then save it.

From my research, I've found that you have to go to "Add Reference" and add the MSWord9 reference to the project. This I have done.

The code looks like this:

private Word.Application WordObj = null;
InitializeComponent();
WordObj = new Word.ApplicationClass();

Now this WordObj gives all of the member functions that I need; however, the parameters all ask for "ref object" data types.

for example:

WordObj.Documents.Open( )

This is where you can specify the document to open, etc., but all of the arguments within .Open() are of type "ref object"--which won't convert from strings or ints.

Has anyone experimented with any of this before, and can maybe give me a push in the right direction?

Thanks guys,
-Chris Bryant

 
Just went through this fun over the weekend (but not with MSWord).

"ref" means that you're passing an object by reference. You'll need to declare it and instantiate it before passing it:
Code:
String myDocName = "c:\\someword.doc";
WordObj.Documents.Open(ref myDocName);

You can also pass arguments as "out" parameters, where they get instantiated in the function (this is what I needed).

Chip H.
Error on line 9: Object of type SIGNATURE expected
 
Yeah,

The ref part was throwing me at first, but I figured that part out. Now I just don't understand why it asks for type "object."

I've tried to do this:

string FileName = "c:/directory/filename.doc";
WordObj.Documents.Open(ref FileName,...);

But it says it can't convert "string" to "object". Does anyone know what sort of "object" that the Word.ApplicationClass() could be looking for? I'm thinking that the Word component has its own defined data types, but I'm not sure what those are, or how to assign variables to them.

Thanks again,
-Chris B.
 
If you figure out the above problem, please let me know.

I've been working on the same issue and can't come up with anything. From what I can tell my code should work, it compiles, but I get an exception when I try to execute the code.

Here's a link that explains all the parameters

Watch out for the 2nd and 3rd to last parameters

you'll need to use these enums provided by ms

object Format = Word.WdOpenFormat.wdOpenFormatAuto;
object Encoding = Office.MsoEncoding.msoEncodingUSASCII;

Lastly, If you're doing a simple project, you may want to do this is VB (because the parameters are all optional there)
 
AtariJag64 -

You shouldn't have any problems passing a String where an Object is required, as System.String inheirits from System.Object. Try casting it to Object like this:
Code:
WordObj.Documents.Open(ref (Object)FileName,...);
Plus, make sure you use the constants like DiggityDuncs says.

Chip H.
 
Hope this helps

object FilePath = "E:/DirectoryName/FileName.doc";
object False = false;
object True = true;
object Blank = "";
object Format = Word.WdOpenFormat.wdOpenFormatAuto;
object Encoder = Office.MsoEncoding.msoEncodingUSASCII;

WordApp.Documents.Open(
ref FilePath,
ref False,
ref False,
ref False,
ref Blank,
ref Blank,
ref False,
ref Blank,
ref Blank,
ref Format,
ref Encoder,
ref True);

Paul

 
Paul,

Thank you so much! That was a big help. Now I'm trying to create the document object:

Word.Document theDocument = WordObj.Documents.Open();

This compiles, but I get a runtime error of the following:

"Object reference is not set to an instance of an object."

I've tried setting the parameter objects as global and local, but this doesn't help. I don't know if the "object reference" its talking about is the parameters or the Word.Document object itself.

Any suggestions?

Thanks again!
-Chris B.
 
Hello

From what I can tell your having problems instantiating your word object. I'm assuming you've made a reference to the word (com object version 8.1)

you want to instantiate the word object as follows:

Word.Application word = new Word.Application();

then you can do whatever you want....

word.Documents.Open(..........);
word.ActiveDocument.PrintOut(........); (The active documents is the document you just opened, I'm sure you can do you manipulation code here, and save it accordingly)

open up the object browser to see what classes and methods the word object has. From what I can tell, if you can do something with your mouse, you can do it through this SDK

Paul
 
You've just faced the problem i've faced 'bout 2 months ago ^-^. This was very confusing wasn't it :)
here's the way to open a document using word component :

Microsoft.Word.Application oWord;
Microsoft.Word.Document oDoc;

object FileName = ""; (blank string means new blank document)
// if you want to open a certain document or template, just fill the filename with the path and the filename
object IsDot = false;
object DocType = 0; (this is enum, while the help about this you can find in vbaword help)
object Visibility = true;

oWord = new Word.ApplicationClass();
oDoc = oWord.Documents.Add(ref FileName, ref IsDot, ref DocType, ref Visibility);

// after this, you could simply edit your bookmark like this

oDoc.Bookmarks.Item (ref FIELDNAME).Range.Text = FIELDVALUE;

hope this could help you :) ciaooooooooooo... if this still doesn't work, just tell me... i already left this about 2 months ^-^
 
I am trying to control Word using ASP.NET (with C#)
but it does not work:
I can instantiate the COM (
Word.Application oWordApp = new Word.ApplicationClass(); )
but I get errors when I am trying to access member of
the object , eg oWordApp.Version.
The error message is:
"QueryInterface for interface Word._Application failed."
(exception: System.InvalidCastException)

Someone got an answer ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top