Monday, March 26, 2012

Opening Word without Automation in ASP.net C#

Hi all,

I am been having trouble with finding an answer to this question but here goes:

Is there any way to write to MSWORD without automation through ASP.NET in C# (maybe write to an existing word document without opening it)?

I am pulling my hair out! I have tried adding IUSR, ASPNET to the web server, I have tried Identity Impersonate = True in the web.config.

My code for opening word looks like this:

object isVisible = true;
object readOnly = true;

I get a Macro storage error on the open statement. I have put a lot of time into figuring this out and I really dont want to do a TOTAL 180.

wordDoc = WordObj.Documents.Open2000(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);

I appreciate any ideas or suggestions given!

Thank you in advance!

David

what are all those "ref missing" statements for the parameter?

object missing = System.Reflection.Missing.Value;

these are values that are optional or I dont have to supply.


I am assuming null does work, and you have already tried it. The second problem is all your "ref missing" objects are the same object, so if the word document modifies any of them internally it is going to be reflected externally. And since they are all pretty much linked together this might be having some unknown effect on the method internally.

Thank you, I will give that a try.

The only help I can find on this matter is help on windows forms. I appreciate your insight!

I may be back :)


Ok Im back...

Now I seem to be getting this error...

Sorry for the cut and paste job:

Have you ever seen anything like this? What do I need to do? Word is installed on the web server...

Again, thank you.

Server Error in '/wordsite' Application.

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[COMException (0x8000401a): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a.] _Default.btnSubmit_Click(Object sender, EventArgs e) +13 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102



Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210


dspyank:

Hi all,

I am been having trouble with finding an answer to this question but here goes:

Is there any way to write to MSWORD without automation through ASP.NET in C# (maybe write to an existing word document without opening it)?

I am pulling my hair out! I have tried adding IUSR, ASPNET to the web server, I have tried Identity Impersonate = True in the web.config.

My code for opening word looks like this:

object isVisible = true;
object readOnly = true;

I get a Macro storage error on the open statement. I have put a lot of time into figuring this out and I really dont want to do a TOTAL 180.

wordDoc = WordObj.Documents.Open2000(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);

I appreciate any ideas or suggestions given!

Thank you in advance!

David

Hi, David

Here is an example:

// Process.Start("WINWORD.exe","");
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text ="实验名称: "+this.textBox1.Text+"\n\n实验时间: "+System.DateTime.Today.ToShortDateString()+"\n";
// oPara1.Range.Font.Bold = 1;
// oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();

Hope this helps


dspyank:

Ok Im back...

Now I seem to be getting this error...

Sorry for the cut and paste job:

Have you ever seen anything like this? What do I need to do? Word is installed on the web server...

Again, thank you.

Server Error in '/wordsite' Application.

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[COMException (0x8000401a): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a.] _Default.btnSubmit_Click(Object sender, EventArgs e) +13 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102



Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

Hi,

The solution of this error may be : The server process could not be started because the configured identity is incorrect. Check the username and password.

Hope this helps.


Hi Jerome.

Thank you for your help! I was wondering though: If a word document already exists, how would I open it without using automation?

I hope Im not being too much of a pain.

Thank you once again!

Dave


dspyank:

Hi Jerome.

Thank you for your help! I was wondering though: If a word document already exists, how would I open it without using automation?

I hope Im not being too much of a pain.

Thank you once again!

Dave

Hi, Dave

In fact, I have answered this problem. My post above had been bolden with the line "Insert a paragraph"

Insead of creating a new word document, you can open it with ////////////

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);

========>>

oDoc = oWord.Documents.Open(_
FileName:=file_title, _
ConfirmConversions:=False, _
ReadOnly:=False, _
AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", _
Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", _
Format:=Word.WdOpenFormat.wdOpenFormatAuto)
/// You can do it briefly, just like "ref oMissing"

Hope this helps.


Jerome,

Thank you again for all your help.

I still can not get rid of that error above. My web.Config file looks like this:

<

assemblies>

<

addassembly="Office, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"/>

<

addassembly="Microsoft.Office.Interop.Word, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"/>

</

assemblies>

<

identityimpersonate="true"password=""userName=""/>

And my word.open statement:

wdConv = false;
readOnly = false;
spaces = "";
wdFormat =Word.WdOpenFormat.wdOpenFormatAuto;
isVisible = false;

WordDoc = WordObj.Documents.Open(

ref fileName,ref wdConv,ref readOnly,ref missing,ref spaces,ref spaces,ref missing,ref spaces,ref spaces,ref wdFormat,ref missing,ref isVisible,ref missing,ref missing,ref missing,ref missing);When I hit the Open statement, I get the error previously mentioned. We have an aspnet user on the server aa well as IUSR. There is not an awful lot of help out on the web for this since ASP.NET doesnt support it?

What am I missing?

I appreciate your help!

Dave


Hi, Dave

Let me clarify this problem. You want to open and write some sentences into word document in the client side without automation in C# by web form, right?

I nearly confuse. I am sorry without understanding clearly.

Thanks


Jerome,

I actually need to open word on a web server (not my machine) then locate a bookmark and write to the bookmark.

The problem I am having is opening the word document. I either get a Macro Storage Error, or the error above.

The Path to the document is correct, Word is on the Server, we have an IUSR, and an aspnet user on the server for generic login.

This code works perfectly on my machine but once I put it on the Server (poof!)

I hope I clarified a little better.

Thank you!

David


Hi, David

It seems the problem about the path of your word document. There may be always a problem if you want to manipulate the file outside of the "WWWROOT" derectory. It is related to security problem on the web server. If your file path are outside of the \wwwroot, that's it.

Hope this helps


Hi Jerome,

I think I understand what you're saying. However, I have been able to access a file (the word file was on the webserver) from my computer and automate it. So the folder I pulled the word document from was not on my computer. The path was fine. When I put my project on the web server and tried to access the same document it threw the error above or a macro error. There wasnt a path error.

I know Im being difficult, but I have people telling me to do it as an RTF file then automate it that way. If this is the case, why wouldnt I just save the word file as an RTF instead of writing them programmatically? I really do not want to go this way.

Thank you!

Dave

0 comments:

Post a Comment