excel macro to open word doc besed on user selection

L

Looper

I need to create a macro to be run from excel that will open a word document.
The name of the word document will be defined by the user and is vaiable
based on user input. I have very limited VB experience and can be classified
as a "cut and paste guy"

through trial and error i have assembled the code below which doesnt work.
Please supply easily understood answer.

Thank you,

Sub OpenWordDocumentFromExcel()

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim p As String
Sheets("lists").Visible = True
Sheets("lists").Select
p = Range("p2").Value
Sheets("lists").Visible = False

'On Error GoTo BadShow
Dim oWord As Object
Set oWord = CreateObject("Word.application")
oWord.Documents.Open Name = p
oWord.Visible = True
AppActivate oWord
Set oWord = Nothing
Exit Sub
'BadShow:
'oWord.Quit
Set oWord = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
J

JLGWhiz

Two things I noticed:

1. Range("P2") would need to contain the complete path, including the file
name.
2. The syntax for this:
oWord.Documents.Open Name = p
should be this:
oWord.Documents.Open FileName:= p
 

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