To create a Microsoft Excel COM-object, it is required to specify in the CreateObject method the Excel.Application program identifier:
Var
Excel: Variant;
Begin
Excel := Variant.CreateObject("Excel.Application");
The COM-object instance will be created in the computer memory. For valuable work with Excel application, it is required to create a new book in it or open the existing one, then for the Application.Visible property (see Visual Basic help system) select True:
Var
Excel: Variant;
Begin
Excel := Variant.CreateObject("Excel.Application");
//Create a new book
Excel.GetProperty("Workbooks").Invoke("Add", New Variant[0]);
//or open existing book
Excel.GetProperty("Workbooks").Invoke("Open", "c:\Book1.xlsx");
//Display application
Excel.SetProperty("Visible", True);
After that, the application window will be displayed on the screen.
The Microsoft developers changed the approach to work with windows in the Windows 7 operating system, that is why the specified above code will create the Excel application, but the application will not get the focus. The application window will be in the background, the shortcut will be available only on the taskbar. If it is required to put the window to the foreground and get in focus, there is the following work around: before displaying the window, minimize the window size and then restore it. The following code can be used in Fore for it:
Enum XlWindowState
xlMaximized = -4137, xlMinimized = -4140, xlNormal = -4143
End Enum XlWindowState;
Var
Excel: Variant;
Begin
Excel := Variant.CreateObject("Excel.Application");
Excel.GetProperty("Workbooks").Invoke("Add", New Variant[0]);
Excel.SetProperty("WindowState", XlWindowState.xlMinimized);
Excel.SetProperty("WindowState", XlWindowState.xlMaximized);
Excel.SetProperty("Visible", True);
See also: