Difficult closing the .exe process in excel - vba

I'm building a MS-Excel extract from a VB6 application. I'm having a very difficult time getting rid of the .exe process for excel. I generate an extract and save it to C: drive. I don't open it or anything, yet it's still visible in the Task Manager under Processes. Here is how I declare and close all excel components.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
This I do all the way at the end of the procedure
Xlbook.close
Set xlSheetWeek = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Why is the process still working even though I do all the xlbook.close and Don't have excel open. It shouldn't be showing.

You need to call the Quit() method on your Excel.Application object (xlApp), https://msdn.microsoft.com/en-us/library/office/ff839269.aspx:
xlApp.Quit()

You can get crazy and
sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide

Related

Opening Excel and Running Macro From Outlook Leaves Excel Stuck in Task Manager

I have a macro in Outlook that calls an Excel file and runs a macro in that Excel file then closes the file. The problem is After closing Excel it stays in the Task Manager. I have tested this a million times and I even have removed all code in my Excel macro to see if that was the problem but Excel is still is still getting stuck in the task manager. My Outlook code is:
Dim xlApp As Object
Dim xlWB As Workbook
Dim strFile As String
Set xlApp = CreateObject("excel.application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
strFile = "c:\desktop\a.xlsm"
Set xlWB = Workbooks.Open(strFile)
xlApp.Run ("Cleanup")
xlWB.Close False
If Not xlWB Is Nothing Then
Set xlWB = Nothing
End If
xlApp.Quit
If Not xlApp Is Nothing Then
Set xlApp = Nothing
End If
The problem with the code was that I was not opening the workbook with the Excel application that I created. I fixed the problem by adding xlApp here:
Set xlWB = xlApp.Workbooks.Open(strFile)
That could have not been a more simple fix to such a time consuming problem :(

Write in an already opened Excel workbook from Powerpoint Slide

I am trying to write in an already manually opened workbook using VBA from my PowerPoint presentation.
Using CreateObject and then Workbook.Open it opens a new instance of the file.
I'm trying to use GetObject as found in several examples over the web.
Here's my code :
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = Trueme
Set xlWorkbook = xlApp.Workbooks.Open(ActivePresentation.Path & "\Suivi individuel.xlsx", True, False)
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
I've tried:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx", "Excel.Application")
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
It says runtime error 432: File Name or Class Name not found during automation operation.
Try:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx")
If you just want to get an open instance of Excel then
Set xlApp = GetObject(,"Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Wrap that in an error handler if there's a chance Excel might not be open
See: https://support.microsoft.com/en-us/kb/288902

Open Excel File on a specific worksheet through Process.Start() in VB.NET (External Open)

Error
I have a database with excel file names stored together with specific worksheet name for each. What I exactly need is to simply open (external) the excel file with the focus to the specific worksheet name.
I simply tried something like following. But it just opens the excel file with the focus on the default worksheet, not the worksheet I want.
Process.Start("X:\myexcelpath\myexcelworkbook.xlsx", "myworksheet")
So please let me know how I can do this stuff in vb.net
Thank you.
Unfortunately, you cannot do this through a command line. What I would suggest is that you use the Microsoft.Office.Interop.Excel.
How to manipulate an excel workbook with Excel.Workbook
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWS As Excel.Worksheet
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(sFilePath)
xlWS = CType(xlWorkBook.Worksheets(sheetNameOrIndex), Excel.Worksheet)
xlApp.Visible = True

Creating Excel Files using VB2010

I have a form with 1 button to create an Excel file on my desktop.
I get error message:
NullReferenceException Was Unhandled
Object reference not set to an instance of an object
and it highlights the code:
WB = excelapp.workbooks.add
I did add the reference "Microsoft excel 14.0" and my full code is below:
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
WB = excelapp.workbooks.add
excelapp.visible=true
end sub
Missing New on your Excel instance for starters:
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook = xlApp.Workbooks.Add()
Dim xlWorksheet As Excel.Worksheet = CType(xlWorkbook.Sheets("sheet1"), Excel.Worksheet)
xlWorksheet.Cells(1, 1) = "data in first cell"
xlWorksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Test.xlsx")
xlWorkbook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
You should probably put this in Try/Catch/Finally block to catch errors in case you run into issues, but mostly because if the program doesn't continue properly finish that code block, an EXCEL.EXE will remain open in your task manager, as well as whatever Excel file it was accessing will be "in use by another program" when you try to access/modify/delete it.
just add one line, then it'll work
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
excelapp = new excel.application
WB = excelapp.workbooks.add
excelapp.visible=true
end sub

VBA - Excel sometimes fails to close properly

I have a macro in outlook, which helps runs statistics in an excel workbook.
However sometimes it fails to close it properly, and ends up ruining the process, since the workbook is open still, when i run it next time.
This is my method for closing it.
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open(strpath)
...
xlWB.Save
xlWB.Close savechanges:=True
xlApp.Quit
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
From my understanding it should do it.
Did you turn the displayalerts off? Use:
xlApp.DisplayAlerts = False
after you instanciate the Excel application. That prevents Excel from asking for user input ("are you really sure you want to ...?). Such popup could prevent Excel from closing.
Happened to me more than once on an invisable application.