Solved

Problem Automating Word 2007 -- ActiveDocument not found

Posted on 2008-10-17
7
1,148 Views
Last Modified: 2013-12-17
Hello Experts,

I have a asp.net page that creates and saves a .docx file to a directory and then uses word automation to open the docx and save it as a PDF file. I realize this isn't a great solution, but it's the only option I have at the moment. This works great on my dev box which is running Office 2007 sp1 on W2k3 Server. I was then asked to deploy this to a Win 2008 server. It also has Office 2007 sp1 installed.  

The problem:  Now the app runs and saves the docx file.  However, the word automation piece isn't working. I open the docx using this:
MSdoc.Documents.Open(ref Source, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

And then I save the file using this:
 MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                       ref Unknown, ref Unknown);

This last line triggers the error because it cannot find an Active Document.  Again, this works fine on my dev box.  I'm not sure what might be causing this.

Any help is much appreciated.
0
Comment
Question by:soapygus
  • 4
  • 3
7 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22741731
Is the application opening a winword window for this document?
are you logged into the server when you tried  to run this application?
are you activating a document before saving?

I guess your answer would be yes for the above

You should not be opening a winword window or activate it because if you do so in the applicaiton it will fail if no user is logged in to the machine since the application will not create an active window on the server.

get the reference to the document and do what ever you want using the reference do not activate
0
 

Author Comment

by:soapygus
ID: 22743584

Thanks for the help. I'm not sure I understand what you mean by get a reference to the doc. Is it possible to save the document as a PDF using the techinique you describe?

Here is my method in full.

if (MSdoc == null) MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();

            try
            {
                MSdoc.Visible = true;
                MSdoc.Documents.Open(ref Source, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                MSdoc.Application.Visible = true;
                MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal;

                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

                MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                       ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
                return;
            }
            finally
            {
                if (MSdoc != null)
                {
                    MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                    //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
                // for closing the application
                MSdoc.Quit(ref Unknown, ref Unknown, ref Unknown);
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22743957
remove the following lines of code since we dont need the document to be opened in a window.

MSdoc.Visible = true;
                MSdoc.Application.Visible = true;
                MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal;

also check if there is any Documents.Save method instead of ActiveDocuments.Save

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:soapygus
ID: 22744191

Ok --

I have updated the code to reference the doc as follows:

 if (wordApp == null) wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

            try
            {
                //Open the source docx
                wordDoc = wordApp.Documents.Open(
                     ref Source, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown);
               
                //Export docx to PDF
                if (wordDoc != null)
                {                
                    wordDoc.ExportAsFixedFormat(Target, paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref Unknown);
                }

            }

Unfortunately, ever time I step through the code the method exits at this line:
       
if (wordDoc != null)

I'm not sure why the wordDoc evaluates to null.  I've tried hard coding the path to the file, but I still get the same results.
0
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 500 total points
ID: 22744545
Here is a simple example on opening and saving word document using C#
http://www.codeproject.com/KB/office/automate_word.aspx

Hope this would help you.
0
 

Author Comment

by:soapygus
ID: 22744619

Thanks you got me started in the right path.

I just created a new web app and used the exact same code and it worked.  I think I have a reference that is interfering, but I haven't found it yet.
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22744741
you are welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

911 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

24 Experts available now in Live!

Get 1:1 Help Now