Solved

Reference items in Word.Application.Documents collection

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

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 51

Expert Comment

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

oWord.Documents(1).Name
0
Independent Software Vendors: 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 51

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 51

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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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