MS Word Automation

Hi!

I have a big ms word document that I would like to open from my ASP.NET and add text at some specified bookmark.
I looked over google and found out it was kinda straight foward to open a doc, but I can't even get to open an existing one !

I've been able to open a new doc, write text to it and save it to my HD and print it.

Here is my code:

            private void btnPrint_Click(object sender, System.EventArgs e)
            {
                  object fileName = "c:\\test.doc";
                  object readOnly = false;
                  object isVisible = true;
                  object oTrue = true;
                  object oFalse = false;
                  object missing = System.Reflection.Missing.Value;

                  Word.ApplicationClass oWord = new Word.ApplicationClass();

                  oWord.Visible = true;  // Which doesn't seem to work, the word won't show up and when I check it's value later on, oWord.Visible still at false.

                  // The next line won't work .. will just kind wait here 'til I stop the application
                  // Word.Document oWordDoc = oWord.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, ref missing, ref missing, ref missing, ref missing);

                  // The next line works well
                  Word.Document oWordDoc = oWord.Documents.Add(ref missing, ref missing, ref missing, ref missing);

                  // Won't reach those line if I use the Open method. If I use the Add Method,
                  oWordDoc.Activate();

                  // Will try to add text only if I used the Add method to create a blank doc
                  oWord.Selection.TypeText("This is the text");
                  oWord.Selection.TypeParagraph();

                  // Prints out the new document with "This is the text" but I never reached this point
                  // From an opened document =/
                  oWord.PrintOut(ref oTrue, 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, ref missing, ref missing, ref missing);


                  oWord.Application.Quit(ref missing, ref missing, ref missing);
            }

Any help will be GREATLY appreciated! I'm ready to put more points for a 100% working solution!
LVL 1
PUB_ULAsked:
Who is Participating?
 
JigitConnect With a Mentor Commented:
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;

public class MyClass
{
      private void btnPrint_Click(object sender, System.EventArgs e)
      {

            Word.Application word = null;

            object templateFilenameObj =  "c:\\test.doc",
                  falseObj = false,
                  trueObj = true,
                  emptyStringObj = String.Empty,
                  zeroObj = 0,
                  missingValueObj = Missing.Value,
                  fileFormatObj = Word.WdSaveFormat.wdFormatTemplate,
                  wordFilenameObj = filename,
                  formatObj = Word.WdOpenFormat.wdOpenFormatAuto;

            try
            {
                  word = OfficeServer.OfficeApp.OfficeInstance.Word;
                        
                  // Open Word template document
                  Word._Document doc = word.Documents.Open( ref templateFilenameObj, ref falseObj, ref trueObj, ref falseObj,
                        ref emptyStringObj, ref emptyStringObj, ref falseObj, ref emptyStringObj, ref emptyStringObj,
                        ref zeroObj, ref missingValueObj, ref missingValueObj, ref missingValueObj, ref missingValueObj,
                        ref missingValueObj, ref missingValueObj);
                        
                  // Save merged file as Word document
                  doc.SaveAs(ref wordFilenameObj, ref fileFormatObj, ref falseObj, ref emptyStringObj,
                        ref trueObj, ref emptyStringObj, ref falseObj, ref falseObj, ref falseObj, ref falseObj,
                        ref falseObj, ref missingValueObj, ref missingValueObj, ref missingValueObj,
                        ref missingValueObj, ref missingValueObj );

                  // Close document
                  doc.Close(ref falseObj, ref missingValueObj, ref missingValueObj);

            }
            catch
            {
            }
            finally
            {
                  word.Close( falseObj );
            }
      }
}
0
 
JigitCommented:
Hi, it seems to be a tough question and we'll try to investigate it step by step. So I guess it costs more points :-)

The main problem may arise because of ASP.NET application runs under ASPNET account. I would suggest to create a Windows Application and test the same code there first. What might happens it your case, that MS Word tries to pop up a window and it fails because ASPNET is not an interactive user. So you must ensure your Office is fully activated and does not try to install anything on openning.

Let me know your progress and we'll think then.

HTH,
Jigit
0
 
PUB_ULAuthor Commented:
Ok.

I found out trying it in a win application, that when closing the word, it was asking to save or not. so I changed the code so it answers that and using the win app was doing fine. Nothing was shown and the page was printing :)

So I copied the code over to my ASP application and same thing still happen. it won't open the document at all, it kinda stay at that line forever:
Word.Document oWordDoc = oWord.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, ref missing, ref missing, ref missing, ref missing);

Any idea?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
JigitCommented:
I've jist noticed that you set oWord.Visible to True. What's the reason? IMHO, it must be set to False. Could you try and tell me the result?

HTH,
Jigit
0
 
PUB_ULAuthor Commented:
I set it to false already in my code and still not working =/
0
 
PUB_ULAuthor Commented:
I also switched the isVisible to false and still not working
0
 
JigitCommented:
There is also isVisible paramaeter to the Open method. Try setting it also to false.
0
 
PUB_ULAuthor Commented:
Already did it !! hehe..

Do you happen to know how to put up some text at a given bookmark ? (Since I can startring work on that in the windows based app eventhough it needs to work in asp.net at the end)
0
 
JigitCommented:
Unfortunately, no. I'll be able to send you working code after the weekend.
0
 
PUB_ULAuthor Commented:
Here I tested and works well for bookmark:

// Get a reference to the EndroitA's bookmark Range and add some text in it
object oBookMark = "EndroitA";
Word.Range myRange = oWordDoc.Bookmarks.get_Item(ref oBookMark).Range;
myRange.Text = "Here is a testt";

The only thing left is open an existing doc in ASP.NET =]
0
 
JigitCommented:
I'll get to work on Sunday and will publish you code for this.
0
 
PUB_ULAuthor Commented:
Ok.

I'll probably have it already by then cuz this is pretty urgent but if I haven't closed this question yet, It will be more than appreciated!
0
 
PUB_ULAuthor Commented:
or is there a way for me to create a dll with the code I need

and simply call this from my asp.net ?

cuz in fact, I just need to print that word doc in pdf format and know where it is so I can have a download link for it
0
 
JigitCommented:
Of course, you can create a DLL by creating a new Windows Library project in VS.NET. But IMHO it won't work.
0
 
PUB_ULAuthor Commented:
IMHO ?
0
 
JigitCommented:
It will work when you call the DLL from WinApp and won't work if you call it from ASP.NET application. The reason is there is the same: there is something interactive that Word is trying to do.
0
 
PUB_ULAuthor Commented:
ok...

What can be interactive in that :

Word.Document oWordDoc = oWord.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, ref missing, ref missing, ref missing, ref missing);

most of the param aren't even interactive at all I guess.. instead of having everything to missing, shouldn't I try using False where I don't wanna use that ?
0
 
PUB_ULAuthor Commented:
the thing that I don't quite get is that when in the windows application I see nothing but the pointer changing real fast the back to normal.. what could be so "interactive" to prevent it to work from asp.net =/

0
 
JigitCommented:
I really don't remember. But I used to do it. The source code is on my working machine. I'll get there on Sunday.
0
 
PUB_ULAuthor Commented:
Nice.

Then I'll be waiting for this!
thanks a lot
0
 
PUB_ULAuthor Commented:
Where can I find a description and possible values for each parameter of the word API ?!

0
 
PUB_ULAuthor Commented:
what version of office is that?

I don't think it's the same as the one I used..
0
 
JigitCommented:
Office 2003.
0
 
PUB_ULAuthor Commented:
For this line: word = OfficeServer.OfficeApp.OfficeInstance.Word;
I get:
C:\Inetpub\wwwroot\Intranet\audition.aspx.cs(473): The type or namespace name 'OfficeServer' could not be found (are you missing a using directive or an assembly reference?)

and for this line: word.Close( falseObj );
I get:
C:\Inetpub\wwwroot\Intranet\audition.aspx.cs(502): 'Microsoft.Office.Interop.Word.Application' does not contain a definition for 'Close'

I guess I could use word.Quit();

but what for the first error ?

0
 
JigitCommented:
Please accept my appologies:
1) OfficeServer is my class. Use:
  word = new Word.ApplicationClass();
2) You are right about this one. Try:
word.Quit();
0
 
PUB_ULAuthor Commented:
I still have the same problem !

The probram will do nothing once reached this line:

Word._Document doc = word.Documents.Open( ref templateFilenameObj, ref falseObj, ref trueObj, ref falseObj,
                              ref emptyStringObj, ref emptyStringObj, ref falseObj, ref emptyStringObj, ref emptyStringObj,
                              ref zeroObj, ref missingValueObj, ref missingValueObj, ref missingValueObj, ref missingValueObj,
                              ref missingValueObj, ref missingValueObj);

the webpage just seems to be loading for ever
0
 
JigitCommented:
Try to remove Speech Recognition from your Windows:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;316215
0
 
PUB_ULAuthor Commented:
humm.. it looks like I don't have it installed !

The process isn't running and when I follow the steps to remove it, it's not listed in the installed service.
0
 
PUB_ULAuthor Commented:
You won't believe me !

I got it work with my code and yours..

the problem was permission !
I had ASPNET account set properly but didn't work.

A friend of mine told me to add NETWORK SERVICE with the correct privilege .. and that did it !

I'll give you 200 pts with an A answer for helping me out so much and that quick  I really appreciated!

Thanks again man
0
 
JigitCommented:
Thanks for accepting my answer. Would you be so kind to fill a feedback (click on the link in Accepted Answer row)?
0
 
PUB_ULAuthor Commented:
Done!

thx again
0
 
breezbackCommented:
I have quite the same problem , except that I can open the doc but not print it.
I get the follwing error:

But the printer is fine ....
So I thought it could be the same issue , please tell me have to 'to add NETWORK SERVICE with the correct privilege ' in Windows 2000 Professional

Thanks in advance,

:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.