Solved

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

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
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.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

728 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