Solved

Reference items in Word.Application.Documents collection

Posted on 2007-04-10
8
1,485 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 49

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 49

Expert Comment

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

oWord.Documents(1).Name
0
 
LVL 49

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 49

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now