No open document error but document is open!!

mickn66

Well-known member
Joined
Nov 2, 2002
Messages
53
I have a simple vb.net form that has the following at the beginning of its class definition:

Dim wordApp As New Word.Application()
Dim actDoc As Word.Document = wordApp.ActiveDocument

I already have Word 2000 running with a document open. And yet, when I run my program, I get this error:

"An unhandled exception of type System.Runtime.InteropServices.COMException
occurred in pdfer.exe

Additional information: This command is not available because no document is open."

But, like I said, there is a document open! Any ideas why Im getting this error? Thanks.
 
heres a quick example to start you off ....
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim objWord As Object = GetObject(, "Word.Application")
            If Not objWord Is Nothing Then
                Dim doc As Microsoft.Office.Interop.Word.DocumentClass = DirectCast(objWord.GetType.InvokeMember("activedocument", Reflection.BindingFlags.GetProperty, Nothing, objWord, Nothing), Microsoft.Office.Interop.Word.DocumentClass)
                MessageBox.Show(doc.Name) /// check you have the document which is open on your desktop.
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
 
Thank you very much for your help! However, I did have a problem with your code. The line "Dim doc As Microsoft.Office.Interop.Word.DocumentClass" gives me an error saying that Microsoft.Office.Interop.Word.DocumentClass is undefined. I figure this means I havent referenced something I need to reference. But I have references to both the Microsoft Word 9.0 Object Library and to Microsoft Office 9.0 Object Library. What am I missing?

Thanks again for your help :)
 
you would just use Dim actDoc As Word.Document as you already are , eg:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim objWord As Object = GetObject(, "Word.Application")
            If Not objWord Is Nothing Then
                Dim doc As Word.Document = DirectCast(objWord.GetType.InvokeMember("activedocument", Reflection.BindingFlags.GetProperty, Nothing, objWord, Nothing), Word.Document)
                MessageBox.Show(doc.Name) /// check you have the document which is open on your desktop.
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
im using office xp 2003 , hence the interop thing :)
 
I think I solved it. I changed
"Microsoft.Office.Interop.Word.DocumentClass" to "Word.documentclass" and it worked. Im not sure exactly why I needed to make that change though, so if you can figure that out, let me know :)

Thanks again
 
Lol - i think you must have been writing up your reply at the same time I was :) Thanks again
 
Back
Top