Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

vba - How to open specific version of Word 2007/2010 in Excel

I have both Word 2007 and 2010 installed. I need to open Word from within Excel but I need to specify which version I need to open within VBA.

I've tried late binding

Dim wordApp2007 As Object
Dim wordApp2010 As Object

Set wordApp2007 = CreateObject("Word.Application.12")
wordApp2007.Visible = True
Set wordApp2010 = CreateObject("Word.Application.14")
wordApp2010.Visible = True

but both open Word 2010

I've also tried early binding by using

Dim wordApp As Word.Application
Set wordApp2007 = New Word.Application
wordApp2007.Visible = True

and setting references to the Word 12.0 object model but this still opens Word 2010 enter image description here

If I register each version of Word using

"C:Program FilesMicrosoft OfficeOffice12WINWORD.EXE" /regserver

"C:Program FilesMicrosoft OfficeOffice14WINWORD.EXE" /regserver

then the version registered opens but then I can't open open the non-registered.

Can anyone help and show me how to open a specific version of Word within Excel using VBA?

Thank you

Edit: Example code....

Option Explicit

Dim wordApp2007 As Word.Application

Sub Word_InfoEarly()
'early binding
Set wordApp2007 = New Word.Application
wordApp2007.Visible = True

    'other Stuff
    Stop

    wordApp2007.Quit
    Set wordApp2007 = Nothing

End Sub


Sub Word_InfoLate()
Dim wordApp2007 As Object
Dim wordApp2010 As Object

    Set wordApp2007 = CreateObject("Word.Application.12")
    wordApp2007.Visible = True
    Set wordApp2010 = CreateObject("Word.Application.14")
    wordApp2010.Visible = True

    'other Stuff
    Stop

    wordApp2007.Quit
    Set wordApp2007 = Nothing
    wordApp2010.Quit
    Set wordApp2010 = Nothing

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a work around:

TaskID = Shell("C:Program FilesMicrosoft OfficeOffice12WINWORD.EXE",vbHide) '2007
'TaskID = Shell("C:Program FilesMicrosoft OfficeOffice14WINWORD.EXE",vbHide) '2010
GetObject(,"Word.Application")

You would also need to test if a previous version of word is open, or use something other than a basic GetObject to activate the window, else there's no guarantees that it will get the right version.

The other way would be to pass the document name in the Shell command, and then GetObject could be called with the document name


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...