Solved

Reference items in Word.Application.Documents collection

Posted on 2007-04-10
8
1,517 Views
Last Modified: 2008-01-09
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
Comment
Question by:PiedBird
  • 4
  • 4
8 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 18887688
perhaps you can try this?

oWord.Documents(0).Name
0
 

Author Comment

by:PiedBird
ID: 18887734
oWord.Documents(0).Name

In the immediate window gives:
"Interface 'Documents' cannot be indexed because it has no default property."
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 18887804
oops, try this instead:

oWord.Documents(1).Name
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 18887806
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
 

Author Comment

by:PiedBird
ID: 18887840
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
 

Author Comment

by:PiedBird
ID: 18887929
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
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 18888230
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
 

Author Comment

by:PiedBird
ID: 18893444
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question