简体   繁体   中英

MS Word Viewer C# .NET automation

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

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.

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.

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM