I’m attempting to have an existing word doc open from when a commandbutton is clicked from an excel user form:
This is the direction I’m heading in and its not working…any ides
Application.Open “C:Documents and SettingsBBondDesktopCaseName.doc”
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Open a exisitng Word doc (Microsoft office/excel)
Try the following (you’ll need to set a reference to word in tools/references if you haven’t already done so). the 9 in Word.Application.9 is for excel 2000 – change both occurences to 8 for 97 and 10 for XP.
Private Sub CommandButton1_Click()
Call OpenWordDoc
End Sub
Sub OpenWordDoc
Dim WordObj As Word.Application
On Error Resume Next
Err.Number = 0
Set WordObj = GetObject(, “Word.Application.9”)
If Err.Number = 429 Then
Set WordObj = CreateObject(“Word.Application.9″)
Err.Number = 0
End If
On Error GoTo 0
WordObj.Visible = True
WordObj.Documents.Open FileName:=”C:Documents and SettingsBBondDesktopCaseName.doc”
End Sub
You can make Brooke’s solution more version agnostic. Also, by changing to “late binding,” you bypass the requirement to set a reference in your template.
Sub OpenWordDoc
Dim WordObj As Object ‘ Word.Application
On Error Resume Next
Err.Clear ‘ Err.Number = 0
Set WordObj = GetObject(, “Word.Application”) ‘ GetObject(, “Word.Application.9”)
If Err.Number = 429 Then
Set WordObj = CreateObject(“Word.Application”) ‘ CreateObject(“Word.Application.9”)
Err.Clear ‘ Err.Number = 0
End If
On Error GoTo 0
WordObj.Visible = True
WordObj.Documents.Open FileName:=”C:Documents and SettingsBBondDesktopCaseName.doc”
End Sub
(I changed Err.Number = 0 to Err.Clear because I’m not sure that the original syntax fully clears the error condition.)
I don’t think you actually need to clear the error. I have used the following code:
On Error Resume Next
Set WordObj = GetObject(, “Word.Application”)
If Err.Number = 429 Then
Set WordObj = CreateObject(“Word.Application”)
End If
On Error Goto ErrorHandler
Literally for years without any problems.
Brooke’s method will give you a starting point to continue if you wish to then carry out some automation tasks in the freshly opened word file.
A simpler suggestion would be to use FollowHyperlink if you don’t want the code to do anything to the file other than open it.
ActiveWorkbook.FollowHyperlink Address:=”C:Documents and SettingsBBondDesktopCaseName.doc”
I have a similar objective. From a macro, I want to do the equivalent of double-clicking a particular Word template (so that an unnamed and unsaved new document opens), and I’d dearly like to avoid some of the more convoluted calculations needed when using GetObject and similar.
ActiveWorkbook.FollowHyperlink Address:= doesn’t work. It opens the template, not a document based on the template.
Anyone know an easy way of doing it?
Thanks
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.