Link to home
Start Free TrialLog in
Avatar of famoso
famosoFlag for United States of America

asked on

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

This is close to what I'm looking for.: https://www.experts-exchange.com/questions/20716375/Server-execution-failed-error.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);
Avatar of BusyPhoto
BusyPhoto

Avatar of famoso

ASKER

That isn't working for me - Plus I'm using ASP.NET.  Anyone have any other suggestions?
Avatar of famoso

ASKER

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;
      }
    }

  }
}
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial