?
Solved

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

Posted on 2011-03-23
3
Medium Priority
?
243 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
[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
  • 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 1500 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

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

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
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) …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

741 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