【发布时间】:2015-12-30 10:24:14
【问题描述】:
运行此简单代码时,每次在控制台应用程序中都会引发异常
Public Function convertWord(ByVal file As String, ByVal Path As String) As Integer
Dim convertObj As Microsoft.Office.Interop.Word.Application
Dim WordDoc As Microsoft.Office.Interop.Word.Document
Try
convertObj = New Microsoft.Office.Interop.Word.Application
WordDoc = New Microsoft.Office.Interop.Word.Document
WordDoc = convertObj.Documents.Open(Path)
WordDoc.ExportAsFixedFormat(file, WdExportFormat.wdExportFormatPDF)
Return 2
Catch ex As Exception
_checkLog("Originalna greška je : " & ex.Message & ex.StackTrace)
Return 5
End Try
End Function
如果我使用调试模式,这个功能就完美了。否则
Log check: 30.12.2015. 11:17:15 Originalna greška je : Object reference not set to an instance of an object. Line 34
第 34 行>>>WordDoc = convertObj.Documents.Open(Path)
【问题讨论】:
-
.Documents 似乎是Nothing。 “Document 对象是 Documents 集合的成员。Documents 集合包含当前在 Microsoft Word 中打开的所有 Document 对象。”。有没有打开的文件?另外
_checkLogfile和Path参数。 -
嘿,先生,我认为这可能是个问题。我的文件和路径参数很好
Log check: 30.12.2015. 13:19:12 File je : G:\POSLOVNO\menzur\635870783515338248.pdf Log check: 30.12.2015. 13:19:12 Path je : G:\POSLOVNO\menzur\635870783515338248.docx Log check: 30.12.2015. 13:19:13 Originalna greška je : Object reference not set to an instance of an object. at OfficeConvert.convertWord(String file, String Path) in C:\Users\Desktop\servis\OfficeConvert.vb:line 37 -
但是当我尝试转换 Excell 时出现此错误
Microsoft Excel cannot access the file '...0680735.xlsx'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook. at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter,..... -
新研究我在继续之前将 _log 存档。如果文件打开,该函数返回 True。结果:(
Log check: 30.12.2015. 13:34:25 File open : False这意味着文件肯定没有打开。 -
您是否尝试过我发布的 SO 链接的第一个答案?
标签: vb.net console-application