Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

740 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