Interop.Word office 2010/2013 的打印预览对话框
Interop.Word print preview dialog for office 2010/2013
在我的应用程序中,我有带有标签的 word 模板,这些标签后来被 interop.word (find/replace)
替换,然后使用打印预览对话框发送到打印:
Interop.Word.Application.Dialogs[WdWordDialog.wdDialogFilePrint]
根据对话框的结果,我正在打印或关闭文档。
对于 office 2003 和 2007,这绝对可以正常工作,但在 office 2010 中,更高版本的打印预览对话框完全不同。
我找到了相关的 post here 但我需要获取对话框结果以便我可以进一步打印或关闭文档。
是否有任何解决方法或解决方案?
这里有一些代码,希望能帮助您更接近我在多年前为 Interop for Office 2000 编写的代码。我用 Office 2007 测试过它,它可以工作,但没有 2010 来测试 atm。
我假设您已经有了 Interop 参考资料。
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);
}
}
}
}
仅供将来参考张贴我自己的问题的答案。最后我想通了。
以下代码适用于任何版本的 Word。当对话框打开时,应用程序会冻结并等待结果,就像另一个对话框一样。
唯一的一点是它只允许选择合适的打印机,但不显示预览。
_doc.Activate();
_wordApp.Visible = true;
var dialogResult = _wordApp.Dialogs[WdWordDialog.wdDialogFilePrint].Show();
if (dialogResult == 1)
_doc.PrintOut();
在我的应用程序中,我有带有标签的 word 模板,这些标签后来被 interop.word (find/replace)
替换,然后使用打印预览对话框发送到打印:
Interop.Word.Application.Dialogs[WdWordDialog.wdDialogFilePrint]
根据对话框的结果,我正在打印或关闭文档。
对于 office 2003 和 2007,这绝对可以正常工作,但在 office 2010 中,更高版本的打印预览对话框完全不同。
我找到了相关的 post here 但我需要获取对话框结果以便我可以进一步打印或关闭文档。
是否有任何解决方法或解决方案?
这里有一些代码,希望能帮助您更接近我在多年前为 Interop for Office 2000 编写的代码。我用 Office 2007 测试过它,它可以工作,但没有 2010 来测试 atm。
我假设您已经有了 Interop 参考资料。
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);
}
}
}
}
仅供将来参考张贴我自己的问题的答案。最后我想通了。
以下代码适用于任何版本的 Word。当对话框打开时,应用程序会冻结并等待结果,就像另一个对话框一样。
唯一的一点是它只允许选择合适的打印机,但不显示预览。
_doc.Activate();
_wordApp.Visible = true;
var dialogResult = _wordApp.Dialogs[WdWordDialog.wdDialogFilePrint].Show();
if (dialogResult == 1)
_doc.PrintOut();