0

Exception thrown every time in console application while running this simple code

    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

If i use debug mode this function works perfect. Otherwise

Log check: 30.12.2015. 11:17:15 Originalna greška je : Object reference not set to an instance of an object. Line 34

Line 34>>>WordDoc = convertObj.Documents.Open(Path)

myString
  • 47
  • 3
  • 13
  • .Documents seems to be [Nothing](http://stackoverflow.com/questions/10837437/interop-word-documents-open-is-null). _"The Document object is a member of the Documents collection. The Documents collection contains all the Document objects that are currently open in Microsoft Word."_. Are there any documents opened? Additionally `_checkLog` `file` and `Path` params. – Alex B. Dec 30 '15 at 12:07
  • Hey sir i think that might be a problem. My file and path params are good `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` – myString Dec 30 '15 at 12:21
  • But when i try to convert Excell i got this error `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,.....` – myString Dec 30 '15 at 12:22
  • New research i put _log on file before proceed. The function returns True if the file is open. The results :( `Log check: 30.12.2015. 13:34:25 File open : False` which means file is not opened for sure. – myString Dec 30 '15 at 12:35
  • Have you tried the first answer of my SO link I posted? – Alex B. Dec 30 '15 at 12:51
  • Alex B. that solved my problem thanks a lot. Post it as answer so i can click solved. I will give my best and try to find simplier solution . Maybe to set this `Identity` through code. – myString Dec 30 '15 at 13:01

1 Answers1

1

Open DCOM Config Settings:

1. Start -> dcomcnfg.exe
2. Computer
3. Local Computer
4. Config DCOM
5. Search for Microsoft Word 97-2003 Documents -> Properties

Tab Identity, change from Launching User to Interactive User.

Alex B.
  • 2,145
  • 1
  • 16
  • 24