open word doc using VBA

Stevetron

New Member
Joined
Jul 9, 2020
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to open a word doc using vba. I have a code that will open the file, but the file does not appear ontop. I would like the word doc to be on the screen after the code runs.

This is what i have:
Sub OpenDocFromExcel()
Dim wordapp
Dim strFile As String


strFile = "C:\Users\barteam.FRASER-AS\Desktop\Questions for cable machine.docx"
Set wordapp = CreateObject("word.Application")
wordapp.documents.Open strFile
wordapp.Visible = True
End Sub

Any help is much appreciated
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try adding activate below visible:

VBA Code:
wordapp.Activate
 
Upvote 0
Absolutely perfect. So simple....if you know how.

Thank you again Trevor
 
Upvote 0
So if i want to merge my 2 lots of code, it does not seem to work, i am guessing i have missed a link of some kind?

VBA Code:
Private Sub Workbook_Open()
Dim answer As VbMsgBoxResult
answer = MsgBox("Is this your first time using this spreadsheet?", vbYesNo + vbQuestion, "Instructions")
If answer = vbYes Then
    Sub OpenDocFromExcel() 'this is where it falls down
    Dim wordapp
    Dim strFile As String
 
 
    strFile = "C:\Users\barteam.FRASER-AS\Desktop\Questions for cable machine.docx"
    Set wordapp = CreateObject("word.Application")
    wordapp.documents.Open strFile
    wordapp.Visible = True
    wordapp.Activate
Else
Worksheets("Sheet2").Activate
End If
End Sub
 
Upvote 0
sorry, i made a mistake, it falls down on a line above

VBA Code:
Private Sub Workbook_Open()
Dim answer As VbMsgBoxResult
answer = MsgBox("Is this your first time using this spreadsheet?", vbYesNo + vbQuestion, "Instructions")
If answer = vbYes Then 'it falls down on the "answer" part of this line
    Sub OpenDocFromExcel()
    Dim wordapp
    Dim strFile As String
 
 
    strFile = "C:\Users\barteam.FRASER-AS\Desktop\Questions for cable machine.docx"
    Set wordapp = CreateObject("word.Application")
    wordapp.documents.Open strFile
    wordapp.Visible = True
    wordapp.Activate
Else
Worksheets("Sheet2").Activate
End If
End Sub
 
Upvote 0
Try taking out the line it highlights assuming it is this line as this points to another sub routine.

VBA Code:
Sub OpenDocFromExcel()
 
Upvote 0
Happy to help and thank you again for letting me know you have a working solution. ;)
 
Upvote 0

Forum statistics

Threads
1,213,552
Messages
6,114,278
Members
448,559
Latest member
MrPJ_Harper

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top