我从未在C#或视觉工作室编程,我认为开始的唯一方法是潜入并体验伤害和痛苦并解决出现的问题。请原谅我的补救问题和缺乏经验。
我在下面的代码中打开了一个应用程序,如果它尚未打开。还返回进程ID(以防万一我需要它)一旦应用程序打开,代码集2就会将文档打印到默认打印机,该打印机在打开应用程序之前设置。
using System;
using System.Diagnostics;
namespace ImageIT.PreFlight
{
public class AppCheck
{
public int IsAppOpen (string appCheck)
{
Console.WriteLine("We are in APPCHECK");
var appToStart = Process.GetProcessesByName(appCheck);
Process[] processlist = Process.GetProcesses();
var appID = 0;
if (appToStart.Length == 0)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = appCheck,
CreateNoWindow = true,
Arguments = "/q /n /x",
WindowStyle = ProcessWindowStyle.Hidden,
};
Process.Start(startInfo);
appID = Process.GetProcessesByName(appCheck)[0].Id;
Console.WriteLine(appCheck + " Has been Starterd - Process ID: " + appID);
Console.WriteLine("");
}
else
{
foreach (Process theprocess in processlist)
if (theprocess.ProcessName == appCheck)
{
appID = theprocess.Id;
Console.WriteLine(appCheck + " Has been found - Process ID: " + appID);
Console.WriteLine("");
}
}
return appID;
}
}
}
代码集2
using System;
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
namespace ImageIT.AppPrint
{
public class PrintToImage
{
public void PrintWordDocument(string fileToPrint, int wordID)
{
object objMissing = System.Reflection.Missing.Value;
Console.WriteLine("We are Currently in PrintToImage");
Console.WriteLine("The current Word INSTANCE ID is: "+ wordID);
//Word.Application objWord = new Word.Application();
Word.Application objWord = (Word.Application)Marshal.GetActiveObject("Word.Application") as Word.Application;
//objWord = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Word.Application;
Word.Document objDoc = new Word.Document();
//object fileName = fileToPrint;
//Console.WriteLine("File to Print:" + fileToPrint);
//objDoc = objWord.Documents.Open(ref fileName,
// ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
// ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
// ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
//object copies = "1";
//object pages = "";
//object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
//object items = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
//object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
//object objTrue = true;
//object objFalse = false;
//objDoc.PrintOut(
// ref objTrue, ref objFalse, ref range, ref objMissing, ref objMissing, ref objMissing,
// ref items, ref copies, ref pages, ref pageType, ref objFalse, ref objTrue,
// ref objMissing, ref objFalse, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
//objDoc.Close(ref objFalse, ref objMissing, ref objMissing);
}
}
}
如果我使用
//Word.Application objWord = new Word.Application();
打开一个新的MS-Word实例,文档按预期打印到图像。可能会有数百个文件到图像,所以我试图避免这种情况。
我能找到的关于如何实现这一目标的最佳文章是
然而,当尝试
时//objWord = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Word.Application;
我收到“在当前背景下不存在objWord的名称”
我将其更改为
Word.Application objWord = (Word.Application)Marshal.GetActiveObject("Word.Application") as Word.Application;
应用程序崩溃了。
注意:大部分行已被注释掉以进行测试。
非常感谢任何帮助,谢谢你的进步。
答案 0 :(得分:0)
感谢您的帮助 - 我最初的尝试并没有太远,并且有一个视觉工作室错误已经使用最新的15.5.1更新进行了更正---行被修改为 - >
Word.Application objWord = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Word.Application
;