I am using visual studio 2008 and world 2003. In my MFC DLL project, i need to use word 2003 automation api to create a new word document. But i want to know how to add a hyperlink to a text?
int CDOCXRun::InstantiateInOLE(COLERange &OLERange, CDOCXContext& Context)
{
int lastEnd = OLERange.get_End();
// Firstly we insert contents by iterating through all child elements and check their name.
for (CXMLElement *ElementIterator = m_lpElement->FirstChildElement(); ElementIterator != NULL; ElementIterator = ElementIterator->NextSiblingElement())
{
DOCX_IGNORE_EXCEPTIONS_BEGIN
CString strName = ElementIterator->GetName(); // Get the name of the current child element
// if the name equales to <w:t>, this run is a small piece of text and its text is stored just in thr following format:
// <w:t> Text </w:t>
if (strName == DOCX_XML_TAG_TEXT)
{
CString lpText = ElementIterator->GetValue(); // Get text string from xml
if (lpText.IsEmpty())
lpText = _T(" ");
else
nTextCount ++;
CString strText = lpText; // OLE uses unicode for inserting data, conver the multibyte string into CString using UTF-8 codeset
OLERange.put_Text(strText); // Set text in rang
}
// If the name equals to <w:tab>, just insert a tab charactor
if (strName == DOCX_XML_TAG_TAB)
{
OLERange.put_Text(_T("\t"));
}
// If the name equals to <w:drawing>, it may be a picture.
// Although picture is only the special case of drawing, we consider only the circumstances of pictures.
if (strName == DOCX_XML_TAG_DRAWING)
{
CDOCXPicture Picture(ElementIterator); // Create a CDOCXPicture object on the child element.
// Check whether the picture is valid
if (Picture.IsValid())
{
Picture.InstantiateInOLE(OLERange,Context); // Call the InstantiateInOLE method to insert this picture into word
nPicCount ++;
}
}
DOCX_IGNORE_EXCEPTIONS_END
}
// Apply run property
DOCX_IGNORE_EXCEPTIONS_BEGIN
CXMLElement *rPr = m_lpElement->FirstDescendantElement(DOCX_XML_TAG_RUN_PROPERTY); // Get the xml node with <w:rPr> name, which stores formats of this run
if (rPr != NULL)
{
CDOCXRunProperty RunProperty(rPr); // Create an CDOCXRunProperty object on the <w:rPr> xml element.
RunProperty.InstantiateInOLE(OLERange, Context); // Call the InstantiateInOLE method to apply the formats specified in <w:rPr> elements into current run
}
DOCX_IGNORE_EXCEPTIONS_END
int nowEnd = OLERange.get_End();
return nowEnd;
}
we know that word is actually composed of xml files and a hyperlink is represented like this"
<w:hyperlink r:id="rId9" w:history="1">
<w:r w:rsidR="00BF5AA1" w:rsidRPr="00A16638">
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>检查文件</w:t>
</w:r>
<w:r w:rsidR="00FE551D" w:rsidRPr="00A16638">
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>是否包含文件头注释</w:t>
</w:r>
<w:r w:rsidR="00BF5AA1" w:rsidRPr="00A16638">
<w:rPr>
<w:rStyle w:val="Hyperlink"/>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>。</w:t>
</w:r>
</w:hyperlink>
my problem is that i can use OLERange.put_Text(strText); to generate text such as http://www.google.com, but i don't know how to make a hyperlink. when you push CTRL and click, it will open your web browser and jump into google.com from word document.
Aucun commentaire:
Enregistrer un commentaire