ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   excel macro to open word doc besed on user selection (https://www.excelbanter.com/excel-programming/433201-excel-macro-open-word-doc-besed-user-selection.html)

looper

excel macro to open word doc besed on user selection
 
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




JLGWhiz[_2_]

excel macro to open word doc besed on user selection
 
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



"Looper" wrote in message
...
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







All times are GMT +1. The time now is 03:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com