Solved

Server execution failed - Word(2000-2003) Automation ASP.NET/C#

Posted on 2004-04-16
5
1,555 Views
Last Modified: 2008-02-25
This is close to what I'm looking for.: http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20716375.html but I'm using Word.
I'm trying to automate Word on a server (that has Word 2000) using ASP.NET to background print to the server's default printer. It works fine locally on my development machine printing to my default/network printer.
The server has Word 2000 and I have 2003. I'm using MSWORD9.OLB after using references to PIA's ver.11 and 10 to no avail.
Here's the code: (minus error trapping)

My error occurs with an error message of ('Server execution failed') on the following line:
        oWordApp = new Word.ApplicationClass();

        object fileName = Server.MapPath(@"/CFMSystem/test.doc");

          // Open Existing Word Document
          oWordDoc = oWordApp.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);

          oWordDoc.Activate();

          // Write to Document
          oWordApp.Selection.WholeStory();
          oWordApp.Selection.Text = "Printing info for xyz";

          // Save Document
          oWordDoc.Save();

          oWordApp.PrintOut(
            ref isTrue, 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);

        //  Monitor Background Printing Status before closing
        Response.Write("Printing ");
        while (oWordApp.BackgroundPrintingStatus != 0) {
          Response.Write(". ");
        }
        Response.Write("Done");
       
        // Close Document
        oWordDoc.Close(ref missing, ref missing, ref missing);
       
        // Quit Application
        oWordApp.Application.Quit(ref missing, ref missing, ref missing);
0
Comment
Question by:famoso
  • 2
5 Comments
 

Expert Comment

by:BusyPhoto
Comment Utility
0
 

Author Comment

by:famoso
Comment Utility
That isn't working for me - Plus I'm using ASP.NET.  Anyone have any other suggestions?
0
 

Author Comment

by:famoso
Comment Utility
Admin can close this.  Word shouldn't be automated from ASP.NET.  My suggested solution is to create a Windows form with a browser control to reference the web app and use a windows form button to print the 'document' from the client after processing on the webserver is finished.  Try referencing ie: \\server\c$\inetpub\wwwroot\etc\someworddoc.doc and print to the default printer... Any questions, email me clonedaddress-expertsexchange@yahoo.com.

Here's my code to print a file (.doc) [i got this code from somewhere and altered it a bit]

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace GeneralProcesses {
  /// <summary>
  /// Shell for the sample.
  /// </summary>
  public class GPPrint {
    // These are the Win32 error code for file not found or access denied.
    const int ERROR_FILE_NOT_FOUND = 2;
    const int ERROR_ACCESS_DENIED  = 5;

    /// <summary>
    /// Prints a file with a .doc extension.
    /// </summary>
    public void Print_doc(string filename) {
      Process myProcess = new Process();

      try {
        // Get the path that stores user documents.
        myProcess.StartInfo.FileName = filename.ToString();

        // Set the verb to use when opening the document
        myProcess.StartInfo.Verb = "Print";

        // Start the process in new window
        myProcess.StartInfo.CreateNoWindow = true;

        // Start the process
        myProcess.Start();

        // Wait for the process to exit
        myProcess.WaitForExit(90);

        if (myProcess.ProcessName!="") {
          myProcess.Kill();
        }
      }
      catch (Win32Exception e) {
        if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND) {
          Console.WriteLine(e.Message + ". Check the path.");
        }

        else if (e.NativeErrorCode == ERROR_ACCESS_DENIED) {
          // Note that if your word processor might generate exceptions
          // such as this, which are handled first.
          Console.WriteLine(e.Message + ". You do not have permission to print this file.");
        }
      }
      catch (Exception) {
        return;
      }
    }

  }
}
0
 

Accepted Solution

by:
modulo earned 0 total points
Comment Utility
PAQed, with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

13 Experts available now in Live!

Get 1:1 Help Now