Results 1 to 8 of 8

Thread: [RESOLVED] how to stop Excel creating extra books

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    193

    Resolved [RESOLVED] how to stop Excel creating extra books

    it seems that every Excel workbook I create in Vb, Excel automatically creates a ghost book with the name Bookn.xls where the n is incremental. After a lot of processing, I could have 20+ blank books open and running in taskmanager!!!
    Any ideas on how to prevent this??
    thanks

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: how to stop Excel creating extra books

    Debbie, how are you interacting with Excel... Can you show us some code?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    193

    Re: how to stop Excel creating extra books

    sure.. first, I call this sub to create the file, then I open and add sheets as I need

    Public Sub Create_Excel_File(ByVal sPath As String, ByVal sName As String)

    Dim pExcel_App As Excel.Application
    Dim pExcel_Wkbook As Excel.Workbook
    Dim wkbSheet As Excel.Worksheet

    Try

    pExcel_App = New Excel.Application
    pExcel_Wkbook = pExcel_App.Workbooks.Add
    '== add worksheet to file
    wkbSheet = pExcel_Wkbook.Worksheets.Add

    Dim sOutExcelPath As String
    sOutExcelPath = sPath & "\" & sName
    pExcel_Wkbook.SaveAs(sOutExcelPath)

    pExcel_App.DisplayAlerts = False
    pExcel_App.ActiveWorkbook.Close(SaveChanges:=True, Filename:=sOutExcelPath)
    pExcel_App.Quit()


    Catch ex As Exception
    MsgBox("ERROR: " & m_Current_Address & " - Create Excel log file for - " & mDistrictName & " - " & ex.Message)
    Finally
    pExcel_App = Nothing
    pExcel_Wkbook = Nothing
    wkbSheet = Nothing
    End Try

    End Sub


    '==== this is adding sheets and data

    pExcel_App = New Excel.Application
    pExcel_Wkbook = pExcel_App.Workbooks.Open(mCurrent_Folder & "\" & mDistrict & ".xls")

    '== add Summary worksheet to file
    wkbSheet = pExcel_Wkbook.Worksheets.Add
    wkbSheet.Name = sWKsheetName & " Summary"

    wkbSheet2 = pExcel_Wkbook.Worksheets.Add
    wkbSheet2.Name = mDistrict_DisplayName

    '== do headings for summary
    With wkbSheet

    .Cells(9, 1) = "blah blah"...

    '== close excel
    Dim sOutExcelPath As String
    sOutExcelPath = mCurrent_Folder & "\" & mDistrict
    pExcel_App.DisplayAlerts = False
    pExcel_Wkbook.SaveAs(sOutExcelPath)

    pExcel_App.ActiveWorkbook.Close(SaveChanges:=True, Filename:=sOutExcelPath)
    pExcel_App.Quit()


    Cheers....

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: how to stop Excel creating extra books

    Try it this way... I just typed this in Notepad so it is untested...

    Code:
    Imports Excel = Microsoft.Office.Interop.Excel
    Public Sub Create_Excel_File(ByVal sPath As String, ByVal sName As String)
    
        Dim pExcel_App As Excel.Application, pExcel_Wkbook As Excel.Workbook
        Dim wkbSheet As Excel.Worksheet, sOutExcelPath As String
        
        sOutExcelPath = sPath & "\" & sName
        pExcel_App = New Excel.ApplicationClass
        pExcel_Wkbook = pExcel_App.Workbooks.Add
        wkbSheet = pExcel_Wkbook.Worksheets.Add
        
        pExcel_App.DisplayAlerts = False
        pExcel_App.ActiveWorkbook.Close(SaveChanges:=True, Filename:=sOutExcelPath)
        pExcel_Wkbook.SaveAs (sOutExcelPath)
        
        pExcel_Wkbook.Close()
        pExcel_App.Quit()
    
        releaseObject (pExcel_App)
        releaseObject (pExcel_Wkbook)
        releaseObject (wkbSheet)
    End Sub
    
    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
    Use the same way to release object in the second part of the code...

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    193

    Talking Re: how to stop Excel creating extra books

    Koolsid,

    THat did it, I shouldv'e thot of that myself. thanks so much

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] how to stop Excel creating extra books

    Glad to be of help
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Posts
    193

    Re: [RESOLVED] how to stop Excel creating extra books

    Bad news, it cleared the instance great for 1, but when I do the subsequent excel books it mysteriously stops, no errors at all.
    It took a while to find out exactly where it goes off the grid, but this is the line..


    System.Runtime.InteropServices.Marshal.ReleaseComObject (obj)
    obj = Nothing

    I even reversed the order and eliminated them one by one, but it seems that doing the Release again does not work!!!! seems kinda stupid to me. THe object is totally new each time it's created.
    So, for now I am living with multiple instances in task manager until the pc is restarted...

  8. #8
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: [RESOLVED] how to stop Excel creating extra books

    Hi
    If you still got problem, add this when you are done with your Excel stuff:

    Code:
     Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("Excel")
            For Each p As Process In pProcess
                p.Kill()
            Next
    This will close all Excel books.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width