Home All Groups Group Topic Archive Search About

Exception when closing Word document

Author
3 Apr 2006 2:04 AM
Chris Mahoney
Hi

I'm trying to print a Word document from my VB 2005 app, but I'm having
trouble.

Imports Microsoft.Office.Interop
Private oWord As Word.Application
Private WordTemplate As Object = "N:\Checklist.dot"

Public Sub PrintDocument()
    Dim oDoc As Word.Document
    oWord = New Word.Application
    oDoc = oWord.Documents.Add(WordTemplate)
    oDoc.PrintOut()
    oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    oWord.Quit()
End Sub

When I run the code, the document gets printed, but then I get an error
on the .Close line:

System.Runtime.InteropServices.COMException
Exception from HRESULT: 0x800A14EC
   at Microsoft.Office.Interop.Word.DocumentClass.Close(Object&
SaveChanges, Object& OriginalFormat, Object& RouteDocument)

Can anyone tell me what I am doing wrong?

Thanks
Chris

Author
3 Apr 2006 2:52 AM
Stephany Young
Clearly, the Close method required 3 arguments and you have only supplied
one.


Show quoteHide quote
"Chris Mahoney" <chrismaho***@gmail.com> wrote in message
news:1144029893.707398.285860@z34g2000cwc.googlegroups.com...
> Hi
>
> I'm trying to print a Word document from my VB 2005 app, but I'm having
> trouble.
>
> Imports Microsoft.Office.Interop
> Private oWord As Word.Application
> Private WordTemplate As Object = "N:\Checklist.dot"
>
> Public Sub PrintDocument()
>    Dim oDoc As Word.Document
>    oWord = New Word.Application
>    oDoc = oWord.Documents.Add(WordTemplate)
>    oDoc.PrintOut()
>    oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
>    oWord.Quit()
> End Sub
>
> When I run the code, the document gets printed, but then I get an error
> on the .Close line:
>
> System.Runtime.InteropServices.COMException
> Exception from HRESULT: 0x800A14EC
>   at Microsoft.Office.Interop.Word.DocumentClass.Close(Object&
> SaveChanges, Object& OriginalFormat, Object& RouteDocument)
>
> Can anyone tell me what I am doing wrong?
>
> Thanks
> Chris
>
Author
4 Apr 2006 8:22 PM
Chris Mahoney
That worked, and I've possibly found a bug in VB - the tooltip lists
all three arguments as optional.