Interop.Word print preview dialog for office 2010/

2019-05-11 19:56发布

问题:

In my application I have word template with tags which are later replaced using interop.word (find/replace) and then sent to print using print preview dialog: Interop.Word.Application.Dialogs[WdWordDialog.wdDialogFilePrint]

On a result of a dialog I am printing or closing the document.

For office 2003 and 2007 this is working absolutely fine, but in office 2010 later print preview dialog is absolutely different.

I've found related post here but I need to grab dialog result so that i could further do print or close the doc.

Is there any workaround or soultions for that?

回答1:

Just for the future reference posting answer to my own question. Finally I could figure it out.

Following code is working with any version of Word fine. While dialog is open application freezes and waits for a result just as another dialogs.

The only moment is it allows to choose appropriate printer only, but does not show preview.

             _doc.Activate();
             _wordApp.Visible = true;

             var dialogResult = _wordApp.Dialogs[WdWordDialog.wdDialogFilePrint].Show();

             if (dialogResult == 1)
                 _doc.PrintOut();


回答2:

Here is some code that will hopefully help you get closer that I wrote for Interop for Office 2000 years ago. I tested it with Office 2007 and it works, but don't have 2010 to test atm.

I'm assuming you already have the Interop references.

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace printdialog
{
    public partial class Form1 : Form
    {
        Word.ApplicationClass WordApp;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create Word Instance
            WordApp = OpenWordApplication();

             //Show Word
            WordApp.Visible = true;

            //Open a Word Doc
            OpenWordDocument(WordApp, "c:\\test.docx");


            DialogResult result = MessageBox.Show("Would you like to Print?", "PrintPreview", MessageBoxButtons.OKCancel);
            switch (result)
            {
                case DialogResult.OK:
                    {
                        WordApp.PrintPreview = true;
                        //if preview call above doesn't work try the call below 
                        //WordApp.ActiveWindow.View.Type = Word.WdViewType.wdPrintPreview;
                        break;
                    }
                case DialogResult.Cancel:
                    {
                        CloseWordApplication(WordApp);
                        break;
                    }
            }
        }

        /// <summary>
        /// This method returns a Word.ApplicationClass Object.
        /// Tested with the Microsoft 9.0 Object Library ( COM )
        /// </summary>
        /// <returns>Word.ApplicationClass Object</returns>
        public static Word.ApplicationClass OpenWordApplication()
        {
            try
            {
                Word.ApplicationClass WordApp = new Word.ApplicationClass();
                return (WordApp);
            }

            catch (Exception e)
            {
                //show the user the error message
                MessageBox.Show(e.Message);

                return (null);
            }

        }

        /// <summary>
        /// This method returns a Word.Document Object from the File Location and loads it into the 
        /// Word.ApplicationClass Object. Basically it means it opens a previously saved word document. 
        /// Tested with the Microsoft 9.0 Object Library ( COM )
        /// </summary>
        /// <param name="WordApp">This is the Word.ApplicationClass Object. It is the Object that contains
        /// the Word Application</param>
        /// <param name="FileLocation">This is the File Location for the Word Document you would like to open.
        /// Note that this is the full long name of the File Location.</param>
        /// <returns>Word.Document Object</returns>
        public static Word.Document OpenWordDocument(Word.ApplicationClass WordApp, string FileLocation)
        {
            try
            {
                object j_FileName = FileLocation;
                object j_Visible = true;
                object j_ReadOnly = false;
                object j_NullObject = System.Reflection.Missing.Value;

                // Let's open the document
                Word.Document WordDoc = WordApp.Documents.Open(ref j_FileName,
                ref j_NullObject, ref j_ReadOnly, ref j_NullObject, ref j_NullObject,
                ref j_NullObject, ref j_NullObject, ref j_NullObject, ref j_NullObject,
                ref j_NullObject, ref j_NullObject, ref j_Visible);

                return (WordDoc);
            }

            catch (Exception e)
            {
                //show the user the error message
                MessageBox.Show(e.Message);

                return (null);
            }
        }


        /// <summary>
        /// This method closes the Word.ApplicationClass instance that is sent
        /// as a parameter. Releasing the COM Object by Marshal seems
        /// to properly dispose of the Object.
        /// Tested with the Microsoft 9.0 Object Library ( COM )
        /// </summary>
        /// <param name="WordApp"></param>
        public static void CloseWordApplication(Word.ApplicationClass WordApp)
        {
            try
            {
                object j_SaveChanges = false;
                object j_NullObject = System.Reflection.Missing.Value;

                WordApp.Quit(ref j_SaveChanges, ref j_NullObject, ref j_NullObject);
                Marshal.ReleaseComObject(WordApp);
                WordApp = null;

                System.GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            catch (Exception e)
            {
                //show the user the error message
                MessageBox.Show(e.Message);

            }
        }

    }
}