Solved

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

Posted on 2011-03-23
3
234 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

707 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