|
VBA code that runs from Access to look at Word and get the list of open documents for the RowSource of a listbox. Select the active document.
This listbox shows the names of documents that are open in Word. The active document is highlighted.
Although not covered in this article, the selection is also read since the purpose of this form in Access is to change Word document properties.
In this example, the listbox to show Word documents is named lstWordDocuments. Customize your code accordingly.
'*************** Code Start *********************************************** ' Purpose : code behind a form to change RowSource of a listbox ' with names of open Word documents ' Select the active document ' Author : crystal (strive4peace) ' Code List: www.msaccessgurus.com/code.htm ' This code: https://msaccessgurus.com/VBA/ListboxWordDocuments.htm ' LICENSE : ' You may freely use and share this code, but not sell it. ' Keep attribution. Mark your changes. Use at your own risk. '------------------------------------------------------------------------------ Function ListOpenDocuments() As Boolean 's4p 220807, 221210 'CALLs ' lstWordDocuments_reset '----------- customize 'GLOBAL VARIABLES ' goWord -- Word application ' early binding: as Word.Application ' late binding: as Object ' goDoc -- active Word document ' early binding: as Word.Document ' late binding: as Object 'MODULE VARIABLE ' moDoc -- Word.Document or Object used for looping ListOpenDocuments = False Dim sActiveDocumentName As String Dim sList As String Dim sListboxControlname As String sListboxControlname = "lstWordDocuments" ' --- customize On Error Resume Next If goWord Is Nothing Then Set goWord = GetObject(, "Word.Application") End If On Error GoTo Proc_Err If goWord Is Nothing Then MsgBox "Word isn't open",, "Can't list documents" GoTo Proc_Exit End If sList = "" sActiveDocumentName = "" With goWord If .Documents.Count > 0 Then Set goDoc = .ActiveDocument sActiveDocumentName = goDoc.Name For Each moDoc In .Documents With moDoc sList = sList & """" & .Name & """;" _ & """" & .FullName & """;" End With Next moDoc Else Set goDoc = Nothing Call lstWordDocuments_reset ' --- customize -- clear listbox MsgBox "No documents open" _ ,, "Can't list documents" GoTo Proc_Exit End If End With With Me.Controls(sListboxControlname) .RowSource = Left(sList,Len(sList) - 1) If Nz(.Value, "") <> sActiveDocumentName Then .Value = sActiveDocumentName End If .Requery End With ListOpenDocuments = True Proc_Exit: On Error Resume Next Exit Function Proc_Err: MsgBox Err.Description _ ,, "ERROR " & Err.Number _ & " ListOpenDocuments" Resume Proc_Exit Resume End Function Private Sub lstWordDocuments_reset() '--------- customize for your listbox name '221108 s4p With Me.lstWordDocuments .RowSource = "" .Value = Null End With End Sub '*************** Code End *****************************************************Code was generated with colors using the free Color Code add-in for Access.
ListBox.RowSource property (Access)
Documents.Count property (Word)
Download text file with code: vba_ListOpenDocuments__TXT.zip
If you have trouble with the download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the BAS file. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm
There can be many reasons to read what documents are open in Word. In my case, I'm building an application to manage variable information in Word, and taking heavy advantage of Word document properties.
If you like this page, please let me know, thank you. Donations are always appreciated. Merry Christmas ~
Here's the link for this page in case you want to copy it and share it with someone:
https://msaccessgurus.com/VBA/ListboxWordDocuments.htm
or in old browsers:
http://www.msaccessgurus.com/VBA/ListboxWordDocuments.htm
Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.
While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. So you can learn on your own, I'll give you lots of links to good resources.
Do you manipulate Word documents from Access?
Bookmarks are commonly used, but the problem is that you have to make each change individually.
With properties, you can reference the same value all over the place.
Do you want my help?
Email me at training@msAccessGurus
~ crystal
the simplest way is best, but usually the hardest to see