Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How correctly set User Properties to MS Outlook Mail Item in MFC

How correctly set User Properties to MS Outlook Mail Item in MFC

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomquestionc++
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2970611
    wrote on last edited by
    #1

    Hi I have a problem with an MFC Project that opens an email template, and sets some user properties on the mail item. The code I have been given with is as follows: SetProperties() { LPDISPATCH userProps; outlookmailitem->InvokeHelper(0xf816, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&userProps, NULL); COleDispatchDriver* outlookUser = new COleDispatchDriver(userProps); LPDISPATCH userProp1; static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; CString propertyName = _T("Email Marking"); outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here! COleDispatchDriver* outlookUserProp = new COleDispatchDriver(userProp1); static BYTE params2[] = VTS_VARIANT; VARIANT va1; va1.vt = VT_BSTR; va1.bstrVal = emailMarking.AllocSysString(); outlookUserProp->InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, params2, &va1); delete(outlookUserProp); Where I have added "//falls here!" is where it fails, and I think this is parameter issue, again. I cannot tell you what the error message I am getting because all that VS2010 is giving me is unhandled exception. Looking at MSDN for VBA - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.add.aspx#Y19 - it is final two parameters to me that Outlook is expecting, add to folders and display format. What I am trying to achieve is to ensure that the add to folder is set to false, and for the final one, I would like to set this to text if possible. How can set theses in MFC? What is the correct syntax? Do they need to set in this way or could Outlook set theses with the parameters being passed? Thanks

    D R 2 Replies Last reply
    0
    • U User 2970611

      Hi I have a problem with an MFC Project that opens an email template, and sets some user properties on the mail item. The code I have been given with is as follows: SetProperties() { LPDISPATCH userProps; outlookmailitem->InvokeHelper(0xf816, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&userProps, NULL); COleDispatchDriver* outlookUser = new COleDispatchDriver(userProps); LPDISPATCH userProp1; static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; CString propertyName = _T("Email Marking"); outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here! COleDispatchDriver* outlookUserProp = new COleDispatchDriver(userProp1); static BYTE params2[] = VTS_VARIANT; VARIANT va1; va1.vt = VT_BSTR; va1.bstrVal = emailMarking.AllocSysString(); outlookUserProp->InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, params2, &va1); delete(outlookUserProp); Where I have added "//falls here!" is where it fails, and I think this is parameter issue, again. I cannot tell you what the error message I am getting because all that VS2010 is giving me is unhandled exception. Looking at MSDN for VBA - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.add.aspx#Y19 - it is final two parameters to me that Outlook is expecting, add to folders and display format. What I am trying to achieve is to ensure that the add to folder is set to false, and for the final one, I would like to set this to text if possible. How can set theses in MFC? What is the correct syntax? Do they need to set in this way or could Outlook set theses with the parameters being passed? Thanks

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Member 2972992 wrote:

      CString propertyName = _T("Email Marking");
      outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here!

      Using a CString object is probably what is causing you grief.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

      U 1 Reply Last reply
      0
      • D David Crow

        Member 2972992 wrote:

        CString propertyName = _T("Email Marking");
        outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here!

        Using a CString object is probably what is causing you grief.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

        U Offline
        U Offline
        User 2970611
        wrote on last edited by
        #3

        No, I think - tatic BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; is correct because I have seen this in http://repositorium.googlecode.com/svn/trunk/AbstractSpoon/OutlookImpExp/Source/msoutl.cpp What should CString be?

        D 1 Reply Last reply
        0
        • U User 2970611

          No, I think - tatic BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; is correct because I have seen this in http://repositorium.googlecode.com/svn/trunk/AbstractSpoon/OutlookImpExp/Source/msoutl.cpp What should CString be?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Member 2972992 wrote:

          What should CString be?

          Without actually trying it, I honestly do not know. I'd try VARIANT and BSTR to see what they produce. Since InvokeHelper() throws an exception, why are you not catching it and examining the actual cause?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

          1 Reply Last reply
          0
          • U User 2970611

            Hi I have a problem with an MFC Project that opens an email template, and sets some user properties on the mail item. The code I have been given with is as follows: SetProperties() { LPDISPATCH userProps; outlookmailitem->InvokeHelper(0xf816, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&userProps, NULL); COleDispatchDriver* outlookUser = new COleDispatchDriver(userProps); LPDISPATCH userProp1; static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; CString propertyName = _T("Email Marking"); outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here! COleDispatchDriver* outlookUserProp = new COleDispatchDriver(userProp1); static BYTE params2[] = VTS_VARIANT; VARIANT va1; va1.vt = VT_BSTR; va1.bstrVal = emailMarking.AllocSysString(); outlookUserProp->InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, params2, &va1); delete(outlookUserProp); Where I have added "//falls here!" is where it fails, and I think this is parameter issue, again. I cannot tell you what the error message I am getting because all that VS2010 is giving me is unhandled exception. Looking at MSDN for VBA - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.add.aspx#Y19 - it is final two parameters to me that Outlook is expecting, add to folders and display format. What I am trying to achieve is to ensure that the add to folder is set to false, and for the final one, I would like to set this to text if possible. How can set theses in MFC? What is the correct syntax? Do they need to set in this way or could Outlook set theses with the parameters being passed? Thanks

            R Offline
            R Offline
            Roger Broomfield
            wrote on last edited by
            #5

            I would hazard a guess from the code that its the same issue you had before, regarding the passing of [in, optional] VARIANT parameters. Everytime you come across an [in, optional] VARIANT parameter, what you have to pass is the address of a VARAIANT that contains type VT_ERROR, and value DISP_E_PARAMNOTFOUND. In this case, from the static BYTE params[] declaration there are two "optional" parameters.

            COleVariant varOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
            LPDISPATCH userProp1;
            static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT;
            CString propertyName = _T("Email Marking");
            outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, (LPCTSTR)propertyName, 1, &varOptional, &varOptional);

            Notes: I have not looked up what dispatch method 0x66 is so I am not 100% sure. The presence of a VTS_VARIANT in the params array does not always mean optional, however an optional parameter is always a variant. The VTS_BSTR in the params array of an InvokeHelper call is expected to be an LPCTSTR. to allow the CString to pass the correct value always type cast it to (LPCTSTR). The type casting is neccessary because InvokeHelper is declared as void AFX_CDECL InvokeHelper(DISPID dwDispID, WORD wFlags, VARTYPE vtRet, void* pvRet, const BYTE* pbParamInfo, ...); so the compiler doesnt know specifically what to pass. By type casting it you ensure the correct value is passed.

            U 1 Reply Last reply
            0
            • R Roger Broomfield

              I would hazard a guess from the code that its the same issue you had before, regarding the passing of [in, optional] VARIANT parameters. Everytime you come across an [in, optional] VARIANT parameter, what you have to pass is the address of a VARAIANT that contains type VT_ERROR, and value DISP_E_PARAMNOTFOUND. In this case, from the static BYTE params[] declaration there are two "optional" parameters.

              COleVariant varOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
              LPDISPATCH userProp1;
              static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT;
              CString propertyName = _T("Email Marking");
              outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, (LPCTSTR)propertyName, 1, &varOptional, &varOptional);

              Notes: I have not looked up what dispatch method 0x66 is so I am not 100% sure. The presence of a VTS_VARIANT in the params array does not always mean optional, however an optional parameter is always a variant. The VTS_BSTR in the params array of an InvokeHelper call is expected to be an LPCTSTR. to allow the CString to pass the correct value always type cast it to (LPCTSTR). The type casting is neccessary because InvokeHelper is declared as void AFX_CDECL InvokeHelper(DISPID dwDispID, WORD wFlags, VARTYPE vtRet, void* pvRet, const BYTE* pbParamInfo, ...); so the compiler doesnt know specifically what to pass. By type casting it you ensure the correct value is passed.

              U Offline
              U Offline
              User 2970611
              wrote on last edited by
              #6

              Hi I have tried your suggestion and that did work! However, because one of parameters is a bool what is the correct syntax for passing a bool? Would it be COleVariantBool varOptionalBool (VT_TRUE, VT_ERROR); ? Further, when the line for saving the properties is called InvokeHelper(0xf048, DISPATCH_METHOD, VT_EMPTY, NULL, NULL); It gives an unhandled exception again. Looking at the MS Office VBA for this, properties don't save method, and it appears to me that when you add and set the properties this carries out the saving. Have I got this wrong? Is there something else I need to do? Thanks

              R 1 Reply Last reply
              0
              • U User 2970611

                Hi I have tried your suggestion and that did work! However, because one of parameters is a bool what is the correct syntax for passing a bool? Would it be COleVariantBool varOptionalBool (VT_TRUE, VT_ERROR); ? Further, when the line for saving the properties is called InvokeHelper(0xf048, DISPATCH_METHOD, VT_EMPTY, NULL, NULL); It gives an unhandled exception again. Looking at the MS Office VBA for this, properties don't save method, and it appears to me that when you add and set the properties this carries out the saving. Have I got this wrong? Is there something else I need to do? Thanks

                R Offline
                R Offline
                Roger Broomfield
                wrote on last edited by
                #7

                To pass a bool use one of these

                COleVariant varTRUE((short)-1,VT\_BOOL);
                COleVariant varFALSE((short)0,VT\_BOOL);
                

                I am unsure why Save (0xf048) would cause an exception. What is the exception ? I am also unsure why you dont use type-safe methods of invoking Outlook. Open the Class Wizard dialog "key combination Ctrl+W". Click "Add Class...". Click "From a type library...". Locate your MSOUTLxx.OLB, mine is in "C:\Program Files\Microsoft Office\Office12" click Open. Select the appropriate classes, or all of them, and click OK. This will add to your project header and implementation files that, in my humble opinion, will make your life easier.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups