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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Exchange Word Interop

Status
Not open for further replies.

Rhys666

Programmer
May 20, 2003
1,106
I am writing a data exchange tool in asp.net (c#). I', looking to access data in csv or xml files or word 2000 documents. The first two are relatively simple but I'm strufggling a little on the Word 2000 interoperability.

I am using a text editor and command line compiler and have no choice but to do this as I do not have access to an IDE to reference a com object with that nice point, click and go functionality.

I've looked at the tlbimp and SDK sample but just don't seem to be getting it I'm afraid, (Monday morning brain-block?).

I can adapt the SDK Word Interop sample and use a bat file to create what I believe should be a Wrapper for Word called Word.dll, and successfully compile my application referencing the metadata from the word.dll
Code:
tlbimp "%ProgramFiles%\Microsoft Office\Office\msword9.olb" /silent /out:Word.dll 
IF NOT EXIST bin mkdir bin 
%CORPATH%csc /t:library /out:bin\DataExchange.dll /r:System.dll /r:System.Web.dll /r:System.Xml.dll /r:System.Data.dll /r:Word.dll /optimize+ /recurse:*.cs

However, when i change my .cs files to reference interop services and the Word wrapper;
Code:
using System.Runtime.InteropServices; 
using Word;

...I get the follwing compiler errors;
Default.cs(16,32): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)
Default.cs(19,20): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)
Default.cs(20,20): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)
Default.cs(21,20): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)
Default.cs(22,20): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)
Default.cs(23,20): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)
Default.cs(24,20): error CS0234: The type or namespace name 'Web' does not exist in the class or namespace 'Word.System' (are you missing an assembly reference?)

but all that is on these lines are my class declaration;
Code:
public class Default : System.Web.UI.Page

and control declarations
Code:
protected System.Web.UI.WebControls.Button btnGetData; 
protected System.Web.UI.WebControls.Label XMLPending; 
protected System.Web.UI.WebControls.Label DOCPending; 
protected System.Web.UI.WebControls.Label CSVPending; 
protected System.Web.UI.WebControls.Label upLoadFailed; 
protected System.Web.UI.WebControls.Label upLoadCompleted;

Anyone got any idea's?

Rhys
"When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas
"As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates
 
You do have the

using System;

directive included in your class as well? If not that may cause this error. Looks to me like .NET is trying top find the Web namespace in Word.System not System...

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
I guessed as much and changed my approach and created an entirely seperate wrapper class for the Word interoperability and it exposes the Word methods etc I want. Basically when i've got a Word document I want to access/manipulate, the following;
Code:
WordInteropWrapper processFile_DOC = new WordInteropWrapper();
processFile_DOC.Open (_tempPath + _fileName);

instantiates the class and opens the document in memory (it doesn't need to be viewed), which I can then play with using whichever other methods I've got wrapped up.

In case anyone else cares the basic wrapper class looks like this
Code:
namespace MyNamespace 
{

	
	using System;
	using System.Configuration;
	using System.IO;


	/// <summary>
	/// Summary description for ProcessWordFile.
	/// </summary>
	public class WordInteropWrapper
	{


		private Word.ApplicationClass oWordApplic;	// a reference to Word application
		private Word.Document oDoc;
		
		
		public WordInteropWrapper()
		{
			// activate the interface with the COM object of Microsoft Word
			oWordApplic = new Word.ApplicationClass();
		}


		// Open a file (the file must exists) and activate it
		public void Open( string strFileName)
		{
			object fileName = strFileName;
			object readOnly = false;
			object isVisible = true;
			object missing = System.Reflection.Missing.Value;

			oDoc = oWordApplic.Documents.Open(ref fileName, ref missing,ref readOnly, 
				ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
				ref missing, ref missing, ref isVisible);	
		}


		// Open a new document
		public void Open( )
		{
			object missing = System.Reflection.Missing.Value;
			oDoc = oWordApplic.Documents.Add(ref missing, ref missing,ref missing, ref missing);

			oDoc.Activate();			
		}


		public void Quit( )
		{
			object missing = System.Reflection.Missing.Value;
			oDoc.Close(ref missing, ref missing, ref missing);
			oWordApplic.Application.Quit(ref missing, ref missing, ref missing);

		}


		public void Save( )
		{
			oDoc.Save();			
		}


		public void SaveAs(string strFileName )
		{
			object missing = System.Reflection.Missing.Value;
			object fileName = strFileName;

			oDoc.SaveAs(ref fileName, ref missing,ref missing, ref missing,ref missing,ref missing,ref missing,
				ref missing,ref missing,ref missing, ref missing);//, ref missing, ref missing, ref missing, ref missing, ref missing);
		}
		

		// Save the document in HTML format
		public void SaveAsHtml(string strFileName )
		{
			object missing = System.Reflection.Missing.Value;
			object fileName = strFileName;
			object Format = (int)Word.WdSaveFormat.wdFormatHTML;
			oDoc.SaveAs(ref fileName, ref Format,ref missing, ref missing,ref missing,ref missing,ref missing,
				ref missing,ref missing,ref missing, ref missing);//, ref missing, ref missing, ref missing, ref missing, ref missing);
		}

	}


}

and I slightly amended my command line compile bat file to;
Code:
tlbimp "%ProgramFiles%\Microsoft Office\Office\msword9.olb" /silent /out:bin\Word.dll

IF NOT EXIST bin mkdir bin
%CORPATH%csc /t:library /out:bin\DataExchange.dll /r:System.dll /r:System.Web.dll /r:System.Xml.dll /r:System.Data.dll /r:bin\Word.dll /optimize+ /recurse:*.cs

Thus far it's all working a treat!

Rhys
"When one burns one's bridges, what a very nice fire it makes" -- Dylan Thomas
"As to marriage or celibacy, let a man take the course he will. He will be sure to repent" -- Socrates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top