Window object in Word 2007

  • Thread starter Peter Karlström
  • Start date
P

Peter Karlström

Hi

I have developed a COM-Addin for Word XP and Word 2003 using VB 6.0.
Now I'm beginning to check what needs do be done when the customer upgrades
to Word 2007.

In my COM-Addin I frequently have used the
"<wordApp>.Windows(docNameVariable).Document" approach to be sure I work with
the correct window.
This seems to be a "NoNo" in the Word 2007 object model, since I get the
error 5941 (Not part of the member collection) when I access any part of the
document, such as Variables and CustomDocumentProperties.

There is a lot to work to be done if I have to change every line of code
which uses this way of locating the document.
Or is it some other thing I have to change for this to work properly?

Thanks in advance
 
J

Jialiang Ge [MSFT]

Hello Peter,

From your post, my understanding on this issue is: 1. How to use
<wordApp>.Windows(docNameVariable).Document in Word 2007, and 2. How to
migrate the COM Addins that were designed for Word 2003, XP, to support
Word 2007. If I'm off base, please feel free to let me know.

For the first question about the use of
<wordApp>.Windows(docNameVariable).Document in Word 2007, based on my test,
this function still runs well on my side. I get the 5941 error only when
the docNameVariable refers to a document that is currently not open. Would
you confirm if docNameVariable is specified correctly on your side? Below
is my detailed test steps:

Step1. Open Word 2007, create a new document "Document1".
Step2. Go to VBA project (Alt+F11), and write the following macro:
Dim oCustomProps As Object
Set oCustomProps =
Application.Windows("Document1").Document.CustomDocumentProperties
For Index = 1 To oCustomProps.Count
oProp = oCustomProps.Item(Index)
MsgBox (oProp.Name & " " & oProp.Type & " " & oProp.Value)
Next
Step3. Run the macro. It does not throw any error.
Step4. Change the document name in the macro from "Document1" to
"Document2" (There is no Document2 opened at the moment).
Step5. Run the macro again, and it throws the error 5941

For you second question about how to migrate the COM addins that were
designed for Word 2003/XP to support Word 2007, I'd suggest the TechNet
article:

Top migration issues in 2007 Office system.
http://technet.microsoft.com/en-us/library/cc179116.aspx

Differences in the 2007 Office system
http://technet.microsoft.com/en-us/library/cc179054.aspx

According to the "Object Model" section in the first article, we indeed
need to test all applications, all lines of code to ensure they are
compatible with the 2007 Office release. I understand this means a lot to
work on your side, but to ensure the compatibility, we have to test the
whole project again. If you encounter any problem during the migration,
feel free to let me know and I do my best to help you.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Karlström

Hi Jialiang Ge

It seems that we're not using the exact same approach. The way I have used
it, can best be described in some sample code below. The variable ActiveDoc
is a global string variable which holds the document name (doc.Name).

These lines of code does not work for me in Word 2007 (but works in Word
2003):
1. Select Case wrdApp.Windows(ActiveDoc).Document.ProtectionType
2. If Not Trim(wrdApp.Windows(ActiveDoc).Document.Variables("WD_doctype"))
<> "" Then
3. tLang =
Trim(wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_Språk"))

Can you explain why my approach doen't work?

Thanks in advance

--
Peter Karlström
Midrange AB
Sweden
 
J

Jialiang Ge [MSFT]

Hello Peter,

I have tried your sample scenarios. I write the following vb6 code and test
it in both Office 2007 and 2003 systems, however, I am still not able to
reproduce the error. It works well on my side.

Private Sub Command1_Click()
Dim wrdApp As Object
Dim testDoc As Object
Dim ActiveDoc As String
ActiveDoc = "TestDoc1.docx" 'in Office 2003, change it to "TestDoc1.doc"

Set wrdApp = CreateObject("Word.Application")
Set testDoc = wrdApp.Documents.Add()
testDoc.SaveAs "d:\" & ActiveDoc

Select Case wrdApp.Windows(ActiveDoc).Document.ProtectionType
End Select

wrdApp.Quit
Set wrdApp = Nothing
End Sub

Would you try it on your side and tell me if it works? I also suggest you
debug your VB program, and check if the value of ActiveDoc is correct.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
P

Peter Karlström

Hi Jialiang Ge

Thanks for your reply.

I also get your sample code to work, but my application is a little more
complicated than just opening and working in one document.

Some of the subroutines opens other documents in the background in order to
get some Autotext-entries or copy some preformated text-pieces.
And since we have code running in the document_WindowActivate event (in
order to control acces to a toolbars buttons) I have been forced to implement
some workarounds.
This makes the application rather complex, and my guess is that word has
problems working with the documents using the
<object>.Windows(docVar).Document - approach under these circumstances.

If I change approach and use the document-object method instead, using a
global Word.Document variable, the parts that I have tested seems to work
just fine.
So I think this will be the way to go, even if it meens that I have to work
through this projects 15000 lines of code.

Thanks again for your support

Regards
 
J

Jialiang Ge [MSFT]

Hello Peter,

I am sorry to hear that the problem causes so much work to do on your side.
But without a reproduce, it's very difficult for me to identify the
problem. Sorry again that I did not do many helps in this issue. If you
encounter any other problem, please let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top