Accessing an open word Doc

Salrandin

New Member
Joined
Jun 27, 2011
Messages
32
Office 2003,

How to I set my document variable to a word document that is already open? Would I use the documents.add function? And what would the path be if it is a newly created document? Thanks.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Code:
Sub ListOpenWordDocs()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim strDocs As String
    
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        MsgBox "No active instance of MS Word"
        Exit Sub
    End If
    On Error GoTo 0
    
    For Each objDoc In objWord.Documents
        strDocs = strDocs & objDoc.Name & vbCrLf
    Next
    
    MsgBox strDocs
End Sub
 
Upvote 0
Code:
Sub ListOpenWordDocs()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim strDocs As String
 
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        MsgBox "No active instance of MS Word"
        Exit Sub
    End If
    On Error GoTo 0
 
    For Each objDoc In objWord.Documents
        strDocs = strDocs & objDoc.Name & vbCrLf
    Next
 
    MsgBox strDocs
End Sub

Cool, is objword an array of word documents? I have little understanding of the word data structures. And also if I wanted to modify say "Document1" within those docs how do I differentiate? Thanks for all the help.
 
Upvote 0
objWord is a reference to the Word application. objWord.Documents is the array of current documents.

Take a look at the code below which looks for an open document named "Document1" and inserts the word "hello" at the end.

Code:
Sub ListOpenWordDocs()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        MsgBox "No active instance of MS Word"
        Exit Sub
    End If
    On Error GoTo 0
    
    For Each objDoc In objWord.Documents
        If objDoc.Name = "Document1" Then
            Exit For
        End If
    Next
    
    If objDoc Is Nothing Then
        Exit Sub
    End If
    
    'objDoc should now be a reference to "Document1" and it can be modified
    objDoc.Range.Select

    With objWord.Selection
        .EndKey wdStory 'move the insertion point to the end of the document
        .TypeParagraph
        .TypeParagraph
        .TypeText "hello"
    End With
End Sub
 
Upvote 0
objWord is a reference to the Word application. objWord.Documents is the array of current documents.

Take a look at the code below which looks for an open document named "Document1" and inserts the word "hello" at the end.

Code:
Sub ListOpenWordDocs()
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
 
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        MsgBox "No active instance of MS Word"
        Exit Sub
    End If
    On Error GoTo 0
 
    For Each objDoc In objWord.Documents
        If objDoc.Name = "Document1" Then
            Exit For
        End If
    Next
 
    If objDoc Is Nothing Then
        Exit Sub
    End If
 
    'objDoc should now be a reference to "Document1" and it can be modified
    objDoc.Range.Select
 
    With objWord.Selection
        .EndKey wdStory 'move the insertion point to the end of the document
        .TypeParagraph
        .TypeParagraph
        .TypeText "hello"
    End With
End Sub

Thanks again! Now if I want to access a table that already exists in "Document1" and if there are multiple tables would it be something similar? Thanks.
 
Upvote 0
Dim objTable as Word.Table 'to declare the object

for each objTable in objDoc.tables
'do stuff
next
 
Upvote 0
Dim objTable as Word.Table 'to declare the object

for each objTable in objDoc.tables
'do stuff
next

Thanks! Hopefully one last thing.. :)

When I input a string into a cell in a MS word table and later try to access it, the value changes.

For instance if I input "SOUTH" into a cell and then try to access it though excel it gives me:

"SOUTH" and a box character. This is troublesome as I can no longer logically match the two values. I tried saving the cell value to a string but it still keeps the box character. Any ideas? Thanks.
 
Upvote 0
It's probably the cell marker, which is a circle with four lines on the corners (yeah I know circle with corners!!!).

When getting the string just do:

myStr=left(myStr,len(myStr)-1)

to get rid of it
 
Upvote 0
It's probably the cell marker, which is a circle with four lines on the corners (yeah I know circle with corners!!!).

When getting the string just do:

myStr=left(myStr,len(myStr)-1)

to get rid of it

Genius...

I should have thought of that. Thanks alot man!
 
Upvote 0
No problem.

The thing to remember with Word is that there is always a hidden character at the end of every paragraph/table cell which contains the formatting for it.
 
Upvote 0

Forum statistics

Threads
1,214,574
Messages
6,120,327
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top