C#代码实现 调用 word2010 问津菜单的打印 功能?
不需要值调出Word打印预览界面或值显示打印设置界面,要支持预览并能界面直接选择打印机

C#代码实现 调用 word2010 问津菜单的打印 功能?
不需要值调出Word打印预览界面或值显示打印设置界面,要支持预览并能界面直接选择打印机

guoxuen 上午好☀️☀️☀️️
本答案参考ChatGPT-3.5
实现调用Word 2010菜单的打印功能需要使用Office.Interop.Word库来操作Word应用程序。以下是一个实现该功能的代码示例:
using Microsoft.Office.Interop.Word;
public class WordPrinter
{
public void PrintDocument(string filePath, int copies, string printerName)
{
Application wordApp = new Application();
Document wordDoc = null;
try
{
// 打开Word文档
wordDoc = wordApp.Documents.Open(filePath);
// 设置打印机和份数
wordApp.ActivePrinter = printerName;
wordDoc.PrintOut(Copies: copies, Pages: Type.Missing, Range: WdPrintOutRange.wdPrintAllDocument);
// 关闭Word文档
wordDoc.Close();
}
finally
{
// 释放Word对象
if (wordDoc != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
wordDoc = null;
}
if (wordApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;
}
}
}
}
调用示例:
WordPrinter printer = new WordPrinter();
printer.PrintDocument("C:\\Path\\To\\Document.docx", 7, "Printer Name");
解决方案:
请注意,这种方法是通过使用COM来操作Word应用程序实现的,需要确保系统中已安装Word 2010并具备足够的权限来使用COM对象。