簡體   English   中英

我如何為我的應用程序創建一個自己的實例excel並使用它?

[英]How I can create for my application one's own instance excel and use it?

我如何為我的應用程序創建一個自己的實例excel並使用它? 現在我在調用CreateInstance時出現com錯誤,並且正在進行excel實例。 我想為我的應用程序使用excel實例的全局處理程序,並在我的應用程序關閉時將其終止

Excel允許多個實例。 以下代碼適用於多個實例( XLXL1 )。 即使您以前手動啟動Excel也可以使用它。 您是否會展示代碼示例以澄清您的問題。

#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll"
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files (x86)\Microsoft Office\Office12\excel.exe" \
  rename("DialogBox","ExcelDialogBox") rename("RGB","ExcelRGB") \
  exclude("IFont","IPicture")

#include <stdexcept>
#include <iostream>

int main()
{
  CoInitialize(NULL);
  try {
    Excel::_ApplicationPtr XL, XL1;

    HRESULT hr = XL.CreateInstance(L"Excel.Application");
    if(FAILED(hr)) {
      char msg[1024] = {0};
      sprintf(msg, "E: initializing first instance failed: %d", hr);
      throw std::runtime_error(msg);
    }

    Excel::_WorkbookPtr workbook = XL->Workbooks->Add(Excel::xlWorksheet); 
    Excel::_WorksheetPtr worksheet = XL->ActiveSheet;
    worksheet->SaveAs("c:\\test.xls");

    hr = XL1.CreateInstance(L"Excel.Application");
    if(FAILED(hr)) {
      char msg[1024] = {0};
      sprintf(msg, "E: initializing second instance failed: %d", hr);
      throw std::runtime_error(msg);
    }

    workbook->Close();
    XL->Quit();
    XL1->Quit();
  }
  catch(_com_error &ce)
  {
    std::cout<<"caught" << std::endl;
    // Handle the error
  }

  CoUninitialize();

  system("pause");
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM