Opening Word from Excel.

Rick2000

New Member
Joined
May 6, 2011
Messages
4
Just started learning about VBA. Despite following the codes below from the tutorials, I still get the "Run-time error 91: Object variable or With block variable not set" error . Don't know where I am going wrong and I would appreciate your advice.

Many thanks in advance.

Rick.


My codes are:

Sub OpenWordDoc()
Dim wdapp As Word.Application
Dim wddoc As Word.Document

On Error Resume Next

Set wdapp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdapp = GetObject("Word.Application")
End If
On Error GoTo 0

wdapp.Documents.Open ("C:\Documents.docx")

Set wddoc = wdapp.ActiveDocument

wdapp.Visible = True


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Rick and welcome to the Board
A few little errors. Compare this one to yours.
Code:
Sub OpenWordDoc()
Dim wdapp As Object 'object not Application
Dim wddoc As Object 'object again

On Error Resume Next

Set wdapp = CreateObject("Word.Application") 'create NOT Get
    If Err.Number <> 0 Then 'Word isn't already running
    Set wdapp = GetObject("Word.Application") 'Create NOT Get
    End If
On Error GoTo 0
wdapp.documents.Open Filename:="C:\Documents.docx" 'Filename statement added ANDMake sure the path and filename are correct 
Set wddoc = wdapp.ActiveDocument

wdapp.Visible = True
End Sub

HTH
 
Upvote 0
Hi Rick and welcome to the Board
A few little errors. Compare this one to yours.
Code:
Sub OpenWordDoc()
Dim wdapp As Object 'object not Application
Dim wddoc As Object 'object again

On Error Resume Next

Set wdapp = CreateObject("Word.Application") 'create NOT Get
    If Err.Number <> 0 Then 'Word isn't already running
    Set wdapp = GetObject("Word.Application") 'Create NOT Get
    End If
On Error GoTo 0
wdapp.documents.Open Filename:="C:\Documents.docx" 'Filename statement added ANDMake sure the path and filename are correct 
Set wddoc = wdapp.ActiveDocument

wdapp.Visible = True
End Sub

HTH

Thanks Michael. I tried you suggestions but didn't work.
For some reason, I only got it to work if I use:
Dim wdapp as Word.Application
Dim wddoc as Word.Document

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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