Opening Workbook from Word VBA

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
49
Office Version
  1. 365
  2. 2016
Hi all - I'm trying to open up a workbook from VBA in Word. Works fine, in the sense that the workbook opens - but when I try to reference the workbook from code in Word, my code thinks that an Excel add-in file that loads on startup is the workbook I just opened(lets call it "MAddIn.xlam"). See the code below - Excel thinks that "oWB" is "MAddIn.xlam", not "MyFile.xlsm"(If I type in "?oWB.name" in the immediate window, it returns "MAddIn.xlam"). Can anyone tell me what I'm doing wrong, and how I can get oWB to reference "MyFile.xlsm" instead of "MAddIn.xlam"? Thanks.

Code:
Sub OpenExcelFile()
    
    Dim oExcel As Excel.Application
    Dim oWB As Workbook
    Set oExcel = New Excel.Application
    Set oWB = oExcel.Workbooks.Open("G:\MyFile.xlsm")
    oExcel.Visible = True


    
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
That's strange ... I don't see any error in that code .... Maybe someone knows.

In the meantime, you could set oWB to the ActiveWorkbook as follows :

Code:
Sub OpenExcelFile()    
    Dim oExcel As Excel.Application
    Dim oWB As Workbook
    Set oExcel = New Excel.Application
    Set oWB = oExcel.Workbooks.Open("G:\MyFile.xlsm")
    oExcel.Visible = True
[COLOR=#0000ff]    Set oWB = oExcel.ActiveWorkbook[/COLOR]
    
End Sub
 
Upvote 0
Sounds like a dodgy addin to me. That said, have you tried repairing the Office installation (via Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair)?
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,055
Members
452,542
Latest member
Bricklin

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