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.
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,416
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
7,930
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,445
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..
 

Watch MrExcel Video

Forum statistics

Threads
1,108,806
Messages
5,524,987
Members
409,614
Latest member
wile2u

This Week's Hot Topics

Top