• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1547
  • Last Modified:

Reference items in Word.Application.Documents collection

I'm trying to insert text in an open word document with code called from a .net form. In an attempt to access the open document I have used:
Dim oWord As Word.Application
oWord = GetObject(, "Word.Application")

The following returns 1 when my document is open:
oWord.Documents.Count

but this returns an error (requested collection member doesn't exist):
oWord.Documents.Item(0).Name

Why can I count items in the documents collection but not reference them by name or index?
0
PiedBird
Asked:
PiedBird
  • 4
  • 4
1 Solution
 
Ryan ChongCommented:
perhaps you can try this?

oWord.Documents(0).Name
0
 
PiedBirdAuthor Commented:
oWord.Documents(0).Name

In the immediate window gives:
"Interface 'Documents' cannot be indexed because it has no default property."
0
 
Ryan ChongCommented:
oops, try this instead:

oWord.Documents(1).Name
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ryan ChongCommented:
This is a sample I tried:

Private Sub Command1_Click()
    Dim oWord As Object
    On Error Resume Next
    Set oWord = GetObject(, "Word.Application")
    If Err.Number > 0 Then
        Set oWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    If oWord.Documents.Count > 0 Then
        MsgBox oWord.Documents(1).Name
    End If
    'oWord.Quit
    Set oWord = Nothing
End Sub
0
 
PiedBirdAuthor Commented:
Ryancys, Your sample seems to work but I don't know why. I'll blunder about a bit more trial and error and get back to you with what I find. Thanks
0
 
PiedBirdAuthor Commented:
OK. The start of my problem seemed to be in declaring:
Dim oWord As Word.Application

instead of your:
Dim oWord As Object

Remember my application object did allow a count of its documents so it wasn't a complete dud. It just wouldn't let me know what the documents were. I understand (maybe!) that your object was late bound and more cooperative. I guess it's not exactly the same object - even after the binding I mean. Can we ask it to identify itself to relieve my curiosity?

I'll give you most points for getting me this far but we haven't really conquered the 'why' yet so I'd better keep this open a bit longer.
0
 
Ryan ChongCommented:
I think the problem is on:

oWord.Documents.Item

where actually oWord.Documents don't have a collection of Item objects, this is what I found here.

if you try change back:

Dim oWord As Object

to:

Dim oWord As Word.Application

I think it should works too..
0
 
PiedBirdAuthor Commented:
It doesn't work for me. When I make the change my code editor says:
"Interface 'Word.Application' can't be indexed..."  Funny thing is if I specify item as well as the Word.Application declaration the editor is pacified however I still won't return any documents with 'item()'

The puzzle is:
Dim oWord As Word.Application
oWord = GetObject(, "Word.Application")


seems to create a different thing from:
Dim oWord As Object
Set oWord = GetObject(, "Word.Application")

Only the latter beast has a collection I can view. I'm not sure why but I can live with it. Thanks for your help.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now