首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Interop.Word 2010/2013年office打印预览对话框

Interop.Word 2010/2013年office打印预览对话框
EN

Stack Overflow用户
提问于 2015-02-27 07:49:07
回答 2查看 3K关注 0票数 3

在我的应用程序中,我有带有标记的word模板,这些标记后来使用interop.word (find/replace)替换,然后使用打印预览对话框:Interop.Word.Application.Dialogs[WdWordDialog.wdDialogFilePrint]发送到打印。

在一个对话框的结果,我打印或关闭的文件。

对于office 2003和2007,这是绝对好的工作,但在office 2010以后打印预览对话框是绝对不同的。

我已经找到了相关的post here,但我需要获取对话框结果,以便进一步打印或关闭文档。

有什么解决办法或解决办法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-03 06:06:59

只是为了将来参考贴出对我自己问题的答案。我终于能搞清楚了。

下面的代码正在处理任何版本的Word罚款。当对话框打开时,应用程序会冻结并等待结果,就像另一个对话框一样。

唯一的时刻是它只允许选择适当的打印机,但不显示预览。

代码语言:javascript
复制
             _doc.Activate();
             _wordApp.Visible = true;

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

             if (dialogResult == 1)
                 _doc.PrintOut();
票数 4
EN

Stack Overflow用户

发布于 2015-02-27 12:15:36

下面是我在2000年前为Office为Interop编写的一些代码,希望能帮助您更接近它。我用Office 2007测试了它,它可以工作,但是没有2010年的时间来测试atm。

我假设你已经有互操作的参考资料了。

代码语言:javascript
复制
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);

            }
        }

    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28759978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档