On Fri, Feb 12, 2010 at 2:50 PM, David Boccabella
<[email protected]> wrote:
> 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

Not needed in VB.
>
>         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
>                 Dim objDoc As Microsoft.Office.Interop.Word.Document

You're trying to create this document. No need to create a blank one first.

>                 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 = objApp.Documents.Open(sFilename)

Perhaps you were testing, but VB provides the Missings for you.

Now to the debugging:
Seeing as it is not throwing an exception, I assume there are
situatios when Document.Open can return Nothing.

You could check to see if the document has been opened in any case:
    If objDoc Is Nothing Then
     ' I don't know if Normal.dot or other documents are open already,
you may need to adjust the zeros here.
     If objApp.Documents.Count>0 Then _
      objDoc = objApp.Documents(0)
    End If
>
>
>                     objDoc.Activate()
>
> ERROR HERE!!!!!!
>
<snip>
>
>                     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

'I'd add this clean up here
                       Try
                          objApp.Quit()
                       Catch
                       End Try
>                 End Try
>
>                 objDoc = Nothing
>
>                 objApp = Nothing
>
<snip>
> ---------------------------------------------------------

-- 
Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)

Reply via email to