none
C++ for Outlook,how to add Hyperlink to mail Template RRS feed

  • Question

  • i want to add a mail template that contain a hyperlink to outlook,such as:

    the "Click here" is HyperLink,But my code couldn't do that~Only Add a HyperLink .

    my code:

    if(SUCCEEDED(hr)){
    VARIANT m
    ;
    m
    .vt = VT_ERROR;
    m
    .lVal = (long)DISP_E_PARAMNOTFOUND;
    CLSIDFromProgID(L"Outlook.Application",&olCLSID);
    hr
    = CoCreateInstance(olCLSID,NULL,
    CLSCTX_LOCAL_SERVER
    |CLSCTX_INPROC_SERVER,IID_IDispatch,
    (void **)&olApp);
    if(SUCCEEDED(hr)){
    IDispatch * olAppointmentItem;
    VARIANT x
    ,T;
    VariantInit(&x);
    T
    .vt = VT_INT;
    T
    .intVal = 1;
    hr
    =  AutoWrap(DISPATCH_METHOD, &x,olApp,L"CreateItem",1,T);
    if(SUCCEEDED(hr)){
    olAppointmentItem
    = x.pdispVal;
    IDispatch * olInspector;
    T
    .vt = VT_BOOL;
    T
    .boolVal = false;
    VariantInit(&x);
    hr
    =  AutoWrap(DISPATCH_METHOD, &x,olAppointmentItem,L"Display",
    1,T);
    VariantInit(&x);
    hr
    =  AutoWrap(DISPATCH_METHOD, &x,olAppointmentItem,
    L
    "GetInspector",0);
    if(SUCCEEDED(hr)){
    olInspector
    = x.pdispVal;
    IDispatch * wdDocument;
    hr
    = AutoWrap(DISPATCH_PROPERTYGET,&x,olInspector,
    L
    "WordEditor",0);
    if(SUCCEEDED(hr)){
    wdDocument
    = x.pdispVal;
    IDispatch * wdHyperLinks;
    IDispatch * wdRange;
    VariantInit(&x);
    hr
    =  AutoWrap(DISPATCH_PROPERTYGET, &x,wdDocument,
    L
    "HyperLinks",0);
    if(SUCCEEDED(hr)){
    wdHyperLinks
    = x.pdispVal;
    hr
    =  AutoWrap(DISPATCH_METHOD,&x,wdDocument
    ,L"Range",2,m,m);
    if(SUCCEEDED(hr)){
    wdRange
    = x.pdispVal;
    VariantInit(&x);
    VARIANT A
    ,D,Ar;
    A
    .vt = VT_BSTR;
    A
    .bstrVal =
    SysAllocString(L"http://www.google.com");
    D
    .vt = VT_BSTR;
    Ar.vt = VT_DISPATCH;
    Ar.pdispVal = wdRange;
    D
    .bstrVal = SysAllocString(L"google");
    hr
    = AutoWrap(DISPATCH_METHOD,&x,wdHyperLinks,
    L
    "Add",6,m,D,m,m,A,Ar);
    if(SUCCEEDED(hr)){
    printf
    ("Add HyperLink successfully!\n");
    }
    }
    }
    }
    }
    }
    }

    HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp,
    LPOLESTR ptName
    , int cArgs...) {
    va_list marker
    ;
    va_start
    (marker, cArgs);

    if(!pDisp) {
    _exit
    (0);
    }

    DISPPARAMS dp
    = { NULL, NULL, 0, 0 };
    DISPID dispidNamed
    = DISPID_PROPERTYPUT;
    DISPID dispID
    ;
    HRESULT hr
    ;
    char buf[200];
    char szName[200];

    WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);

    hr
    = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT,
    &dispID);
    if(FAILED(hr)) {
    sprintf
    (buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx",
    szName
    , hr);
    printf
    ("%s\n",buf);
    //_exit(0);
    return hr;
    }

    VARIANT
    *pArgs = new VARIANT[cArgs+1];
    for(int i=0; i<cArgs; i++) {
    pArgs
    [i] = va_arg(marker, VARIANT);
    }

    dp
    .cArgs = cArgs;
    dp
    .rgvarg = pArgs;

    if(autoType & DISPATCH_PROPERTYPUT) {
    dp
    .cNamedArgs = 1;
    dp
    .rgdispidNamedArgs = &dispidNamed;
    }
    hr
    = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp,
    pvResult
    , NULL, NULL);
    if(FAILED(hr)) {
    sprintf
    (buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx",
    szName
    , dispID, hr);
    printf
    ("%s\n",buf);
    //_exit(0);
    return hr;
    }
    va_end
    (marker);

    delete [] pArgs;

    return hr;
    }

    Thursday, February 28, 2013 6:22 AM

Answers

  • this is the effect of your range object - if you want to place hyperlink inside existing text you either have to move there selection object and get range from this or use find api from word and get range from it.
    Friday, March 1, 2013 5:06 AM
  • Hi Cappu893300,

    There have a similar code snippet which I provide in the forum for similar issue. I hope it can help you.

    // Outlook_AddLink.cpp : Defines the entry point for the console application.
     //
     
    #include "stdafx.h"
     #include <Windows.h>
     
    HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, 
        LPOLESTR ptName, int cArgs...) {
         va_list marker;
         va_start(marker, cArgs);
     
        if(!pDisp) {
             //_exit(0);
         }
     
        DISPPARAMS dp = { NULL, NULL, 0, 0 };
         DISPID dispidNamed = DISPID_PROPERTYPUT;
         DISPID dispID;
         HRESULT hr;
         char buf[200];
         char szName[200];
     
        WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
     
        hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, 
            &dispID);
         if(FAILED(hr)) {
             sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx",
                 szName, hr);
             printf("%s\n",buf);
             //_exit(0);
             return hr;
         }
     
        VARIANT *pArgs = new VARIANT[cArgs+1];
         for(int i=0; i<cArgs; i++) {
             pArgs[i] = va_arg(marker, VARIANT);
         }
     
        dp.cArgs = cArgs;
         dp.rgvarg = pArgs;
     
        if(autoType & DISPATCH_PROPERTYPUT) {
             dp.cNamedArgs = 1;
             dp.rgdispidNamedArgs = &dispidNamed;
         }
         hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, 
            pvResult, NULL, NULL);
         if(FAILED(hr)) {
             sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx",
                 szName, dispID, hr);
             printf("%s\n",buf);
             //_exit(0);
             return hr;
         }
         va_end(marker);
     
        delete [] pArgs;
     
        return hr;
     }
     
    int _tmain(int argc, _TCHAR* argv[])
     {
         HRESULT hr;
         CLSID olCLSID;
         IDispatch *olApp;
         hr =  OleInitialize(NULL);
         if(SUCCEEDED(hr)){      
            VARIANT m;
             m.vt = VT_ERROR;
             m.lVal = (long)DISP_E_PARAMNOTFOUND;
             CLSIDFromProgID(L"Outlook.Application",&olCLSID);
             hr = CoCreateInstance(olCLSID,NULL,
                 CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,IID_IDispatch,
                 (void **)&olApp);
             if(SUCCEEDED(hr)){
                 IDispatch * olAppointmentItem;
                 VARIANT x,T;
                 VariantInit(&x);
                 T.vt = VT_INT;
                 T.intVal = 1;
                 hr =  AutoWrap(DISPATCH_METHOD, &x,olApp,L"CreateItem",1,T);
                 if(SUCCEEDED(hr)){
                     olAppointmentItem = x.pdispVal;
                     IDispatch * olInspector;
                     T.vt = VT_BOOL;
                     T.boolVal = false;
                     VariantInit(&x);
                     hr =  AutoWrap(DISPATCH_METHOD, &x,olAppointmentItem,L"Display",
                         1,T);
                     VariantInit(&x);
                     hr =  AutoWrap(DISPATCH_METHOD, &x,olAppointmentItem,
                         L"GetInspector",0);
                     if(SUCCEEDED(hr)){
                         olInspector = x.pdispVal;
                         IDispatch * wdDocument;
                         hr = AutoWrap(DISPATCH_PROPERTYGET,&x,olInspector,
                             L"WordEditor",0);
                         if(SUCCEEDED(hr)){
                             wdDocument = x.pdispVal;
                             IDispatch * wdHyperLinks;
                             IDispatch * wdRange;
                             VariantInit(&x);
                             hr =  AutoWrap(DISPATCH_PROPERTYGET, &x,wdDocument,
                                 L"HyperLinks",0);
                             if(SUCCEEDED(hr)){
                                 wdHyperLinks = x.pdispVal;                          
                                hr =  AutoWrap(DISPATCH_METHOD,&x,wdDocument
                                     ,L"Range",2,m,m);
                                 if(SUCCEEDED(hr)){
                                     wdRange = x.pdispVal;
                                     VariantInit(&x);
                                     VARIANT A,D,Ar;
                                     A.vt = VT_BSTR;
                                     A.bstrVal = 
                                        SysAllocString(L"http://www.google.com");
                                     D.vt = VT_BSTR;
                                     Ar.vt = VT_DISPATCH;
                                     Ar.pdispVal = wdRange;
                                     D.bstrVal = SysAllocString(L"google");
                                     hr = AutoWrap(DISPATCH_METHOD,&x,wdHyperLinks,
                                         L"Add",6,m,D,m,m,A,Ar);
                                     if(SUCCEEDED(hr)){
                                         printf("Add HyperLink successfully!\n");
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         system("pause");
         CoUninitialize();
         return 0;
     }

    Have a good day,

    Tom

    Friday, March 1, 2013 1:29 AM
    Moderator

All replies

  • just to make sure: your code adds hyperlink but it is displayed as original url instead of 'click here' text?
    Thursday, February 28, 2013 7:44 AM
  • Hi Cappu893300,

    There have a similar code snippet which I provide in the forum for similar issue. I hope it can help you.

    // Outlook_AddLink.cpp : Defines the entry point for the console application.
     //
     
    #include "stdafx.h"
     #include <Windows.h>
     
    HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, 
        LPOLESTR ptName, int cArgs...) {
         va_list marker;
         va_start(marker, cArgs);
     
        if(!pDisp) {
             //_exit(0);
         }
     
        DISPPARAMS dp = { NULL, NULL, 0, 0 };
         DISPID dispidNamed = DISPID_PROPERTYPUT;
         DISPID dispID;
         HRESULT hr;
         char buf[200];
         char szName[200];
     
        WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
     
        hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, 
            &dispID);
         if(FAILED(hr)) {
             sprintf(buf, "IDispatch::GetIDsOfNames(\"%s\") failed w/err 0x%08lx",
                 szName, hr);
             printf("%s\n",buf);
             //_exit(0);
             return hr;
         }
     
        VARIANT *pArgs = new VARIANT[cArgs+1];
         for(int i=0; i<cArgs; i++) {
             pArgs[i] = va_arg(marker, VARIANT);
         }
     
        dp.cArgs = cArgs;
         dp.rgvarg = pArgs;
     
        if(autoType & DISPATCH_PROPERTYPUT) {
             dp.cNamedArgs = 1;
             dp.rgdispidNamedArgs = &dispidNamed;
         }
         hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, 
            pvResult, NULL, NULL);
         if(FAILED(hr)) {
             sprintf(buf, "IDispatch::Invoke(\"%s\"=%08lx) failed w/err 0x%08lx",
                 szName, dispID, hr);
             printf("%s\n",buf);
             //_exit(0);
             return hr;
         }
         va_end(marker);
     
        delete [] pArgs;
     
        return hr;
     }
     
    int _tmain(int argc, _TCHAR* argv[])
     {
         HRESULT hr;
         CLSID olCLSID;
         IDispatch *olApp;
         hr =  OleInitialize(NULL);
         if(SUCCEEDED(hr)){      
            VARIANT m;
             m.vt = VT_ERROR;
             m.lVal = (long)DISP_E_PARAMNOTFOUND;
             CLSIDFromProgID(L"Outlook.Application",&olCLSID);
             hr = CoCreateInstance(olCLSID,NULL,
                 CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,IID_IDispatch,
                 (void **)&olApp);
             if(SUCCEEDED(hr)){
                 IDispatch * olAppointmentItem;
                 VARIANT x,T;
                 VariantInit(&x);
                 T.vt = VT_INT;
                 T.intVal = 1;
                 hr =  AutoWrap(DISPATCH_METHOD, &x,olApp,L"CreateItem",1,T);
                 if(SUCCEEDED(hr)){
                     olAppointmentItem = x.pdispVal;
                     IDispatch * olInspector;
                     T.vt = VT_BOOL;
                     T.boolVal = false;
                     VariantInit(&x);
                     hr =  AutoWrap(DISPATCH_METHOD, &x,olAppointmentItem,L"Display",
                         1,T);
                     VariantInit(&x);
                     hr =  AutoWrap(DISPATCH_METHOD, &x,olAppointmentItem,
                         L"GetInspector",0);
                     if(SUCCEEDED(hr)){
                         olInspector = x.pdispVal;
                         IDispatch * wdDocument;
                         hr = AutoWrap(DISPATCH_PROPERTYGET,&x,olInspector,
                             L"WordEditor",0);
                         if(SUCCEEDED(hr)){
                             wdDocument = x.pdispVal;
                             IDispatch * wdHyperLinks;
                             IDispatch * wdRange;
                             VariantInit(&x);
                             hr =  AutoWrap(DISPATCH_PROPERTYGET, &x,wdDocument,
                                 L"HyperLinks",0);
                             if(SUCCEEDED(hr)){
                                 wdHyperLinks = x.pdispVal;                          
                                hr =  AutoWrap(DISPATCH_METHOD,&x,wdDocument
                                     ,L"Range",2,m,m);
                                 if(SUCCEEDED(hr)){
                                     wdRange = x.pdispVal;
                                     VariantInit(&x);
                                     VARIANT A,D,Ar;
                                     A.vt = VT_BSTR;
                                     A.bstrVal = 
                                        SysAllocString(L"http://www.google.com");
                                     D.vt = VT_BSTR;
                                     Ar.vt = VT_DISPATCH;
                                     Ar.pdispVal = wdRange;
                                     D.bstrVal = SysAllocString(L"google");
                                     hr = AutoWrap(DISPATCH_METHOD,&x,wdHyperLinks,
                                         L"Add",6,m,D,m,m,A,Ar);
                                     if(SUCCEEDED(hr)){
                                         printf("Add HyperLink successfully!\n");
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         system("pause");
         CoUninitialize();
         return 0;
     }

    Have a good day,

    Tom

    Friday, March 1, 2013 1:29 AM
    Moderator
  • my code only adds hyperlink Inthe forefront,such as

    Click here

    i don't really know the method "AutoWrap",now i want to make it like that:

    Friday, March 1, 2013 2:38 AM
  • my code only adds hyperlink Inthe forefront,such as

    Click here

    i don't really know the method "AutoWrap",now i want to make it like that:

    Friday, March 1, 2013 2:43 AM
  • this is the effect of your range object - if you want to place hyperlink inside existing text you either have to move there selection object and get range from this or use find api from word and get range from it.
    Friday, March 1, 2013 5:06 AM
  • Hi Cappu893300,

    I agree what DamianD said. I just provide your a general solution. You need complete it via yourself.

    Have a good day,

    Tom

    Monday, March 4, 2013 3:03 AM
    Moderator