Home > Software engineering >  MFC operation excel add hyperlinks to excel
MFC operation excel add hyperlinks to excel

Time:09-24

Through the MFC to write Excel, and to set a specific cell hyperlink
Put a hyperlink//
Cstrings cc.//hyperlinks ¨ address
Cc=_T (" http://www.baidu.com/index.php? Tn=maxthon2 & amp; Ch=3 ");//
Range. AttachDispatch (sheet. GetRange (_variant_t (_T (" C2 ")), _variant_t (_T (" C2 "))));//get area
Links. AttachDispatch (range. GetHyperlinks ());//get the Hyperlinks object

This sentence error interrupt directly find useful error message
The links. The Add (range, cc, _variant_t (_T (" ")), _variant_t (_T (" ")), _variant_t (_T (" ")));//set the hyperlink

Please the great god

CodePudding user response:

@ weixin_38323761

CodePudding user response:

EXCEL macro recording, manual operation, according to the macro code editor

 
//Excel12. CPP: Defines the entry point for the console application.
//

# include "stdafx. H"
#include
#include

# import "C:/Program Files/Microsoft Office/OFFICE12/the oledata.mso. DLL" \
Rename (" RGB ", "RBGMSO") rename (" SearchPath ", "SearchPathMSO") \
Rename (" DocumentProperties DocumentPropertiesMSO ", "") no_auto_exclude
# import "C:/Program Files/Microsoft Office/OFFICE12/VBE6EXT OLB" no_namespace
# import "C:/Program Files/Microsoft Office/OFFICE12/excel. Exe" \
Rename (" DialogBox ExcelDialogBox ", "") rename (" RGB", "ExcelRGB") \
Rename (" used by CopyFile ExcelCopyFile ", "") rename (" ReplaceText", "ExcelReplaceText") \
Rename (" IFont IFontXL ", "")

Void dump_com_error (_com_error & amp; E)
{
_tprintf (_T (" Oops - hit an error! \ n "));
_tprintf (_T (" \ \ a tCode=% 08 lx \ n "), e.E rror ());
_tprintf (_T (" \ \ a tCode fancy=% s \ n "), e.E rrorMessage ());
_bstr_t bstrSource (e.S ource ());
_bstr_t bstrDescription (e.D escription ());
_tprintf (_T (" \ \ a tSource=% s \ n "), (LPCTSTR) bstrSource);
_tprintf (_T (" \ \ a tDescription=% s \ n "), (LPCTSTR) bstrDescription);
}
Struct StartOle {
StartOle () {CoInitialize (NULL); }
~ StartOle () {CoUninitialize (); }
} _inst_StartOle;


Int main (int arg c, char * argv [])
{
Using the namespace Office;
Using the namespace Excel;

_ApplicationPtr pXL;

Try
{
PXL. CreateInstance (L "Excel. Application");

The pXL - & gt; PutVisible (0, VARIANT_TRUE);

WorkbooksPtr pBooks=pXL - & gt; Workbooks.
_WorkbookPtr pBook=pBooks - & gt; Add ((long) xlWorksheet);

_WorksheetPtr pSheet=pXL - & gt; ActiveSheet.

PSheet - & gt; Name="Market Share!" ;

//insert hyperlink
Try
{
//Range (" i2). Select
//ActiveSheet. Hyperlinks. Add Anchor:=Selection, Address:=_
//"http://bbs.csdn.net/topics/392326155", TextToDisplay:="test hyperlink
"
RangePtr pRang=pSheet - & gt; Range [" i2];
HyperlinksPtr pLinks=pSheet - & gt; Hyperlinks.
PLinks - & gt; Add (pRang,
"Http://bbs.csdn.net/topics/392326155",
VtMissing,
VtMissing,
"Test links");
}
The catch (_com_error & amp; E)
{
Dump_com_error (e);
}

PSheet - & gt; Range [" A2 "] - & gt; Value2="Company A";
PSheet - & gt; Range [" A3 "] - & gt; Value2=75.0;
PSheet - & gt; Range [" A4 "] - & gt; Value2=75.0;

Sleep (6000);

PBook - & gt; PutSaved (0, VARIANT_TRUE);
The pXL - & gt; The Quit ();
}
The catch (_com_error & amp; E)
{
Dump_com_error (e);
}

return 0;
}


  • Related