Solved

What is the difference between a Document and a Window in Word Automation

Posted on 2011-03-23
3
235 Views
Last Modified: 2012-05-11
I have some Word automation code that operates fine with Office 2003.
When we run the code on a machine with Office 2010, it fails.

The code seems to not differentiate between Windows and Documents - it refers to:

WordApp.Windows(myDoc) and WordApp.Documents(myDoc)

This seems fine for Office 2003. For Office 2010, we get errors saying the items in the Windows collection don't exist.

Can anyone clarify the difference in the two collections and why these errors might appear now?

It is also possible that the differences relate to whether the document is being opened with a template, but this is not conclusive at this point.

Any help is much appreciated.
0
Comment
Question by:AIBMass
  • 2
3 Comments
 

Author Comment

by:AIBMass
ID: 35200962
My current theory is that the items in the collections remain in sync, but that in Office 2010 a reference to wordApp.Windows(myDoc) can not be resolved when myDoc was created with a template.

Does that seem likely?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 35201110
The general rule is to refer to the document via the Documents collection. This will hold all the documents that are open in the current application instance. There is rarely a need to refer to the Window that is being used to display it.

All documents have a template, even if it is simply the Normal one.


You can list documents and Windows separately
Sub Win()
    Dim wnd As Window
    For Each wnd In Application.Windows
        Debug.Print wnd.Document.Name
    Next wnd
End Sub

Sub Docs()
    Dim doc As Document
    For Each doc In Application.Documents
        Debug.Print Document.Name
    Next doc
End Sub

Open in new window

0
 

Author Closing Comment

by:AIBMass
ID: 35201474
The key insight in this solution was that "there is rarely a need to refer to the Window...."

So we took those statements out and recoded them using the Documents collection and everything works!

So we have a simple workaround, but no real understanding of what change was made between the two versions of Office, etc, etc.

Makes me think of going to the doctor: "Doc, it hurts when I refer to the Windows collection.."

"So stop Referring!"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mail Merge with expanding table 16 65
Add a row to a Word Table 23 55
Create a publication in Word from a HTML data 14 64
Populate a word template from Excel 5 42
Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

863 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

20 Experts available now in Live!

Get 1:1 Help Now