ble*_*ter 5 vsto windows-services office-interop c#-4.0
我对Microsoft Office有一个非常奇怪的问题.
我有一个公共库,其唯一目的是打开传递给它的任何word文档文件类型(通过完整文件路径...)并将打开的word文档保存为pdf文件.
奇怪的问题是,如果我从Windows服务中使用该库,每当它尝试打开word文档时,我都会得到一个null ...也就是说,word文档永远不会被打开.
但是,如果我从WPF或Windows Form应用程序中使用库,我从来没有遇到任何问题.我知道线程存在问题,(单线程公寓)但是我不知道如何修复它以解决Windows服务问题.:( :( :(
我将不胜感激任何帮助!我得到的错误是以下内容:
异常消息:{"对象引用未设置为对象的实例."}(参考word文档).内在例外:无效; HResult:-2147467261.数据:ListDictionaryInternal有0个条目; 堆栈跟踪:位于c:\ Project Files ...\DocumentConverter.cs中的DocumentConverter.ToPdf(String currentWorkingFolderPath,String pathToDocumentToConvert):第209行
所以这是库函数.它需要Microsoft Office引用,该引用由Visual Studio Tools for Office创建.
private string ToPDF(string currentWorkingFolderPath, string pathToDocumentToConvert)
{
string temporaryPdfFolderPath = Path.GetFullPath(currentWorkingFolderPath + "\\pdf\\");
string temporaryPdfFilePath = Path.GetFullPath(temporaryPdfFolderPath + "\\pdffile.pdf");
if (!FileSystem.CreateDirectory(temporaryPdfFolderPath))
{
return null;
}
try
{
Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
object objectMissing = System.Reflection.Missing.Value;
wordApplication.Visible = false;
wordApplication.ScreenUpdating = false;
FileInfo wordFile = new FileInfo(pathToDocumentToConvert);
Object fileName = (Object)wordFile.FullName;
// This is where it breaks when called from windows service. Use the dummy value as a placeholder for optional arguments
Document wordDocument = wordApplication.Documents.Open(ref fileName, ref objectMissing,
true, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing);
object outputFileName = (object)temporaryPdfFilePath;
object fileFormat = WdSaveFormat.wdFormatPDF ;
// Save document into PDF Format
wordDocument.SaveAs(ref outputFileName,
ref fileFormat, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing,
ref objectMissing, ref objectMissing, ref objectMissing, ref objectMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)wordDocument).Close(ref saveChanges, ref objectMissing, ref objectMissing);
wordDocument = null;
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((Microsoft.Office.Interop.Word._Application)wordApplication).Quit(ref objectMissing, ref objectMissing, ref objectMissing);
wordApplication = null;
}
catch (Exception ex)
{
//logging code
return null;
}
return temporaryPdfFilePath;
}
Run Code Online (Sandbox Code Playgroud)
ble*_*ter 24
Per @Sameer S在他的帖子中:在Windows Server 2008上支持Office 2003互操作吗?
Microsoft Windows Server 2008不支持官方Microsoft Office 2003 Interop.
但经过大量的代码和搜索的排列和组合之后,我们遇到了一个适用于我们场景的解决方案.
解决方案是插入Windows 2003和2008维护其文件夹结构的方式之间的差异,因为Office Interop依赖于桌面文件夹进行文件打开/保存.2003系统包含systemmprofile下的桌面文件夹,该文件夹于2008年缺席.
因此,当我们在2008年根据相应的层次结构创建此文件夹时,如下所示; 办公室Interop能够根据需要保存文件.此桌面文件夹需要在下创建
C:\ WINDOWS\system32 \设置\ systemprofile
和
C:\ WINDOWS\Syswow64资料\设置\ systemprofile
多谢你们..!
MS 在类似服务器的场景(如 ASP.NET 或 Windows Service 或类似)中不支持Office Interop - 请参阅http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2!
您将需要使用一些库来实现您想要的:
创建这些文件夹后:
C:\ Windows \ System32 \ config \ systemprofile \桌面C:\ Windows \ SysWOW64 \ config \ systemprofile \ Desktop
确保计划任务正在使用可以访问这些文件夹的配置文件运行。