Close Excel Application from Word

matapagi2019

New Member
Joined
Mar 8, 2019
Messages
8
How can I close excel application with commandButton2 in ms word after I open it with CommandButton1 with vba code :
[Vba}
Private Sub CommandButton1_Click()
Dim ObjExcel

Dim ObjXls
Set ObjExcel = CreateObject("Excel.Application")
Set ObjXls = ObjExcel.Workbooks.Open("D:\Agendaku.xls")
ObjExcel.Visible = True
End Sub

Thanks.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,407
Re: Word VBA to Close Excel Application

Try this:
Code:
Private Sub CommandButton2_Click()
    Dim ObjExcel As Object
    Dim ObjXls As Object
    Set ObjExcel = GetObject(, "Excel.Application")
    Set ObjXls = ObjExcel.Workbooks("Agendaku.xls")
    ObjXls.Close SaveChanges:=True ' or False
End Sub
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,666
Office Version
  1. 2016
Platform
  1. Windows
Re: Word VBA to Close Excel Application

To close the entire excel application, I would use something along these lines :

Code:
Private Sub CommandButton2_Click()

    Dim ObjExcel As Object
    Dim ObjXls As Object
    
    On Error Resume Next
    
    Set ObjXls = GetObject("D:\Agendaku.xls")
    If Err.Number = 0 Then
        Set ObjExcel = ObjXls.Application
        ObjXls.Close SaveChanges:=True ' or False
        ObjExcel.Application.Quit
      Else
        MsgBox "Unable to locate workbook!"
    End If
    
End Sub
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,629
Re: Word VBA to Close Excel Application

matapagi2019: Please don't resurrect old threads. The one you posted in was 8 years old. Start a new thread instead.

I have split your discussion off to a separate thread.
 

matapagi2019

New Member
Joined
Mar 8, 2019
Messages
8

ADVERTISEMENT

Re: Word VBA to Close Excel Application

Try this:
Code:
Private Sub CommandButton2_Click()
    Dim ObjExcel As Object
    Dim ObjXls As Object
    Set ObjExcel = GetObject(, "Excel.Application")
    Set ObjXls = ObjExcel.Workbooks("Agendaku.xls")
    ObjXls.Close SaveChanges:=True ' or False
End Sub


Wow..it worked.
Seem it's so easy for you as an expert.
Thank you for two things, your help and your fast response.
Thanks a lot..!
 

matapagi2019

New Member
Joined
Mar 8, 2019
Messages
8
Re: Word VBA to Close Excel Application

To close the entire excel application, I would use something along these lines :

Code:
Private Sub CommandButton2_Click()

    Dim ObjExcel As Object
    Dim ObjXls As Object
    
    On Error Resume Next
    
    Set ObjXls = GetObject("D:\Agendaku.xls")
    If Err.Number = 0 Then
        Set ObjExcel = ObjXls.Application
        ObjXls.Close SaveChanges:=True ' or False
        ObjExcel.Application.Quit
      Else
        MsgBox "Unable to locate workbook!"
    End If
    
End Sub


It worked too.
Thank you too for your help and your fast response.
Thanks a lot..!
 

matapagi2019

New Member
Joined
Mar 8, 2019
Messages
8
Re: Word VBA to Close Excel Application

matapagi2019: Please don't resurrect old threads. The one you posted in was 8 years old. Start a new thread instead.

I have split your discussion off to a separate thread.


I'm really sorry for this.
Just meant to focus on that topic, so I didn't pay attention to the date posted.
Forgive me as a newbie here.
However, I was very happy following this forum that I never did before.
And so grateful getting good and helpful friends.

Thank you all..
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,826
Messages
5,855,866
Members
431,771
Latest member
CoryMelth

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
Top