10

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 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
Pang
  • 9,564
  • 146
  • 81
  • 122
  • Can you please post the code which creates the object, then calls the registered version of MS Word? – EastOfJupiter Aug 22 '12 at 13:23
  • I've edited my question to show both declaration and assign of the objects –  Aug 22 '12 at 13:38
  • I 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 Aug 22 '12 at 13:42
  • Do 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? – EastOfJupiter Aug 22 '12 at 13:57
  • No 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. –  Aug 22 '12 at 14:06
  • This related SuperUser question may help http://superuser.com/q/362906 – barrowc Aug 22 '12 at 21:51

5 Answers5

3

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

SeanC
  • 15,695
  • 5
  • 45
  • 66
  • 1
    +1 Logically this should shell the correct app, unless MS have some irritating validation in place to always launch the newer version – Matt Donnan Aug 22 '12 at 13:52
  • Using 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. –  Aug 22 '12 at 14:03
  • @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. – Scott Holtzman Aug 22 '12 at 15:50
  • Try the WshShell object instead of the native Shell function. Have a look [this previous answer](http://stackoverflow.com/a/8906912/119775) of mine. – Jean-François Corbett Aug 23 '12 at 07:18
  • @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! –  Aug 23 '12 at 14:17
1

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

JeffK
  • 11
  • 1
1

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.

aaa
  • 857
  • 4
  • 25
  • 46
Andy Brown
  • 5,309
  • 4
  • 34
  • 39
0

This is a VB.NET solution:

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

This is a bit intimidating to some, but there may be a registry edit that can solve this. I am unable to test as I only have one version of MS Office available to me, however, previous versions still have registry keys left over.

I found the 2007 version of Word in the registry, and it's default location is C:\program Files\Microsoft Office\Office14\WINWORD.EXE" indicating that older versions of Word are registered to the newest version install location as it's new default.

What you might be able to do is navigate to the registry location

HKEY_CLASSES_ROOT\Word.Documet.12\shell\Open\Command 

Change the (Default) key to read "C:\program Files\Microsoft Office\Office12\WINWORD.EXE" /n "%1"

In theory whenever

Set wordApp2007 = CreateObject("Word.Application.12")

is invoked it may probe the registry for the location of the executable, and find the correct path.

R3uK
  • 14,417
  • 7
  • 43
  • 77
EastOfJupiter
  • 781
  • 5
  • 11
0

I had a similar issue, and thought I would detail my experience for those that stumble across this in the future.

In my situation, I had a Powerpoint macro that was supposed to open a file dialog for the user to select some Excel files and then create tables from the data; I had no problem with it until I recently installed Excel 2003. Now Powerpoint was opening up an Excel 2003 file dialog, which would raise errors when trying to select a *.xlsx file. It didn't matter if I used Excel.Application.10 or Excel.Application.14 in my code to create the Excel object, it was always an Excel 2003 file dialog.

I noticed in Explorer that *.xlsx files were set to be opened in Excel 2010 and *.xls files were set to be opened in Excel 2003. I tried to usual way to reset *.xls files to be opened in 2010 to no avail. I ended up having to delete the registry key and repair Office 2010. Now that all Excel files open in 2010, my problem has been fixed.

I know my problem was a bit different than yours, but I think my experience could help lead to a solution. I think any solution will end up relying on some registry editing.

Ironbeard
  • 467
  • 4
  • 8