How to open specific version of Word 2007/2010 in Excel
Solution 1
This is a work around:
TaskID = Shell("C:\Program Files\Microsoft Office\Office12\WINWORD.EXE",vbHide) '2007
'TaskID = Shell("C:\Program Files\Microsoft Office\Office14\WINWORD.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
Solution 2
This may further explain why the code works some times and not others.
My observation on the situation of the command
'Set wordAppxxxx = CreateObject("Word.Application.xx")'
working or not on your computer is that it is a function of the latest update you get from Microsoft.
Why I believe this:
I have an application that converts a text file to a Word document for backwards compatibility with some legacy apps. The best plan includes using a version of Word similar to the version the legacy apps were designed with/to. As a result, I searched on how to invoke a legacy version of Word as opposed to the default offering on my computer which is Word 2010.
The solution noted in this discussion chain provided the answer to my question. (Thank you Stack Overflow contributors!) I wanted to use Word XP, so I looked at my directories and observed that Word XP (aka Word 2002) is a member of Office 10, so I created the command
'Set wordApp2002 = CreateObject("Word.Application.10")'
and my program launched Word 2002 and the world was a happy place.
Over the weekened, I had an update to my computer. I control the updates via an app which gives me control over when updates occur such that I can observe changes to my configuration. This morning (9/30/13) I turned on a computer that had a Word update. I did not know this until after I had made one run of my app from last week. The app ran fine and invoked Word 2002 as expected.
But then I got the banner page informing me of a Word 2010 update that was installing itself.
Afterwards, I ran the app that worked so well for me last week and once today. Now, after the Word update (immediately after!), the same code now launches Word 2010 despite the fact that the command line invoking Word 2002 has not changed.
This appears strong evidence that a Microsoft update tweaked the settings that previously allowed the VB code to work as expected. This might be a good item to bring to Microsoft's attention so see if we can get this item stabilized in subsequent update packages to allow consistent behavior in future releases.
I hope this is helpful,
JeffK
Solution 3
I wasted half a day on this, and want to help prevent others doing the same! I'm running Windows 7 and Office 2013 and 2010 on the same laptop. I wanted to get Access VBA to open up an old version of Word, as Word 2013 call-outs are printing with thick black borders.
Having tried lots of variations, here's my code which worked:
Sub GetWordReference()
'finally got Access to open old version of Word
'open Word 2010
Shell "C:\Program Files (x86)\Office 2010\Office14\winword.exe"
'open Word 2013
'Shell "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"
TryAgain:
On Error GoTo NoWord
Set word2010 = GetObject(, "Word.Application")
On Error GoTo 0
word2010.Visible = True
'word2010.Documents.Add
'word2010.Selection.TypeText "This is Word " & word2010.Version
Exit Sub
NoWord:
Resume TryAgain
End Sub
I can't get the SO code editor to show this correctly, but copying and pasting should work.
Comments
-
Pang almost 2 years
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
If I register each version of Word using
"C:\Program Files\Microsoft Office\Office12\WINWORD.EXE" /regserver
"C:\Program Files\Microsoft Office\Office14\WINWORD.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
-
EastOfJupiter over 11 yearsCan you please post the code which creates the object, then calls the registered version of MS Word?
-
Admin over 11 yearsI've edited my question to show both declaration and assign of the objects
-
EastOfJupiter over 11 yearsI wasn't clear. I appologize, but I'd like the entire subroutine(s). It's hard to see what might be missing or incorrect without the whole block.
-
EastOfJupiter over 11 yearsDo you have both documents loading, but in only one instance of MS Word? For example, if you minimize Word, do all documents minimize with it, or just the specific document leaving all other word documents visible?
-
Admin over 11 yearsNo 2 application of Word open, both are the current registered version of Word. I do want 2 applications open but the first being Word2007 and the second Word2010.
-
barrowc over 11 yearsThis related SuperUser question may help superuser.com/q/362906
-
-
Matt Donnan over 11 years+1 Logically this should shell the correct app, unless MS have some irritating validation in place to always launch the newer version
-
Admin over 11 yearsUsing the Shell starts the Office configuration process if the version is not the current registered version. It then fails on GetObject as it's not ready for a few minutes.
-
Admin over 11 years
Dim wordApp2007 As New Object
doesn't compile. -
Scott Holtzman over 11 years@ooo - a bit of a hack, but you could use
Application.Wait
to tell VBA to hang out for a bit while the Shell does its thing. -
Jean-François Corbett over 11 yearsTry the WshShell object instead of the native Shell function. Have a look this previous answer of mine.
-
CaBieberach over 11 yearsdrop the
New
. UseDim wordApp2007 As Object
-
EastOfJupiter over 11 years@CaBieberach - The OP already had that line of code, which does not work. I've spent quite a bit of time lately doing .NET development, and I caught that as a perceived bug, however, I was incorrect.
-
Admin over 11 years@Jean-François Corbett - thanks for that. Essentially using Sean's idea with WshShell works. Not what I was expecting and seems a bit of a hack but it'll do for now. Thanks everyone!