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.
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

vaskov17

Well-known Member
Joined
Apr 27, 2011
Messages
920
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
 

Salrandin

New Member
Joined
Jun 27, 2011
Messages
32
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.
 

vaskov17

Well-known Member
Joined
Apr 27, 2011
Messages
920
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
 

Salrandin

New Member
Joined
Jun 27, 2011
Messages
32

ADVERTISEMENT

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.
 

vaskov17

Well-known Member
Joined
Apr 27, 2011
Messages
920
Dim objTable as Word.Table 'to declare the object

for each objTable in objDoc.tables
'do stuff
next
 

Salrandin

New Member
Joined
Jun 27, 2011
Messages
32

ADVERTISEMENT

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.
 

vaskov17

Well-known Member
Joined
Apr 27, 2011
Messages
920
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
 

Salrandin

New Member
Joined
Jun 27, 2011
Messages
32
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!
 

vaskov17

Well-known Member
Joined
Apr 27, 2011
Messages
920
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,217
Messages
5,594,887
Members
413,947
Latest member
gizmolucy

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
Top