none
Cant set the RTFBody of an AppointmentItem RRS feed

  • Question

  • I've cribbed this off the internet as I want to set the RTFBody of an appointment item

    Imports outlook = Microsoft.Office.Interop.Outlook
    
    Module Module1
        Sub Main()
            Dim oApp As outlook.Application = New outlook.Application
            Dim t As outlook.AppointmentItem = DirectCast(oApp.CreateItem(outlook.OlItemType.olAppointmentItem), outlook.AppointmentItem)
    
            t.Start = Convert.ToDateTime("11/05/2015")
            t.End = Convert.ToDateTime("11/05/2015")
            t.Subject = "VB.Net test"
            t.RTFBody = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
        End Sub
    End Module

    The last line gives an error when trying to set the RTFBody. 

    This page seems to make the very odd suggestion that it can only be set during a send operation ??

    https://support.microsoft.com/en-us/kb/2605734

    Wednesday, November 4, 2015 10:37 AM

Answers

  • Thanks. This now works

    Module Module1
    
        Sub Main()
            Dim oApp As Object
            'Dim t As Object
            Dim t As Microsoft.Office.Interop.Outlook.AppointmentItem
    
            oApp = CreateObject("Outlook.Application")
            t = oApp.CreateItem(1)
    
            t.Start = Convert.ToDateTime("11/05/2015")
            t.End = Convert.ToDateTime("11/05/2015")
            t.Subject = "VB COM test"
            t.RTFBody = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
            t.Display()
        End Sub
    
    End Module

    I assume this is classed as early binding ?

    In your previous message you said " There is a difference in the RtfBody property behavior based on whether you use early or late binding."

    Please can you point me at a web page that explains the difference in this behavior for this property so I can pass on the info to the developers of the language I am using to see if anything can be done t get it to work

    The difference in the early/late behavior for a given property is unusual ?

    • Marked as answer by Jellybabies Tuesday, November 24, 2015 11:27 AM
    Tuesday, November 24, 2015 10:46 AM
  • There is no web page - it is just a known problem in OOM - setting the RtfBody property using early binding works. Setting through IDispatch::GetIDsIfNames / Invoke does not.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Jellybabies Tuesday, November 24, 2015 2:22 PM
    Tuesday, November 24, 2015 2:14 PM

All replies

  • What error do you get? What property or method exactly fires the exception?

    The RTFBody property of the AppointmentItem class allows to set a byte array that represents the body in Rich Text Format (RTF).

    You may consider using the WordEditor property which is available in Outlook 2007 and above, it returns a Microsoft Word Document object.

    Wednesday, November 4, 2015 12:59 PM
  • The error is

    System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Value does not fall within the expected range.
      Source=Microsoft.VisualBasic
      StackTrace:
           at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateSet(Object o, Type& objType, String name, Object[] args, String[] paramnames, Boolean OptimisticSet, CallType UseCallType)
           at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean OptimisticSet, Boolean RValueBase, CallType CallType)
           at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments)
           at ConsoleApplication1.Module1.Main() in C:\AKDocsFromWhenConnectedToCallisto\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Module1.vb:line 12
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    Wednesday, November 4, 2015 1:12 PM
  • Where and when do you run the code?
    Wednesday, November 4, 2015 1:34 PM
  • The code above is the whole application.

    I just started an new project > console application

    and added the Microsoft.Office.Interop,Outllook .NET reference

    then just clicked run

    Wednesday, November 4, 2015 1:38 PM
  • Hi Jellybabies,

    I made a test with your description, but I failed to reproduce your issue under Outlook 2013 and VS2013. It worked correctly at my side with the code below:

    Imports outlook = Microsoft.Office.Interop.Outlook
    Module Module1
    
        Sub Main()
            Dim oApp As outlook.Application = New outlook.Application
            Dim t As outlook.AppointmentItem = DirectCast(oApp.CreateItem(outlook.OlItemType.olAppointmentItem), outlook.AppointmentItem)
    
            t.Start = Convert.ToDateTime("11/05/2015")
            t.End = Convert.ToDateTime("11/05/2015")
            t.Subject = "VB.Net test"
            t.RTFBody = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
            t.Display()
        End Sub
    
    End Module

    What is your Office version and VS version? If you manually enter rtf text string in outlook, will you get error? For a general suggestion, I suggest you update your Office and VS to latest version.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Thursday, November 5, 2015 5:46 AM
  • Thanks for taking the trouble to try this for me 

    I'm using VS2010 and Office 2010

    I pasted you code back over mine and I still get the same result

    It's looking like there is a bug in Office 2010 when trying to set this property although there doesn't seem to be much hard evidence of this on the net beyond the link in my original post

    Thursday, November 5, 2015 9:25 AM
  • Hi Jellybabies,

    I made a test under VS2010 an Office 2010, and it worked correctly at my side.

    I suggest you test this code in other pcs to check whether this issue still exists.

    In addition, I suggest you repair your Outlook.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, November 6, 2015 6:46 AM
  • Hmmmmm

    Thanks for trying this out again for me

    I have now run a repair on my Office 2010 and rebooted

    Run a repair on VS2010 (which took an absolute age) It warbled about SQL 3.5 compact edition but everything else was fine so I rebooted

    Opened VS2010, loaded the project, added a blank line to force recompile .... and .... same result

    So the question is what does your machine have that mine doesn't

    The fact it works for you explains why i'm not finding much on the net about this issue

    I wonder what it could be !

    Friday, November 6, 2015 2:28 PM
  • Hi Jellybabies,

    To narrow down this issue, I suggest you achieve this function with vba code to check whether it get this error.

    In addition, I suggest you update your Office to latest version.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, November 7, 2015 4:06 AM
  • Hi Edward

    Updating to the latest office is not really an option in the sense we need to be able to support 2010 with any new features we add to our application.

    I'm not a VBA programmer but I've had a quick go at this in the ThisOutlookSession on my machine

    Sub Test()
        Dim oApp As Outlook.Application
        Dim t As Outlook.AppointmentItem
        Set t = Application.CreateItem(olAppointmentItem)
        t.Start = CDate("11/05/2015")
        t.End = CDate("11/05/2015")
        t.Subject = "VB.Net test"
        t.RTFBody = StrConv("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}", vbFromUnicode)
        t.Display
    End Sub


    When I run it I get this error on trying to set the RTFBody

    ---------------------------
    Microsoft Visual Basic for Applications
    ---------------------------
    Run-time error '-1629421569 (9ee0ffff)':

    The operation failed.
    ---------------------------
    OK   Help   
    ---------------------------

    Thanks


    Wednesday, November 11, 2015 10:59 AM
  • Hi Jellybabies,

    I suggest you test with the code below.

    Sub test1()
    Dim t As AppointmentItem
            Set t = Application.CreateItem(olAppointmentItem)
    
            t.Subject = "VB.Net test"
    
            strRTF = "{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}"
           Dim byteRTF() As Byte
    
           byteRTF = StrConv(strRTF, vbFromUnicode)
    
            t.RTFBody = byteRTF
           t.Display
    End Sub

    In addition, if this code did not work either, I assume your Outlook is broken, and I suggest you uninstall and install your outlook again.

    Best Regards,

    Edward

                               

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 12, 2015 9:45 AM
  • Edward thanks very much for sticking with this !

    Well the god news is (I think !) is that I pasted your code into ThisOutlookSession and ran it and it worked !

    So my Outlook is not broken !

    Not sure where this leaves me in that I am trying to integrate with Outlook via COM automation from anther language (Dataflex) but at least this is some good progress !

    Thanks

    Thursday, November 12, 2015 10:05 AM
  • So I guess the next question is how do I set this via COM from VB.net in VS2010. 

    You say you have already achieved this ?

    Thursday, November 12, 2015 1:41 PM
  • You may find the following sample projects helpful:

    VB app automates Outlook (VBAutomateOutlook)

    VB .NET OUTLOOK SEND EMAIL

    Thursday, November 12, 2015 5:31 PM
  • Hi Jellybabies,

    >>how do I set this via COM from VB.net in VS2010

    Since vba code worked correctly in outlook, it seems your issue is related with your vb.net project in VS2010. I reproduced your issue accidentally. I assume you add a wrong version reference. If you add Microsoft.Office.Interop.Outlook 12.0, I could reproduce your issue. I suggest you remove this reference, and add Microsoft.Office.Interop.Outlook 14.0.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, November 13, 2015 2:47 AM
  • A huge thanks for sticking with this

    You spotted my schoolboy error !

    I have changed it to Microsoft.Office.Interop.Outlook 14.0 and can confirm it works

    As you know this is done on the References .NET tab which I assume means we are interacting with it as a .NET component

    I would be hugely grateful if you could create this same simple sample but using the COM interface instead as that is ultimately how I am trying to communicate with it

    Thanks again

    Friday, November 13, 2015 9:58 AM
  • OK a bit of progress on my part as I've managed to get this to work EXCEPT setting the RTFBody which still gives me my original error

    Module Module1
    
        Sub Main()
            Dim oApp As Object
            Dim t As Object
    
            oApp = CreateObject("Outlook.Application")
            t = oApp.CreateItem(1)
    
            t.Start = Convert.ToDateTime("11/05/2015")
            t.End = Convert.ToDateTime("11/05/2015")
            t.Subject = "VB COM test"
            t.RTFBody = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
            t.Display()
        End Sub
    
    End Module

    When I added the Outlook 14.0 from the COM tab I got these references (pic attached)


    • Edited by Jellybabies Friday, November 13, 2015 11:24 AM
    Friday, November 13, 2015 10:43 AM
  • Did you try to use a Word editor instead? See Chapter 17: Working with Item Bodies for more information.
    Friday, November 13, 2015 10:50 AM
  • Because the source I am using to populate the appointment creates RTF data

    As RTF was created by MS this really should work

    I've now got it working from inside Outlook and via .NET interop but not via COM (which is what I need)


    • Edited by Jellybabies Friday, November 13, 2015 11:10 AM
    Friday, November 13, 2015 11:10 AM
  • It would be helpful if you posted the COM code that you are using and provided more information about the errors that you are experiencing.
    Friday, November 13, 2015 12:41 PM
  • The COM code I am using is 4 posts above this one

    The error I get on the t.RTFBody= line   is   Value does not fall within the expected range.

    Thanks

    Friday, November 13, 2015 1:12 PM
  • Have you tried using System.Text.Encoding.AsciiEncoding.GetString()
    Friday, November 13, 2015 1:26 PM
  • When I type System.Text.Encoding  AsciiEncoding is not offered by code sense ?

    Thanks

    Friday, November 13, 2015 1:44 PM
  • Sorry, it should probably be System.Text.ASCIIEncoding.GetString.  Per ASCIIEncoding.GetString Method

    • Edited by RLWA32 Friday, November 13, 2015 1:56 PM fix typos
    Friday, November 13, 2015 1:50 PM
  • Unless I'm missing something (which I could be!) this seems to take a byte array and retun a string, where as I need to take a string and return a NON unicode byte array

    Thanks

    Friday, November 13, 2015 3:51 PM
  •         Dim encodedBytes As Byte()
            encodedBytes = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
            t.RTFBody = encodedBytes
    

    The above snipped does seem to put the right sort of data in the byte array but I still get the error when trying to set the property

    Thanks

    Friday, November 13, 2015 4:06 PM
  • Could it be that the bytes array in VB is not a 'pure' array but a safearray ?

    If so how do I pass the pure byte array via COM ?

    Friday, November 13, 2015 4:54 PM
  • OK a bit of progress on my part as I've managed to get this to work EXCEPT setting the RTFBody which still gives me my original error

    Module Module1
    
        Sub Main()
            Dim oApp As Object
            Dim t As Object
    
            oApp = CreateObject("Outlook.Application")
            t = oApp.CreateItem(1)
    
            t.Start = Convert.ToDateTime("11/05/2015")
            t.End = Convert.ToDateTime("11/05/2015")
            t.Subject = "VB COM test"
            t.RTFBody = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
            t.Display()
        End Sub
    
    End Module

    When I added the Outlook 14.0 from the COM tab I got these references (pic attached)


    Hi Jellybabies,

    Based on your description, it seems that using the com interface is using the reference in the com tab, am I right? In my option, both the .net tab and com tab are ok, .net tab load the reference from .net dll, and com tab load the reference from GAC_MSIL.

    I suggest you follow the steps below to create a project via com tab reference.

    1. Create a new vb.net console application
    2. Add reference Microsoft Outlook 14.0 Object Library in the com tab
    3. Add the your code to the new project
    4. Build your project and run it.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, November 16, 2015 5:38 AM
  • Thanks Edward

    I thought that is what I had done, but I've just done it again with attached screen shots but I still get the same error

    Create Project

    Add COM reference

    Show References loaded

    Error Text

    Monday, November 23, 2015 9:45 AM
  • Try to declare oApp as AppointmentItem instead of generic object. There is a difference in the RtfBody property behavior based on whether you use early or late binding.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!


    Monday, November 23, 2015 6:05 PM
  • Thanks for the reply Please can you show me how the Dim statement should look as Im not sure how to do thet in vb with COM Thanks
    Monday, November 23, 2015 6:19 PM
  • Add Outlook to the project references and change that line to

    Dim t As Outlook.AppointmentItem


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, November 24, 2015 12:59 AM
  • Is this what you meant as it wont compile ?

    Tuesday, November 24, 2015 10:17 AM
  • Use the fully qualified name instead:

    Dim t as Microsoft.Office.Interop.Outlook

    Tuesday, November 24, 2015 10:35 AM
  • Thanks. This now works

    Module Module1
    
        Sub Main()
            Dim oApp As Object
            'Dim t As Object
            Dim t As Microsoft.Office.Interop.Outlook.AppointmentItem
    
            oApp = CreateObject("Outlook.Application")
            t = oApp.CreateItem(1)
    
            t.Start = Convert.ToDateTime("11/05/2015")
            t.End = Convert.ToDateTime("11/05/2015")
            t.Subject = "VB COM test"
            t.RTFBody = System.Text.Encoding.ASCII.GetBytes("{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Riched20 15.0.4599}{\*\mmathPr\mwrapIndent1440 }\viewkind4\uc1 \pard\f0\fs22 Test Body: First Line\parSecond Line of Text\par}")
            t.Display()
        End Sub
    
    End Module

    I assume this is classed as early binding ?

    In your previous message you said " There is a difference in the RtfBody property behavior based on whether you use early or late binding."

    Please can you point me at a web page that explains the difference in this behavior for this property so I can pass on the info to the developers of the language I am using to see if anything can be done t get it to work

    The difference in the early/late behavior for a given property is unusual ?

    • Marked as answer by Jellybabies Tuesday, November 24, 2015 11:27 AM
    Tuesday, November 24, 2015 10:46 AM
  • Yes, an early binding mechanism is used in the code. Use the Type.InvokeMember if you need to use the late binding.
    Tuesday, November 24, 2015 10:51 AM
  • There is no web page - it is just a known problem in OOM - setting the RtfBody property using early binding works. Setting through IDispatch::GetIDsIfNames / Invoke does not.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Jellybabies Tuesday, November 24, 2015 2:22 PM
    Tuesday, November 24, 2015 2:14 PM