MS Word Viewer C# .NET automation

16,024

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
16,024
Rhys Evans
Author by

Rhys Evans

Updated on June 04, 2022

Comments

  • Rhys Evans
    Rhys Evans almost 2 years

    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