C# office 2010/2013的互操作Word打印预览对话框
在我的应用程序中,我有一个带有标签的word模板,这些标签后来使用C# office 2010/2013的互操作Word打印预览对话框,c#,.net,winforms,ms-word,office-interop,C#,.net,Winforms,Ms Word,Office Interop,在我的应用程序中,我有一个带有标签的word模板,这些标签后来使用interop.word(查找/替换)替换,然后使用打印预览对话框发送到打印: Interop.Word.Application.Dialogs[WdWordDialog.wdDialogFilePrint] 根据对话框的结果,我正在打印或关闭文档 对于Office2003和2007来说,这绝对可以正常工作,但在Office2010中,稍后的“打印预览”对话框则完全不同 我已经找到了相关的帖子,但我需要抓取对话框结果,以便我可以进
interop.word(查找/替换)
替换,然后使用打印预览对话框发送到打印:
Interop.Word.Application.Dialogs[WdWordDialog.wdDialogFilePrint]
根据对话框的结果,我正在打印或关闭文档
对于Office2003和2007来说,这绝对可以正常工作,但在Office2010中,稍后的“打印预览”对话框则完全不同
我已经找到了相关的帖子,但我需要抓取对话框结果,以便我可以进一步打印或关闭文档
有什么解决方法或解决方案吗?这里有一些代码,希望能帮助您更接近2000年前我为Office Interop编写的代码。我用Office2007测试了它,它可以工作,但没有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);
}
}
}
}
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
名称空间打印对话框
{
公共部分类Form1:Form
{
Word.ApplicationClass WordApp;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//创建Word实例
WordApp=OpenWordApplication();
//秀词
可见=true;
//打开Word文档
OpenWordDocument(WordApp,“c:\\test.docx”);
DialogResult=MessageBox.Show(“是否要打印?”,“打印预览”,MessageBox按钮.ok取消);
开关(结果)
{
case DialogResult.OK:
{
WordApp.PrintPreview=true;
//如果上面的预览调用不起作用,请尝试下面的调用
//WordApp.ActiveWindow.View.Type=Word.WdViewType.wdPrintPreview;
打破
}
案例对话框结果。取消:
{
CloseWordApplication(WordApp);
打破
}
}
}
///
///此方法返回Word.ApplicationClass对象。
///使用Microsoft 9.0对象库(COM)进行测试
///
///Word.ApplicationClass对象
公共静态Word.ApplicationClass OpenWordApplication()
{
尝试
{
Word.ApplicationClass WordApp=新单词.ApplicationClass();
返回(WordApp);
}
捕获(例外e)
{
//向用户显示错误消息
MessageBox.Show(e.Message);
返回(空);
}
}
///
///此方法从文件位置返回Word.Document对象,并将其加载到
///它基本上意味着它打开了以前保存的Word文档。
///使用Microsoft 9.0对象库(COM)进行测试
///
///这是Word.ApplicationClass对象。它是包含
///单词应用
///这是要打开的Word文档的文件位置。
///请注意,这是文件位置的完整名称。
///文档对象
公共静态Word.Document OpenWordDocument(Word.ApplicationClass WordApp,字符串文件位置)
{
尝试
{
对象j_FileName=文件位置;
对象j_Visible=true;
对象j_ReadOnly=false;
object j_NullObject=System.Reflection.Missing.Value;
//让我们打开文件
Word.Document WordDoc=WordApp.Documents.Open(参考文件名,
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);
返回(WordDoc);
}
捕获(例外e)
{
//向用户显示错误消息
MessageBox.Show(e.Message);
返回(空);
}
}
///
///此方法关闭发送的Word.ApplicationClass实例
///作为参数。通过封送处理释放COM对象似乎
///妥善处置该物品。
///使用Microsoft 9.0对象库(COM)进行测试
///
///
公共静态void CloseWordApplication(Word.ApplicationClass WordApp)
{
尝试
{
对象j_SaveChanges=false;
object j_NullObject=System.Reflection.Missing.Value;
退出(ref j_SaveChanges,ref j_NullObject,ref j_NullObject);
Marshal.ReleaseComObject(WordApp);
WordApp=null;
System.GC.Collect();
GC.WaitForPendingFinalizers();
}
捕获(例外e)
{
//向用户显示错误消息
MessageBox.Show(e.Message);
}
}
}
}
仅供将来参考,请回答我自己的问题。最后我终于明白了
以下代码适用于任何版本的Word fine。当对话框打开时,应用程序冻结并等待结果,就像另一个对话框一样
唯一的时刻是它只允许选择合适的打印机,但不显示预览
_doc.Activate();
_wordApp.Visible = true;
var dialogResult = _wordApp.Dialogs[WdWordDialog.wdDialogFilePrint].Show();
if (dialogResult == 1)
_doc.PrintOut();