Can anyone help with this please?

I my VB.NET app to list each of the documents that are currently open in Microsoft Word. If any docs are not saved then I need to save them. I have tried using the Word application object but creating a new App object doesn't enable me to see existing Word documents that are already open.

I have been trying to use GetProcessByID but once I have identified the process I am unable to perform simple tasks with the process such as SaveAs or iterate through Word's Documents collection and list the names of each document. Any ideas? please?

I am using Word 2007.

I reckoned that it really wouldn't be so difficult but this problem is really causing me a lot of trouble.

Dani AI

Generated

Short summary and a practical route forward (ties to and ):

Creating a new Word.Application starts a fresh instance; a System.Diagnostics.Process is an OS process handle and cannot be cast into a COM Word.Application. The correct approach is to get the running COM object that Word registered in the Running Object Table (ROT). In .NET this is done with Marshal.GetActiveObject or, from VB, Microsoft.VisualBasic.Interaction.GetObject. Once the COM Application is obtained the Documents collection can be enumerated and each document's Saved state inspected and saved.

Example (VB.NET, add a reference to the Word 12.0 Interop assembly):

Try
    Dim app = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"), Microsoft.Office.Interop.Word.Application)
    For Each doc As Microsoft.Office.Interop.Word.Document In app.Documents
        If Not doc.Saved Then
            doc.Save()
        End If
    Next
Catch ex As System.Runtime.InteropServices.COMException
    ' No running instance or access denied
End Try

Troubleshooting notes and cautions:

  • GetActiveObject will throw if no COM-registered Word instance exists or if running under a different user/session/integrity level. Elevated vs non-elevated processes can prevent access.
  • If multiple Word instances are started in odd ways, enumerating the ROT is required to find each registered instance; see the Running Object Table docs.
  • Automating Office from services or server-side code is unsupported by Microsoft; prefer a desktop-interactive process for this automation.

References: Marshal.GetActiveObject (docs), Interaction.GetObject (docs), and the Running Object Table overview ().

Recommended Answers

All 5 Replies

The Document object has a Saved property. If that is true, there are no changes to be saved.

If doc.Saved <> true Then
   ...
End If

Thank you for replying so promptly - although unfortunately you have not quite grasped the problem.

Yes the Word document object does have a Saved property but the difficulty is that I cannot get at it. I have no reference to an already opened Word application object nor its Documents collection.

Code such as

Dim appWord as Word.Appliction
appWord = New Word.Application


does not let see documents that are currently open in Word but simply starts a new instance of Word.

Does anyone have any experience with using GetProcessByID or something similar? If so how do I assign my appWord object to a process that is already running? Any ideas please?

I hope this link will help you -

Unfortunately doesn't have the answer either.

Does anyone know whether I can use a Process in VB.NET and somehow cast it as a Word app object? I have tried to use CreateObjRef but I don't have a sufficient understanding of this function to use it properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.