opening word doc vba runtime error

Stevetron

New Member
Joined
Jul 9, 2020
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi, I have some code that returns a runtime error.

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 'All works up to this point
    Dim wordapp
    Dim strFile As String
 
 
    strFile = "C:\Users\STron\Desktop\Instructions for cable list.docx"
    Set wordapp = CreateObject("word.Application")
    wordapp.documents.Open strFile
    wordapp.Visible = True
    wordapp.Activate
Else
Worksheets("Sheet2").Activate
End If
End Sub

The runtime error I receive is below.
1595865798313.png
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It sounds as though the file is not being found
Let's confirm that

Insert a line which returns a message box
What does the message box say?

Rich (BB code):
    Dim answer As VbMsgBoxResult
    answer = MsgBox("Is this your first time using this spreadsheet?", vbYesNo + vbQuestion, "Instructions")
  
    If answer = vbYes Then 'All works up to this point
        Dim wordapp
        Dim strFile As String
        strFile = "C:\Users\STron\Desktop\Instructions for cable list.docx"
        If Dir(strFile) = "" Then MsgBox "File not found" Else MsgBox "File found"
        Set wordapp = CreateObject("word.Application")
        wordapp.documents.Open strFile
        wordapp.Visible = True
        wordapp.Activate
    Else
        Worksheets("Sheet2").Activate
    End If
End Sub
 
Upvote 0
Thanks for your reply.

I have inserted the line you suggested and i am getting "file not found"

This is strange as i copy and pasted the file location from the file properties. I have double checked spelling too.
 
Upvote 0
I have now sorted this.

For some reason the file has .rtf in the file name
Thank you for your help

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
    Dim wordapp
    Dim strFile As String
 
 
    strFile = "C:\Users\STron\Desktop\Instructions for cable list.rtf.docx"
    'If Dir(strFile) = "" Then MsgBox "File not found" Else MsgBox "File found"
    Set wordapp = CreateObject("word.Application")
    wordapp.documents.Open strFile
    wordapp.Visible = True
    wordapp.Activate
Else
Worksheets("List of cables").Activate
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

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