VB.NET Need explanation for Excel.Application - vb.net

I am very confused as to how Excel.Application works.
Suppose I have the following code:
Dim xlApp As New Excel.Application
Dim xlWbs As Excel.Workbooks
Dim xlWb As Excel.Workbook
xlWbs = xlApp.Workbooks
xlWb = xlWbs.Open("C:\Users\OPA\Desktop\Outage Macro Project Folder\Customized Outage Macro for Testing.xlsm") 'consider this to be wb1
xlApp.visible = true
xlApp.Run(A macro that creates another workbook wb2) 'this creates another workbook wb2
Why is it when I then write:
xlApp.Quit()
Both Wb1 and Wb2 are terminated? But I did not explicitly set up a reference between Wb2 and xlApp! How did xlApp become the parent of Wb2??
How can I set up an excel reference that explicitly refer to Wb2 after it is created? (i.e. xlNewWb = xlApp.workbooks(wb2) 'this doesn't work) so that when I say xlApp.quit(), wb2 do not cease to exist?
Thank you!

How did xlApp become the parent of Wb2??
This line:
xlApp.Run(A macro that creates another workbook wb2)
executed the macro in the context of xlApp.
How can I set up an excel reference that explicitly refer to Wb2 after it is created, [...] so that when I say xlApp.quit(), wb2 do not cease to exist?
Create a second instance of Excel.Application and execute your macro there.

Related

Need to get the last worksheet name in msgbox

I need to get the last (rightmost worksheet) name in msgbox.
I used Sheets(Sheets.Count) to get last sheet. But its only giving first sheets name. Kindly help me on this.
Here is my code.
Sub ShowMRNumber()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open("location")
Set xlSheet = xlApp.Sheets(Sheets.Count)
MsgBox "MR No. is" & vbNewLine xlSheet.Name
xlApp.Workbooks.Close
End Sub
Your line saying
Set xlSheet = xlApp.Sheets(Sheets.Count)
should actually be
Set xlSheet = xlBook.Sheets(xlBook.Sheets.Count)
Using xlApp.Sheets is probably not an issue, as that would probably have defaulted to the active workbook within the xlApp instance of Excel, but Sheets.Count (without a xlApp or xlBook qualifier) would not have been referring to a workbook open in a different instance of Excel - it would have been referring to the active workbook in the instance of Excel where the code was running.

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 :(

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.