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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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..!
 
Upvote 0
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..!
 
Upvote 0
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..
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,048
Members
448,543
Latest member
MartinLarkin

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