Microsoft Developer Network >
Forums Home
>
Microsoft ISV Community Center Forums
>
Visual Basic for Applications (VBA)
>
Open/Close an Excel from Outlook Macro.
Open/Close an Excel from Outlook Macro.
- I have to open and close an excel workbook from a Outlook Macro.The below code is not closing the excel application, instead throwing errors as well as leaving all the excel applications open when each time this function is called. Can some one help me clean the code.Regards,Diwakar=============================Function GetConfigItem(FileName As String, ConfigItem As String) As StringDim xlApp As Excel.ApplicationDim xlWorkbooks As Excel.WorkbooksDim xlSheet As Excel.WorksheetDim ExcelRunning As BooleanDim iCount As IntegerOn Error GoTo ErrGetConfigItem' If ExcelRunning = IsExcelRunning Then' Set xlApp = GetObject(, Excel.Application)' Else' Set xlApp = CreateObject("Excel.Application")' End If' If Not xlApp = GetObject(, "Excel.Application") Is Nothing ThenSet xlApp = CreateObject("Excel.Application")' End IfxlApp.Workbooks.Open (FileName)Set xlSheet = ActiveSheetiCount = 1Do While Cells(iCount, 1).Value <> ""If Cells(iCount, 1).Value = ConfigItem ThenGetConfigItem = Cells(iCount, 2).ValueEnd IfiCount = iCount + 1Loop'====================================================================================='' The config file is still open. The Excel.exe will run even after the app is done.' Code to be added to close !!!' CRACK: go to task manager and close Excel.exe at the end.''=====================================================================================If IsExcelRunning() ThenxlApp.Workbooks.CloseSet xlApp = NothingEnd IfErrGetConfigItem:MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.NumberExit FunctionEnd Function=============================
- Moved byYichun_FengMSFTTuesday, November 03, 2009 9:29 AMVBA issue (From:Visual Basic Language)
Answers
- Below code modifications worked:Regards,Diwakar=========================================Function GetConfigItem(FileName As String, ConfigItem As String) As StringDim xlWorkbook As Excel.WorkbookDim xlSheet As Excel.WorksheetDim iCount As IntegerOn Error GoTo ErrGetConfigItemSet xlWorkbook = GetObject(FileName)If Not xlWorkbook Is Nothing ThenSet xlWorkbook = CreateObject(FileName)End IfSet xlSheet = xlWorkbook.ActiveSheetiCount = 1Do While xlSheet.Cells(iCount, 1).Value <> ""If xlSheet.Cells(iCount, 1).Value = ConfigItem ThenGetConfigItem = xlSheet.Cells(iCount, 2).ValueEnd IfiCount = iCount + 1LoopxlWorkbook.Close SaveChanges:=FalseSet xlWorkbook = NothingSet xlSheet = NothingExit FunctionErrGetConfigItem:MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.NumberErr.ClearxlWorkbook.Close SaveChanges:=FalseSet xlWorkbook = NothingSet xlSheet = NothingExit FunctionEnd Function=========================================
- Marked As Answer byTim LiMSFT, ModeratorMonday, November 09, 2009 3:04 AM
All Replies
- Hi Diwakar Killamsetty,
Since marco in Office is related to Visual Basic for Applications, I'll move this thread to VBA forum for better support.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
- Below code modifications worked:Regards,Diwakar=========================================Function GetConfigItem(FileName As String, ConfigItem As String) As StringDim xlWorkbook As Excel.WorkbookDim xlSheet As Excel.WorksheetDim iCount As IntegerOn Error GoTo ErrGetConfigItemSet xlWorkbook = GetObject(FileName)If Not xlWorkbook Is Nothing ThenSet xlWorkbook = CreateObject(FileName)End IfSet xlSheet = xlWorkbook.ActiveSheetiCount = 1Do While xlSheet.Cells(iCount, 1).Value <> ""If xlSheet.Cells(iCount, 1).Value = ConfigItem ThenGetConfigItem = xlSheet.Cells(iCount, 2).ValueEnd IfiCount = iCount + 1LoopxlWorkbook.Close SaveChanges:=FalseSet xlWorkbook = NothingSet xlSheet = NothingExit FunctionErrGetConfigItem:MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.NumberErr.ClearxlWorkbook.Close SaveChanges:=FalseSet xlWorkbook = NothingSet xlSheet = NothingExit FunctionEnd Function=========================================
- Marked As Answer byTim LiMSFT, ModeratorMonday, November 09, 2009 3:04 AM

