【问题标题】:Interop.Word print preview dialog for office 2010/2013Office 2010/2013 的 Interop.Word 打印预览对话框
【发布时间】:2015-02-27 07:49:07
【问题描述】:

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

我正在打印或关闭文档。

对于 Office 2003 和 2007,这完全可以正常工作,但在 Office 2010 之后的打印预览对话框中完全不同。

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

是否有任何解决方法或建议?

【问题讨论】:

    标签: c# .net winforms ms-word office-interop


    【解决方案1】:

    只是为了将来参考发布我自己的问题的答案。终于我能弄明白了。

    以下代码适用于任何版本的 Word。对话框打开时,应用程序冻结并等待结果,就像另一个对话框一样。

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

                 _doc.Activate();
                 _wordApp.Visible = true;
    
                 var dialogResult = _wordApp.Dialogs[WdWordDialog.wdDialogFilePrint].Show();
    
                 if (dialogResult == 1)
                     _doc.PrintOut();
    

    【讨论】:

      【解决方案2】:

      这里有一些代码,希望能帮助您更接近我在 2000 年前为 Office 2000 的 Interop 编写的代码。我用 Office 2007 对其进行了测试,它可以工作,但没有 2010 来测试 atm。

      我假设您已经拥有互操作引用。

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

      【讨论】:

        猜你喜欢
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-22
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        相关资源
        最近更新 更多