Solved

VB.NET 2010 and MS Excel

Posted on 2014-03-19
7
231 Views
Last Modified: 2014-03-19
I have a VB.NET 2010 app that creates a new test report based on a template XLSX file. When I run my program and get back to my main form, if I go to the Task Manager I can see that Excel.exe process is still running. If I exit my program then the Excel process disappears from the task manager.
I have shown my code below. Is there some other command I am missing that will stop the Excel process?

Thanks,
Charlie

CODE:
Public Sub CreateReportA()

        If appExcel Is Nothing Then
            appExcel = New Excel.Application
        End If

        appExcel.Visible = False

        Dim myPath As String = Application.StartupPath
        Dim mynewfilename As String
        Dim myDate = Format(Now, "yyyy_MM_dd_hh_mm_ss")

        mynewfilename = myPath & "\" & "TestReports\" & rpt_Serial_Number_A & "_" & myDate & ".xlsx"

        reportFileNameSysA = mynewfilename

        oWB = appExcel.Workbooks.Open(myPath & "\" & "TestReportFinal.xlsx", True, True)
        oSheet = oWB.Sheets(1)
        oSheet.Range("rpt_Serial_Number").Value = rpt_Serial_Number_A
        oSheet.Range("rpt_Part_Number").Value = rpt_Part_Number_A
        oSheet.Range("rpt_ATP_Version").Value = rpt_ATP_Version_A
        oSheet.Range("rpt_Internal_Order").Value = rpt_Internal_Order_A
        oSheet.Range("rpt_Technician").Value = rpt_Technician_A
        oSheet.Range("rpt_Test_Date").Value = rpt_Test_Date_A

        oWB.SaveAs(mynewfilename)

        oSheet = Nothing
        oWB.Close()
        oWB = Nothing
        appExcel.Quit()
        appExcel = Nothing

    End Sub
0
Comment
Question by:charlieb01
  • 3
  • 2
  • 2
7 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi charlieb01;

I believe what may be happening is that the com objects are not being released which causes Excel to remain running. try using the ReleaseComObject as shown below.

System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet)
oSheet = Nothing
oWB.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB)
oWB = Nothing
appExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel)
appExcel = Nothing

Open in new window

0
 
LVL 14

Expert Comment

by:Matti
Comment Utility
This is a unkonwn syntax Dim myDate = Format(Now, "yyyy_MM_dd_hh_mm_ss")
Now is for Date and Time is for specific timing Format returns a String in defined format

Matti
0
 

Author Comment

by:charlieb01
Comment Utility
Hi Fernando,
I tried your suggestion but the Excel process is still running (as seen in Task Manager) and does not stop until I end my program. Any other ideas? I am really stumped by this.

Thanks in advance


Matti:
By creating the variable 'myDate' formatted as I did in my program and then appending that to the end of the new filename, I am assured that the new spreadsheet filename will be unique. It also allows my user to see when the test was run simply by looking at the entire filename.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
If you have any other com objects associated with the Excel object they too need to be released. Com objects will not end until all the objects reference counts goes to zero.
0
 
LVL 14

Expert Comment

by:Matti
Comment Utility
Well I think there is no 64bit solution Win32 API would have but this is a VBA/64bit app issue, so that you run someting interopp that's 32 bit port to 64 bit code. As you run this in Visual Studio try 32bit compilation, so the compile options from VS.
0
 

Author Comment

by:charlieb01
Comment Utility
Fernando,
Do you know of anyway I can determine com objects that are running?
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Sorry but I do not.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Paging GridView 7 32
Setting runtime form location 4 18
Showdialog 8 20
Get list of word ducuments in a folder 10 13
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now