Home > Language Reference > Excel Functions > CloseExcelCom

CloseExcelCom

The CloseExcelCom function closes the background Excel instance loaded by WriteExcel or ReadExcel.

Usage

If you need to automate directly the Excel interface after having used WriteExcel/ReadExcel functions, use CloseExcelCom function before using the Shell function to launch Excel.

Syntax

ret = CloseExcelCom()

Parameters

None.

Return value

ret, optional numeric return value. If the Excel instance is already opened, the return value is -1. If Excel instance is not opened yet, the return value is 0.

See also

CreateExcelFile
ExecExcelMacro
ReadExcel
WriteExcel


Example

'This script example captures the market indices on yahoo finances, writes them into Excel, opens the resulting Excel file and prints it.

''The 4 arrays for capturing the columns
Dim tabcell_3$(100)
Dim tabcell_2$(100)
Dim tabcell_1$(100)
Dim tabcell_0$(100)

'Create the c:\wttest\finance.xlsx Excel file
CreateExcelFile("c:\wttest\finance.xlsx")

'Start Yahoo finances web site
StartBrowser("IE", "http://finance.yahoo.com",3)

'Capture the finances data and write them in c:\wttest\finance.xlsx
 UsePage("Yahoo! Finance - Business Finance, Stock Market, Quotes, News")
 ret = CaptureTableHTML("TABLE[CONTENT='Name']", "R1C1:R12C1", tabcell_0$())
 ret = WriteExcel("C:\wttest\finance.xlsx", "Sheet1!A1:A12", tabcell_0$())
 ret = CaptureTableHTML("TABLE[CONTENT='Name']", "R1C2:R12C2", tabcell_1$())
 ret = WriteExcel("C:\wttest\finance.xlsx", "Sheet1!B1:B12", tabcell_1$())
 ret = CaptureTableHTML("TABLE[CONTENT='Name']", "R1C3:R12C3", tabcell_2$())
 ret = WriteExcel("C:\wttest\finance.xlsx", "Sheet1!C1:C12", tabcell_2$())
 ret = CaptureTableHTML("TABLE[CONTENT='Name']", "R1C4:R12C4", tabcell_3$())
 ret = WriteExcel("C:\wttest\finance.xlsx", "Sheet1!D1:D12", tabcell_3$())

'As Excel is launched after a WriteExcel, CloseExcelCom line is mandatory.
 CloseExcelCom()

Shell(Chr$(34)+"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"+Chr$(34)+" c:\wttest\finance.xlsx",1)
'Save the Excel file - the lines below need the UI Automation functions available only in the 64 bit automation software.
'Click the Office button
ClickUIA("[APP='EXCEL.EXE',CLASS='NetUIHWND',IDX=2][NAME='Office Button',ROLE='button']")
'Click Save menu option
ClickUIA("[APP='EXCEL.EXE',CLASS='NetUIHWND'][NAME='Save',ROLE='menu item']")