Hi Folks

Sorry - I realised I had posted one of the older pieces of code that I was
trying to track issues with

 

I am tearing my hair our with this piece of code. I need to use Word within
a service to convert MHT docs to PDF's. I can make this work if I use the
code within a form however not within a service.

 

Here is the code

Now - everything seems to work well until I try and reference objDoc after
opening a document. It returns Object reference not set to an instance of an
object. Whenever I try and see the properties etc.

 

 

I desperately need a solution.  All suggestsion re not using Word in
service, use other product etc cheerfully ignored. I Need to use Word!!!

 

----------------------------------------------------------------------------
-------------------------------

Private Shared Function ProcessWord(ByVal sFilename As Object, ByVal
sFilePDF As Object, ByVal iFormat As Integer) As Integer

 

 

  

        Dim sError As String

        Dim missing As Object = System.Reflection.Missing.Value

 

        For Each proc As Process In Process.GetProcessesByName("WINWORD")

            proc.Kill()

        Next proc

 

        sFilename = "c:\temp\siam2.doc"

        sFilePDF = "c:\temp\aaaa1.pdf"

        iFormat = 17

 

 

        If sFilename.Length > 0 And sFilePDF.Length > 0 Then

            If File.Exists(sFilename) Then

                Dim objApp As New Microsoft.Office.Interop.Word.Application

                Dim objDoc As New Microsoft.Office.Interop.Word.Document

                Try

 

                    objApp.Visible = False

                    objApp.ScreenUpdating = False

                    objApp.DisplayAlerts = WdAlertLevel.wdAlertsNone

                    WriteLogEvent("One", 1006, EventLogEntryType.Error, "")

                    objDoc = objApp.Documents.Open(sFilename, missing,
missing, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing)

 

                    objDoc.Activate()

ERROR HERE!!!!!!

 

                    Dim a1 As String = objDoc.Name

 

                    WriteLogEvent(a1, 1001, EventLogEntryType.Error, "")

 

                    WriteLogEvent("two", 1006, EventLogEntryType.Error, "")

 

                    objDoc.SaveAs(FileName:=sFilePDF,
FileFormat:=WdSaveFormat.wdFormatPDF)

                    WriteLogEvent("three", 1006, EventLogEntryType.Error,
"")

                    objDoc.Close(WdSaveOptions.wdDoNotSaveChanges)

 

                    WriteLogEvent("four...", 1006, EventLogEntryType.Error,
"")

                    ProcessWord = 3

                    WriteLogEvent("five..", 1006, EventLogEntryType.Error,
"")

 

                    objApp.Quit()

                    WriteLogEvent("six..", 1006, EventLogEntryType.Error,
"")

 

                Catch ex As Exception

                    sError = "ProcessWord Abort:" & sFilename & ":-" &
ex.Message.ToString

                    WriteLogEvent(sError, 1006, EventLogEntryType.Error, "")

                    ProcessWord = 4

                End Try

                objDoc = Nothing

                objApp = Nothing

            Else

                ProcessWord = 2

            End If

        Else

            sError = "ProcessWord Error:" & sFilename & ":- Not Exist"

            WriteLogEvent(sError, 1005, EventLogEntryType.Error, "")

            ProcessWord = 1

        End If

 

 

 

 

    End Function

 

 

 

    End Function

 

 

---------------------------------------------------------

Reply via email to