![](/img/trans.png)
[英]How to start new word instance without add-ins when word is already started
[英]Using an already instance of Word
我從來沒有在C#或Visual Studio中編程,我認為入門的唯一方法是潛入並體驗傷害和痛苦並解決問題的產生。 請原諒我的補救問題和缺乏經驗。
我下面有以下代碼,如果尚未打開應用程序,則打開該應用程序。 還返回進程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;
應用程序崩潰。
注意:大多數行已被注釋掉以進行測試。
非常感謝任何幫助,謝謝您的提前。
謝謝您的協助-我的最初嘗試並不遙遠,並且存在一個Visual Studio錯誤,該錯誤已通過最新的15.5.1更新進行了修正---行已修改為->
Word.Application objWord = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Word.Application
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.