Solved

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

Posted on 2004-04-16
5
1,568 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 

Expert Comment

by:BusyPhoto
ID: 10847492
0
 

Author Comment

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

Author Comment

by:famoso
ID: 11099176
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
ID: 11300186
PAQed, with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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