Results 1 to 10 of 10

Thread: Why won't Excel process end!? ** RESOLVED **

  1. #1

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Angry Why won't Excel process end!? ** RESOLVED **

    I wrote a program to check if a SSN is valid based on high-group listings from the Social Security Administration. I am too lazy to create a needed database file from the info manually, so I created a program to download the information for me and create the file.

    The problem is that Excel is still running as a process when the code execution stops. The Excel process doesn't stop until I close the program. What am I doing wrong!!

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WordApp As Word.Application
    4. Dim ExcelApp As Excel.Application
    5.  
    6. Private Sub Form_Load()
    7.     lstStatus.Clear
    8.     Inet1.Protocol = icHTTP
    9. End Sub
    10. Private Sub cmdGet_Click()
    11.     ProcessFile
    12. End Sub
    13. Private Sub ProcessFile()
    14.     cmdGet.Enabled = False
    15.     DoEvents
    16.     Dim strURL As String
    17.     Dim bData() As Byte      ' Data variable
    18.     Dim intFile As Integer   ' FreeFile variable
    19.     strURL = "http://www.ssa.gov/foia/highgroupdownloads/HG0903.doc"
    20.     intFile = FreeFile()
    21.    
    22.     ' The result of the OpenURL method goes into the Byte
    23.     ' array, and the Byte array is then saved to disk.
    24.     lstStatus.AddItem "Opening Social Security Website . . ."
    25.     bData() = Inet1.OpenURL(strURL, icByteArray)
    26.     lstStatus.AddItem "Downloading SSN document . . ."
    27.     Open App.Path & "\ssn.doc" For Binary Access Write As #intFile
    28.     Put #intFile, , bData()
    29.     Close #intFile
    30.    
    31.     lstStatus.AddItem "Converting SSN document to text file . . ."
    32.     Set WordApp = New Word.Application
    33.     With WordApp
    34.         .WindowState = wdWindowStateMinimize
    35.         .Visible = False
    36.         .DisplayAlerts = wdAlertsNone       'hide word alerts msgboxes
    37.         .Documents.Open App.Path & "\SSN.doc"
    38.         .ActiveDocument.SaveAs FileName:=App.Path & "\SSN.txt", _
    39.             FileFormat:=wdFormatText, _
    40.             LockComments:=False, _
    41.             Password:="", _
    42.             AddToRecentFiles:=False, _
    43.             WritePassword:="", _
    44.             ReadOnlyRecommended:=False, _
    45.             EmbedTrueTypeFonts:=False, _
    46.             SaveNativePictureFormat:=False, _
    47.             SaveFormsData:=False, _
    48.             SaveAsAOCELetter:=False
    49.         .Documents.Close False
    50.     End With
    51.     WordApp.Application.Quit False
    52.     Set WordApp = Nothing
    53.    
    54.     lstStatus.AddItem "Reading SSN text file into array . . ."
    55.     Dim strData As String
    56.     Dim intData(1000, 1) As Integer
    57.     Dim i As Integer
    58.  
    59.     intFile = FreeFile()
    60.     i = 0
    61.  
    62.     Open App.Path & "\SSN.txt" For Input As #intFile
    63.     Do While Not EOF(intFile)
    64.         'get group
    65.         Input #intFile, strData
    66.         intData(i, 0) = Val(strData)
    67.         'exit loop if we run out of data but the file still
    68.         '  has "stuff" in it
    69.         If intData(i, 0) = 0 Then Exit Do
    70.         'get area
    71.         Input #intFile, strData
    72.         intData(i, 1) = Val(strData)
    73.         'increment counter
    74.         i = i + 1
    75.     Loop
    76.     Close #intFile
    77.    
    78.     lstStatus.AddItem "Saving SSN text file as CSV file . . ."
    79.     intFile = FreeFile()
    80.  
    81.     Open App.Path & "\SSN.txt" For Output As #intFile
    82.     Dim j As Integer
    83.     For j = 0 To i - 1
    84.         Write #intFile, intData(j, 0), intData(j, 1)
    85.     Next j
    86.     Close #intFile
    87.    
    88.     lstStatus.AddItem "Reading SSN CSV file into Excel . . ."
    89.     Set ExcelApp = New Excel.Application
    90.     With ExcelApp
    91.         .WindowState = xlMinimized
    92.         .Visible = False
    93.         'hide excel alerts msgboxes
    94.         .DisplayAlerts = False
    95.         .Workbooks.OpenText FileName:=App.Path & "\SSN.txt", _
    96.             Origin:=xlWindows, _
    97.             StartRow:=1, _
    98.             DataType:=xlDelimited, _
    99.             TextQualifier:=xlDoubleQuote, _
    100.             ConsecutiveDelimiter:=False, _
    101.             Tab:=False, _
    102.             Semicolon:=False, _
    103.             Comma:=True, _
    104.             Space:=False, _
    105.             Other:=False, _
    106.             FieldInfo:=Array(Array(1, 1), Array(2, 1))
    107.     lstStatus.AddItem "Creating new HG.dbf file . . ."
    108.         .ActiveSheet.Range("A1:B1050").Select
    109.         .Selection.Sort Key1:=Range("A1"), _
    110.             Order1:=xlAscending, _
    111.             Header:=xlGuess, _
    112.             OrderCustom:=1, _
    113.             MatchCase:=False, _
    114.             Orientation:=xlTopToBottom
    115.         .ActiveSheet.Range("A1").Select
    116.         .Selection.EntireRow.Insert
    117.         .ActiveCell.FormulaR1C1 = "Area"
    118.         .ActiveSheet.Range("B1").Select
    119.         .ActiveCell.FormulaR1C1 = "Group"
    120.         .ActiveSheet.Range("A1").Select
    121.         .ActiveWorkbook.SaveAs FileName:=App.Path & "\HG.dbf", _
    122.             FileFormat:=xlDBF4, _
    123.             CreateBackup:=False
    124.         .ActiveWorkbook.Saved = True
    125.         .ActiveWorkbook.Close
    126.         .Workbooks.Close
    127.     End With
    128.     ExcelApp.Application.Quit
    129.     Set ExcelApp = Nothing
    130.     lstStatus.AddItem "Process complete"
    131. End Sub
    Last edited by Armbruster; Oct 10th, 2003 at 01:14 PM.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    G'Day Armbruster,

    I successfully tested the Excel portion of your code. (i made a.txt to test with).

    Maybe it's getting held up somewhere.
    What happens if you step thru the code; where does it hold up?

    That is potentially supported by your comment that Excel runs til you quit your App. If you have posted your entire App there is no where else to .Quit Excel!

  3. #3

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Hey Bruce,

    How's it going? Good to hear from you again.

    That is the entire application. I did step though it on my machine at home and my machine at work. The code runs completely with no errors or breaks. The .quit, .close and set ExcelApp = nothing all process fine.

    I am stumped here!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Keeping busy and yourself?

    What happens if you test with a staic TextFile, and comment out the Inet and Word code to test the Excel side of the house?


    Bruce.

  5. #5

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I ran it like this and had the same results . . . ???

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WordApp As Word.Application
    4. Dim ExcelApp As Excel.Application
    5.  
    6. Private Sub Form_Load()
    7.     lstStatus.Clear
    8.     Inet1.Protocol = icHTTP
    9. End Sub
    10. Private Sub cmdGet_Click()
    11.     ProcessFile
    12. End Sub
    13. Private Sub ProcessFile()
    14.     cmdGet.Enabled = False
    15.     DoEvents
    16. '    Dim strURL As String
    17. '    Dim bData() As Byte      ' Data variable
    18. '    Dim intFile As Integer   ' FreeFile variable
    19. '    strURL = "http://www.ssa.gov/foia/highgroupdownloads/HG0903.doc"
    20. '    intFile = FreeFile()
    21. '
    22. '    ' The result of the OpenURL method goes into the Byte
    23. '    ' array, and the Byte array is then saved to disk.
    24. '    lstStatus.AddItem "Opening Social Security Website . . ."
    25. '    bData() = Inet1.OpenURL(strURL, icByteArray)
    26. '    lstStatus.AddItem "Downloading SSN document . . ."
    27. '    Open App.Path & "\ssn.doc" For Binary Access Write As #intFile
    28. '    Put #intFile, , bData()
    29. '    Close #intFile
    30. '
    31. '    lstStatus.AddItem "Converting SSN document to text file . . ."
    32. '    Set WordApp = New Word.Application
    33. '    With WordApp
    34. '        .WindowState = wdWindowStateMinimize
    35. '        .Visible = False
    36. '        .DisplayAlerts = wdAlertsNone       'hide word alerts msgboxes
    37. '        .Documents.Open App.Path & "\SSN.doc"
    38. '        .ActiveDocument.SaveAs FileName:=App.Path & "\SSN.txt", _
    39. '            FileFormat:=wdFormatText, _
    40. '            LockComments:=False, _
    41. '            Password:="", _
    42. '            AddToRecentFiles:=False, _
    43. '            WritePassword:="", _
    44. '            ReadOnlyRecommended:=False, _
    45. '            EmbedTrueTypeFonts:=False, _
    46. '            SaveNativePictureFormat:=False, _
    47. '            SaveFormsData:=False, _
    48. '            SaveAsAOCELetter:=False
    49. '        .Documents.Close False
    50. '    End With
    51. '    WordApp.Application.Quit False
    52. '    Set WordApp = Nothing
    53. '
    54. '    lstStatus.AddItem "Reading SSN text file into array . . ."
    55. '    Dim strData As String
    56. '    Dim intData(1000, 1) As Integer
    57. '    Dim i As Integer
    58. '
    59. '    intFile = FreeFile()
    60. '    i = 0
    61. '
    62. '    Open App.Path & "\SSN.txt" For Input As #intFile
    63. '    Do While Not EOF(intFile)
    64. '        'get group
    65. '        Input #intFile, strData
    66. '        intData(i, 0) = Val(strData)
    67. '        'exit loop if we run out of data but the file still
    68. '        '  has "stuff" in it
    69. '        If intData(i, 0) = 0 Then Exit Do
    70. '        'get area
    71. '        Input #intFile, strData
    72. '        intData(i, 1) = Val(strData)
    73. '        'increment counter
    74. '        i = i + 1
    75. '    Loop
    76. '    Close #intFile
    77. '
    78. '    lstStatus.AddItem "Saving SSN text file as CSV file . . ."
    79. '    intFile = FreeFile()
    80. '
    81. '    Open App.Path & "\SSN.txt" For Output As #intFile
    82. '    Dim j As Integer
    83. '    For j = 0 To i - 1
    84. '        Write #intFile, intData(j, 0), intData(j, 1)
    85. '    Next j
    86. '    Close #intFile
    87.    
    88.     lstStatus.AddItem "Reading SSN CSV file into Excel . . ."
    89.     Set ExcelApp = New Excel.Application
    90.     With ExcelApp
    91.         .WindowState = xlMinimized
    92.         .Visible = False
    93.         'hide excel alerts msgboxes
    94.         .DisplayAlerts = False
    95.         .Workbooks.OpenText FileName:=App.Path & "\SSN.txt", _
    96.             Origin:=xlWindows, _
    97.             StartRow:=1, _
    98.             DataType:=xlDelimited, _
    99.             TextQualifier:=xlDoubleQuote, _
    100.             ConsecutiveDelimiter:=False, _
    101.             Tab:=False, _
    102.             Semicolon:=False, _
    103.             Comma:=True, _
    104.             Space:=False, _
    105.             Other:=False, _
    106.             FieldInfo:=Array(Array(1, 1), Array(2, 1))
    107.     lstStatus.AddItem "Creating new HG.dbf file . . ."
    108.         .ActiveSheet.Range("A1:B1050").Select
    109.         .Selection.Sort Key1:=Range("A1"), _
    110.             Order1:=xlAscending, _
    111.             Header:=xlGuess, _
    112.             OrderCustom:=1, _
    113.             MatchCase:=False, _
    114.             Orientation:=xlTopToBottom
    115.         .ActiveSheet.Range("A1").Select
    116.         .Selection.EntireRow.Insert
    117.         .ActiveCell.FormulaR1C1 = "Area"
    118.         .ActiveSheet.Range("B1").Select
    119.         .ActiveCell.FormulaR1C1 = "Group"
    120.         .ActiveSheet.Range("A1").Select
    121.         .ActiveWorkbook.SaveAs FileName:=App.Path & "\HG.dbf", _
    122.             FileFormat:=xlDBF4, _
    123.             CreateBackup:=False
    124.         .ActiveWorkbook.Saved = True
    125.         .ActiveWorkbook.Close
    126.         .Workbooks.Close
    127.     End With
    128.     ExcelApp.Application.Quit
    129.     Set ExcelApp = Nothing
    130.     lstStatus.AddItem "Process complete"
    131. End Sub

    Bruce, my wife and I were watching a special about Australia on Discovery. We decided we either want to visit or MOVE there! We kept wishing we had friends to stay with there. Don't be suprised if you see a family of 4 knocking on your door!! ha ha!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    I re-tested with your latest post except for the Inet (I can't load Components on this machine) and replaced the File I/O with C:\.
    My simple TextFile has Alpha characters on each line.

    I'm running Win 2000, and made Reference to the MS Excel 9.0 Object Library.

    Here is what I used:
    VB Code:
    1. Option Explicit
    2. Dim ExcelApp As Excel.Application
    3.  
    4. Private Sub Command1_Click()
    5.     Main
    6. End Sub
    7.  
    8. Private Sub Main()
    9.  
    10.     lstStatus.AddItem "Reading SSN CSV file into Excel . . ."
    11.     Set ExcelApp = New Excel.Application
    12.     With ExcelApp
    13.         .WindowState = xlMinimized
    14.         .Visible = False
    15.         'hide excel alerts msgboxes
    16.         .DisplayAlerts = False
    17.         .Workbooks.OpenText FileName:="[b]C:\[/b]SSN.txt", _
    18.             Origin:=xlWindows, _
    19.             StartRow:=1, _
    20.             DataType:=xlDelimited, _
    21.             TextQualifier:=xlDoubleQuote, _
    22.             ConsecutiveDelimiter:=False, _
    23.             Tab:=False, _
    24.             Semicolon:=False, _
    25.             Comma:=True, _
    26.             Space:=False, _
    27.             Other:=False, _
    28.             FieldInfo:=Array(Array(1, 1), Array(2, 1))
    29.     lstStatus.AddItem "Creating new HG.dbf file . . ."
    30.         .ActiveSheet.Range("A1:B1050").Select
    31.         .Selection.Sort Key1:=Range("A1"), _
    32.             Order1:=xlAscending, _
    33.             Header:=xlGuess, _
    34.             OrderCustom:=1, _
    35.             MatchCase:=False, _
    36.             Orientation:=xlTopToBottom
    37.         .ActiveSheet.Range("A1").Select
    38.         .Selection.EntireRow.Insert
    39.         .ActiveCell.FormulaR1C1 = "Area"
    40.         .ActiveSheet.Range("B1").Select
    41.         .ActiveCell.FormulaR1C1 = "Group"
    42.         .ActiveSheet.Range("A1").Select
    43.         .ActiveWorkbook.SaveAs FileName:="[b]C:\[/b]HG.dbf", _
    44.             FileFormat:=xlDBF4, _
    45.             CreateBackup:=False
    46.         .ActiveWorkbook.Saved = True
    47.         .ActiveWorkbook.Close
    48.         .Workbooks.Close
    49.     End With
    50.     ExcelApp.Application.Quit
    51.     Set ExcelApp = Nothing
    52.     lstStatus.AddItem "Process complete"
    53. End Sub


    We have a spare room and plenty of space, just give us a few days notice
    My wife is from NY and has been here for 8 years, she is now an honorary Aussie.



    Bruce.
    Last edited by Bruce Fox; Oct 9th, 2003 at 04:51 PM.

  7. #7
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Why Won't Excel End?????

    hmm,

    it ran OK for me 3 times then the Excel process would not close.

    If i might be so bold as to say that you are going at this from the wrong direction. what you should do is create a process or use the GetObject API as in the following code for WORD you can use the same technique for EXCEL. then when you close or quit or whatever the APP it really is Closed.........

    If it hangs around in ram still then that is no biggy because that app version is used the next time the code runs, this saves it having to initialize itself again and thus saves time and processing power.

    I know the excel code is a bit elaborate but you can see how to simplify it by looking at the word version....

    Aside from that if you use the following code, you can use an
    existing version of the APP...... if it did not or does not unload or is being used by someone on the Machine for another purpose.

    'for word................... See below this for excel....


    VB Code:
    1. Public Sub OpenFileInWord(StrDocName)
    2.  
    3. ' Note:
    4. ' You must have a reference to the Word Object Library.
    5.  
    6.  
    7. Dim wd As Word.Application
    8.  
    9. On Error GoTo cmdOpenDocErr
    10. 'See if word is already open
    11.     Set wd = GetObject(, "Word.Application")
    12. 'if not create a new instance
    13. RestartOpen:
    14. If wd Is Nothing Then
    15.     Set wd = CreateObject("Word.Application")
    16. End If
    17. wd.Documents.Open (StrDocName)
    18.     wd.Visible = True
    19.  
    20.  
    21.    
    22. Exit Sub
    23.  
    24. cmdOpenDocErr:
    25.   Select Case Err.Number
    26.     Case 381
    27.       MsgBox "Please select a document.", vbCritical, "Document Selection Error"
    28.    
    29.     Case 429
    30.     Resume RestartOpen
    31.   End Select
    32.  
    33. End Sub


    For Excel Termination etc....
    VB Code:
    1. Private Sub cmdExport_Click()
    2.  
    3. ' Note:
    4. ' You must have a reference to the Excel Object Library.
    5. '
    6. Dim i           As Long
    7. Dim j           As Long
    8. Dim lRowCount   As Long
    9. Dim lPasteCount As Long
    10. Dim sLtr        As String
    11. Dim sStart      As String
    12. Dim sEnd        As String
    13. Dim sRowData    As String
    14. Dim sSelData    As String
    15. Dim oExcelApp   As excel.Application
    16. Dim oWs         As excel.Worksheet
    17. Dim oWb         As excel.Workbook
    18.  
    19. Const cNUMCOLS = 6
    20. Const cNUMROWS = 700
    21. Const cFIXEDROWS = 6
    22. Const cCLIPROWS = 500
    23.  
    24. On Error GoTo ErrorHandler
    25. Screen.MousePointer = vbHourglass
    26. If Dir(sNewXlsFile) <> "" Then Kill sNewXlsFile
    27. '
    28. ' Create an invisible Excel instance.
    29. '
    30. ' Open a previously created worksheet that has most
    31. ' of the desired formatting already. Save this template
    32. ' as a new file so as not to destroy it.
    33. '
    34. Set oExcelApp = CreateObject("EXCEL.APPLICATION")
    35. oExcelApp.Visible = False
    36. oExcelApp.Workbooks.Open FileName:=sXlsTemplate, ReadOnly:=True, ignoreReadOnlyRecommended:=True
    37. Set oWs = oExcelApp.ActiveSheet
    38. Set oWb = oExcelApp.ActiveWorkbook
    39. oWs.SaveAs FileName:=sNewXlsFile, FileFormat:=xlNormal
    40. '
    41. ' Populate the header information by writting
    42. ' directly to specific cells.
    43. '
    44. ' Note:
    45. ' Strings are prefixed with a quote mark.
    46. '
    47. With oWs
    48.     .Cells(1, 4).Value = "'Value1"
    49.     .Cells(2, 4).Value = "'Value2"
    50.     .Cells(3, 4).Value = "'Value3"
    51.     .Cells(4, 4).Value = "'Value4 Value4 Value4 Value4 Value4 Value4"
    52.     .Cells(5, 5).Value = "'Value5"
    53.     .Cells(5, 6).Value = "'Value6"
    54.     .Cells(5, 7).Value = "'Value7"
    55. End With
    56. '
    57. ' Now lets populate the "body" of the spreadsheet.
    58. ' Determine the range of cells to be populated
    59. ' and change their format to numeric.
    60. '
    61. sStart = "A" & CStr(cFIXEDROWS + 1)
    62. sLtr = Mid$("ABCDEFGHIJKLMNOPQRSTUVWXYZ", cNUMCOLS + 1, 1)
    63. sEnd = sLtr & CStr(cFIXEDROWS + cNUMROWS + 1)
    64.  
    65. oWs.Range(sStart, sEnd).Select
    66. oWs.Range(sStart, sEnd).Activate
    67. oWs.Range(sStart, sEnd).NumberFormat = "#,##0.00"
    68. '
    69. ' Populate the body of the spreadsheet.
    70. '
    71. sSelData = ""
    72. lRowCount = 0
    73. lPasteCount = 0
    74.  
    75. For i = 0 To cNUMROWS
    76.     sRowData = ""
    77.     '
    78.     ' Create the rows to send to Excel. Each row
    79.     ' is a tab delimited string of values terminated
    80.     ' by a carriage return and line feed. Data can
    81.     ' come from a grid or other source.
    82.     '
    83.     For j = 0 To cNUMCOLS
    84.         sRowData = sRowData & CStr(j) & vbTab
    85.     Next
    86.     sRowData = Left$(sRowData, Len(sRowData) - 1)
    87.     '
    88.     ' Rows are accumulated into blocks then stored in
    89.     ' the clipboard and pasted into Excel in one shot.
    90.     '
    91.     ' They can be written one at a time but this is
    92.     ' faster since the data is kept in memory and
    93.     ' there are fewer calls to Excel.
    94.     '
    95.     sSelData = sSelData + sRowData + vbCrLf
    96.     lRowCount = lRowCount + 1
    97.    
    98.     If lRowCount = cCLIPROWS Then
    99.         Clipboard.Clear
    100.         Clipboard.SetText sSelData
    101.         sSelData = ""
    102.         With oWs
    103.             .Range("A" & CStr(lPasteCount * cCLIPROWS + cFIXEDROWS)).Select
    104.             .Paste
    105.             .Range("A1").Select
    106.         End With
    107.         lRowCount = 0
    108.         lPasteCount = lPasteCount + 1
    109.     End If
    110. Next
    111. '
    112. ' Paste the last block of data into the worksheet.
    113. '
    114. Clipboard.Clear
    115. Clipboard.SetText sSelData
    116. With oWs
    117.     .Range("A" & CStr(lPasteCount * cCLIPROWS + cFIXEDROWS)).Select
    118.     .Paste
    119.     .Range("A1").Select
    120. End With
    121. '
    122. ' Change the formatting on a few cells.
    123. '
    124. ' Select and highlight a cell. Change the font
    125. ' style and color on certain parts of its contents.
    126. '
    127. oWs.Range("D4").Select
    128. oWs.Range("D4").Activate
    129. With oExcelApp.ActiveCell.Characters(Start:=1, Length:=10).Font
    130.     .FontStyle = "Regular"
    131.     .Size = 11
    132.     .ColorIndex = 5
    133. End With
    134. With oExcelApp.ActiveCell.Characters(Start:=20, Length:=30).Font
    135.     .FontStyle = "Italic"
    136.     .Size = 11
    137.     .ColorIndex = xlAutomatic
    138. End With
    139. '
    140. ' Just for fun, change the color of
    141. ' the first column to Red.
    142. '
    143. sStart = "A" & CStr(cFIXEDROWS + 1)
    144. sEnd = "A" & CStr(cFIXEDROWS + cNUMROWS + 1)
    145. oWs.Range(sStart, sEnd).Select
    146. oWs.Range(sStart, sEnd).Activate
    147. oWs.Range(sStart, sEnd).Font.ColorIndex = 3
    148. '
    149. ' Change the border and color of the last row.
    150. '
    151. j = (lPasteCount * cCLIPROWS) + cFIXEDROWS + lRowCount
    152. For i = 1 To cNUMCOLS + 1
    153.     With oWs.Cells(j, i)
    154.         .Borders(xlTop).LineStyle = xlDouble
    155.         .Font.Bold = True
    156.         .Font.ColorIndex = 3
    157.     End With
    158. Next
    159. '
    160. ' Make the last row a total line. Build and insert
    161. ' a formula into its first cell. Then copy the
    162. ' formula to the remaining cells.  When it is copied
    163. ' Excel will update the cell references for you.
    164. '
    165. oWs.Cells(j, 1).Value = "=SUM(A" & CStr(cFIXEDROWS + 1) & ":A" & CStr(j - 1) & ")"
    166. For i = 1 To cNUMCOLS
    167.     oWs.Cells(j, 1).Copy
    168.     oWs.Cells(j, i + 1).Select
    169.     oWs.Paste
    170. Next
    171. '
    172. ' Save the changed worksheet.
    173. '
    174. oWb.Save
    175. oWb.Saved = True
    176. '
    177. ' Terminate and release the Excel objects.
    178. '
    179. oExcelApp.Quit
    180. Set oWs = Nothing
    181. Set oWb = Nothing
    182. Set oExcelApp = Nothing
    183. Screen.MousePointer = vbDefault
    184. MsgBox "Data export complete", vbInformation, "Excel Export Example"
    185. Exit Sub
    186.  
    187. ErrorHandler:
    188.     Screen.MousePointer = vbDefault
    189.     MsgBox Err.Description & " (" & CStr(Err.Number) & ")", vbExclamation, "Excel Export Example"
    190.     On Error Resume Next
    191.     oExcelApp.Quit
    192.     Set oWs = Nothing
    193.     Set oWb = Nothing
    194.     Set oExcelApp = Nothing
    195. End Sub

    hope this helps...



    bindu

  8. #8
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Hey i found the answer .!!!!

    Hi There check out the program in the zip file i think it will do nicely.


    Bindu

    Attached Files Attached Files

  9. #9

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Bruce & Binduau,

    I think it has to do with one of the sheet objects not closing properly. I created a test app and I can close excel without any problems . . .

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow _
    4.         Lib "user32" _
    5.         Alias "FindWindowA" _
    6.         (ByVal lpClassName As String, _
    7.         ByVal lpWindowName As String) As Long
    8.        
    9. Dim ExcelApp As Excel.Application
    10. Const gcClassnameMSExcel = "XLMAIN"
    11.  
    12. Public Sub CheckExcelOpen()
    13.     Dim lngHandle As Long
    14.     lngHandle = FindWindow(gcClassnameMSExcel, vbNullString)
    15.     If lngHandle <> 0 Then
    16.         MsgBox "Excel IS running"
    17.     Else
    18.         MsgBox "Excel is NOT running"
    19.     End If
    20. End Sub
    21. Private Sub Form_Load()
    22.     Command1.Caption = "Check Excel"
    23.     Command2.Caption = "Open Excel"
    24.     Command3.Caption = "Close Excel"
    25. End Sub
    26. Private Sub Command1_Click()
    27.     CheckExcelOpen
    28. End Sub
    29. Private Sub Command2_Click()
    30.     'open excel
    31.     Set ExcelApp = New Excel.Application
    32.     With ExcelApp
    33.         .WindowState = xlMinimized
    34.         .Visible = False
    35.         .DisplayAlerts = False  'hide excel alerts msgboxes
    36.     End With
    37. End Sub
    38. Private Sub Command3_Click()
    39.     'close excel
    40.     ExcelApp.Application.Quit
    41.     Set ExcelApp = Nothing
    42. End Sub
    43. Private Sub Command4_Click()
    44.     Unload Me
    45. End Sub


    Binduau - I tried the example you posted and it works like a charm!

    Bruce - One of these days . . . I'm gonna take you up on that!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  10. #10
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Cool Excel won't DIE!!!!!

    Glad to be of service man..

    bindu


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