3

Is it possible to automate the following: referencing MS Word Viewer to open a document programatically and then print it? C# ideally

I'm guessing if it is possible to open it then more than likely it will be possible to print it.

I've tried adding a reference to COM Object in Visual Studio .. MS Office 11 / 12 Object Library but MS Word Library isnt listed? Any ideas?

I haven't got Office 200x installed

cheers

Jota
  • 17,281
  • 7
  • 63
  • 93
Rhys Evans
  • 203
  • 2
  • 4
  • 10

6 Answers6

5

We did it by using the Word Interop assembly. This requires Word to be installed (launches a WINWORD process behind the scenese) and the interop allows you to interact with it in your code.

As far as I know, that is the only way to do it.

Russell
  • 17,481
  • 23
  • 81
  • 125
3

Try Aspose.Words, it's designed to allow for Office automation without any dependencies on having Word installed. It provides a nice API to open the document then perform a range of actions such as print, export to pdf and many other outcomes.

John Sibly
  • 22,782
  • 7
  • 63
  • 80
Brian Scott
  • 9,221
  • 6
  • 47
  • 68
2

Following code will open up Word View with the file you pass to it.

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("WORDVIEW.exe", fileName.ToString());
System.Diagnostics.Process.Start(info);

Try to mess around with the arguments as well to pass a command line print (I do not know if you can).

And yes after exhausting every avenue there is no way I have found to Interop with the Microsoft Viewer which is very frustrating.

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
Buck Hiatt
  • 21
  • 1
2

maybe like this:

class Program
{
    static void Main(string[] args)
    {
        PrintDocument(@"C:\test.docx", 2);
        Console.ReadKey();
    }

    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    private static void PrintDocument(string name, int copies)
    {
        var process = System.Diagnostics.Process.Start(new ProcessStartInfo
        {
            FileName = name,
            UseShellExecute = true
        });

        process.WaitForInputIdle();
        SetForegroundWindow(process.MainWindowHandle);

        SendKeys.SendWait("^p"); // send CTRL+P
        SendKeys.SendWait(copies.ToString()); // send number of copies
        SendKeys.SendWait("~"); // send ENTER

        // -- or send all in one
        //SendKeys.SendWait(string.Format("^p{0}~", copies));
    }
}
Mariush
  • 21
  • 1
1

Are referring to the free Microsoft Word Viewer, which allows you to view Word documents without actually having Word installed? If so, I don't believe there is a way to automate the viewer since it doesn't install the Word COM automation libraries, which is what you would need.

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
  • Sometimes programs support another way, e.g. DDE or a command-line parameter, to tell them to print something. You may see that in the registry, e.g. in my `HKEY_CLASSES_ROOT\AcroExch.acrobatsecuritysettings.1\shell\Print\command` I have a value ""C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"" /p /h "%1" ... where the /p parameter is presumably telling it to print. – ChrisW Aug 18 '09 at 01:37
1

This is how to use word automation services

Using the Interop assemblies is always a bad idea if it's run on the server Word Automation Services

That uses SharePoint which not everyone has. You also deliver the file to a web page via a WebRequestMethod and print the page to a cute pdf writer or another driver. Just send the bytes of the file with a mime type. You would print in the page load of the asp.net web page.

hoang
  • 1,887
  • 1
  • 24
  • 34
user746880
  • 11
  • 1