+ Reply to Thread
Results 1 to 2 of 2

How to close specific workbook not all active workbooks?

  1. #1
    Bon
    Guest

    How to close specific workbook not all active workbooks?

    Hello all

    I have assign a specific workbook to the workbook variable. But, it
    doesn't close the specific workbook. It closes all active workbooks.
    How can I solve this problem?

    My module:

    Sub CopyWorksheetContentToWork(strFilePath As String)
    Dim xlApp As Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim WorkbookToWorkOn As String
    Dim ExcelWasNotRunning As String

    WorkbookToWorkOn = strFilePath

    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")

    If Err Then
    ExcelWasNotRunning = True
    Set xlApp = CreateObject("Excel.Application")
    End If

    Set xlWorkbook = xlApp.Workbooks(WorkbookToWorkOn)

    'Code for copying Excel worksheets content to ActiveDocument

    xlApp.Visible = False
    xlWorkook.Close SaveChangee:=False
    xlApp.Quit
    Set xlWorkbook = Nothing
    Set xlApp = Nothing
    End Sub

    Could anyone point out my mistakes?
    Thank you very much

    Cheers
    Bon


  2. #2
    K Dales
    Guest

    RE: How to close specific workbook not all active workbooks?

    It is when you do XlApp.Quit. You are closing down the Excel session
    entirely, so all workbooks and Excel itself will close.

    If you always want to leave Excel open when done, just take that line out.
    If you want it open only if it was open initially, use your variable
    ExcelWasNotRunning to determine if you should do the XlApp.Quit or not.
    --
    - K Dales


    "Bon" wrote:

    > Hello all
    >
    > I have assign a specific workbook to the workbook variable. But, it
    > doesn't close the specific workbook. It closes all active workbooks.
    > How can I solve this problem?
    >
    > My module:
    >
    > Sub CopyWorksheetContentToWork(strFilePath As String)
    > Dim xlApp As Excel.Application
    > Dim xlWorkbook As Excel.Workbook
    > Dim WorkbookToWorkOn As String
    > Dim ExcelWasNotRunning As String
    >
    > WorkbookToWorkOn = strFilePath
    >
    > On Error Resume Next
    > Set xlApp = GetObject(, "Excel.Application")
    >
    > If Err Then
    > ExcelWasNotRunning = True
    > Set xlApp = CreateObject("Excel.Application")
    > End If
    >
    > Set xlWorkbook = xlApp.Workbooks(WorkbookToWorkOn)
    >
    > 'Code for copying Excel worksheets content to ActiveDocument
    >
    > xlApp.Visible = False
    > xlWorkook.Close SaveChangee:=False
    > xlApp.Quit
    > Set xlWorkbook = Nothing
    > Set xlApp = Nothing
    > End Sub
    >
    > Could anyone point out my mistakes?
    > Thank you very much
    >
    > Cheers
    > Bon
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1