?
Solved

Reference items in Word.Application.Documents collection

Posted on 2007-04-10
8
Medium Priority
?
1,536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 53

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 53

Expert Comment

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

oWord.Documents(1).Name
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Accepted Solution

by:
Ryan Chong earned 1500 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 53

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: 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.

Question has a verified solution.

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

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 …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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