Solved

MS Word Automation

Posted on 2004-09-09
32
270 Views
Last Modified: 2008-02-01
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!
0
Comment
Question by:PUB_UL
  • 18
  • 13
32 Comments
 
LVL 4

Expert Comment

by:Jigit
ID: 12017643
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12018438
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12019779
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12019837
I set it to false already in my code and still not working =/
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020040
I also switched the isVisible to false and still not working
0
 
LVL 4

Expert Comment

by:Jigit
ID: 12020074
There is also isVisible paramaeter to the Open method. Try setting it also to false.
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020133
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12020242
Unfortunately, no. I'll be able to send you working code after the weekend.
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020277
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12020292
I'll get to work on Sunday and will publish you code for this.
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020319
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020472
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12020506
Of course, you can create a DLL by creating a new Windows Library project in VS.NET. But IMHO it won't work.
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020516
IMHO ?
0
 
LVL 4

Expert Comment

by:Jigit
ID: 12020566
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020636
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:PUB_UL
ID: 12020790
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12020800
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12020976
Nice.

Then I'll be waiting for this!
thanks a lot
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12021053
Where can I find a description and possible values for each parameter of the word API ?!

0
 
LVL 4

Accepted Solution

by:
Jigit earned 200 total points
ID: 12037678
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12044265
what version of office is that?

I don't think it's the same as the one I used..
0
 
LVL 4

Expert Comment

by:Jigit
ID: 12044285
Office 2003.
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12044435
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12044468
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12044680
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12044911
Try to remove Speech Recognition from your Windows:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;316215
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12045145
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
 
LVL 1

Author Comment

by:PUB_UL
ID: 12045375
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
 
LVL 4

Expert Comment

by:Jigit
ID: 12045408
Thanks for accepting my answer. Would you be so kind to fill a feedback (click on the link in Accepted Answer row)?
0
 
LVL 1

Author Comment

by:PUB_UL
ID: 12045438
Done!

thx again
0
 

Expert Comment

by:breezback
ID: 12950568
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now