]> git.saurik.com Git - wxWidgets.git/commitdiff
ensure that Excel is visible and has an opened workbook (closes #10837)
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 25 May 2009 20:42:39 +0000 (20:42 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 25 May 2009 20:42:39 +0000 (20:42 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@60744 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/oleauto/oleauto.cpp

index b360751bdbd2fd672c866373550d609ba6e7908f..f803eaf7c085cda8ebc02f9469acfb6388e8e65d 100644 (file)
@@ -201,12 +201,36 @@ void MyFrame::OnTest(wxCommandEvent& WXUNUSED(event))
     wxAutomationObject excelObject, rangeObject;
     if (!excelObject.GetInstance(_T("Excel.Application")))
     {
+        // Start Excel if it is not running
         if (!excelObject.CreateInstance(_T("Excel.Application")))
         {
             wxMessageBox(_T("Could not create Excel object."));
             return;
         }
     }
+
+    // Ensure that Excel is visible
+    if (!excelObject.PutProperty(_T("Visible"), true))
+    {
+        wxMessageBox(_T("Could not make Excel object visible"));
+    }
+    const wxVariant workbooksCountVariant = excelObject.GetProperty(_T("Workbooks.Count"));
+    if (workbooksCountVariant.IsNull())
+    {
+        wxMessageBox(_T("Could not get workbooks count"));
+        return;
+    }
+    const long workbooksCount = workbooksCountVariant;
+    if (workbooksCount == 0)
+    {
+        const wxVariant workbook = excelObject.CallMethod(_T("Workbooks.Add"));
+        if (workbook.IsNull())
+        {
+            wxMessageBox(_T("Could not create new Workbook"));
+            return;
+        }
+    }
+
     if (!excelObject.PutProperty(_T("ActiveCell.Value"), _T("wxWidgets automation test!")))
     {
         wxMessageBox(_T("Could not set active cell value."));