如何使用ole Automation(C ++)将超链接分配给Excel单元?

时间:2019-04-29 15:04:33

标签: c++ excel hyperlink ole

我正在尝试通过ole automation(C ++)将超链接添加到excel单元中。 我知道如何使用excel vba添加超链接。此vba正常工作

With Worksheets(1) 
 .Hyperlinks.Add Anchor:=.Range("a5"), 
 _ Address:="https://example.microsoft.com", 
 _ ScreenTip:="Microsoft Web Site", 
 _ TextToDisplay:="Microsoft" 
End With 

我试图获取超链接指针。但是超链接中的add方法返回类型不匹配错误

IDispatch *pHyber = NULL;
VARIANT resulthyper;
VariantInit ( & resulthyper);

hr = AutoWrap(DISPATCH_PROPERTYGET, &resulthyper, _pXlSheet, L"Hyperlinks",0);
pHyber = resulthyper.pdispVal;

CATUnicodeString cellAddress = GetCellEquivalent (3,5);

CATBSTR bstrCellAddress;
cellAddress.ConvertToBSTR(&bstrCellAddress);

IDispatch *pRange = NULL;

VARIANT resultrange;
VariantInit ( & resultrange);

VARIANT range;
VariantInit ( & range);
range.vt = VT_BSTR;
range.bstrVal = bstrCellAddress;

hr = AutoWrap(DISPATCH_PROPERTYGET, & resultrange, _pXlSheet, L"Range",1, range);
pRange = resultrange.pdispVal;

CATUnicodeString iAdd = "some text";
CATUnicodeString iScreenTip = "some text";
CATUnicodeString iTxtToDisplay = "some text";

CATBSTR bstrAddValue;
iAdd.ConvertToBSTR(&bstrAddValue);

CATBSTR bstrScreenTip;
iScreenTip.ConvertToBSTR(&bstrScreenTip);

CATBSTR bstrTxtDisplay;
iTxtToDisplay.ConvertToBSTR(&bstrTxtDisplay);

IDispatch *pXlChartObject;

VARIANT result4;
VariantInit(&result4);

VARIANT Address, ScreenTip, TextToDisplay;
Address.vt = VT_BSTR;
Address.bstrVal = bstrAddValue;
ScreenTip.vt = VT_BSTR;
ScreenTip.bstrVal = bstrScreenTip;
TextToDisplay.vt = VT_BSTR;
TextToDisplay.bstrVal = bstrTxtDisplay;

hr = AutoWrap(DISPATCH_METHOD, &result4, pHyber, L"Add", 4,TextToDisplay,ScreenTip,Address,range);

if(FAILED(hr))
{
    hr = AutoWrap(DISPATCH_PROPERTYPUT, &result4, pHyber, L"Add", 4, range, Address, ScreenTip, TextToDisplay);
}
pXlChartObject = result4.pdispVal;

我不知道哪里出了问题,应该为Autowrap函数以相反的顺序传递参数,对吗?

0 个答案:

没有答案