0

I have code that works fine in all aspects, but I can not find a way to create a hyperlink in an Outlook appointment. The address is placed in column H in Excel, and I want to use VBA to export it to a certain calendar. Any help would greatly be appriciated.

My code is as follows:

Sub Appointments()
        
        Const olAppointmentItem As Long = 1
        
        Dim OLApp As Object
        Dim OLNS As Object
        Dim OLAppointment As Object
        Dim miCalendario As Object
        Dim r As Long
        On Error Resume Next
        Set OLApp = GetObject(, "Outlook.Application")
        If OLApp Is Nothing Then Set OLApp = CreateObject("Outlook.Application")
        On Error GoTo 0
        
        If Not OLApp Is Nothing Then
        
            Set OLNS = OLApp.GetNamespace("MAPI")
            OLNS.Logon "Outlook"
               
            b = 1
            r = 2
            
            Dim mysub, myStart, myEnd, mydes, myallday
            While Len(Cells(r, 5).Text) <> 0
                mysub = Cells(r, 7)
                If Not Cells(r, 13).Value = 0 Then
                mysub = mysub & "(s. " & Cells(r, 13).Value & ")" & vbCrLf
                End If
                '& ", " & Cells(r, 3)
                myStart = DateValue(Cells(r, 1).Value) + Cells(r, 2).Value
                myEnd = DateValue(Cells(r, 1).Value) + Cells(r, 3).Value
        
        mydes = ""
            
            
        Set miCalendario = OLApp.Session.GetDefaultFolder(9).Folders(ActiveSheet.Name)
        
        Set OLAppointment = miCalendario.Items.Add(olAppointmentItem)
        Dim olItems As Items
        Dim olApptItem As Outlook.AppointmentItem
        Set olItems = miCalendario.Items
        Set olApptItem = miCalendario.Items.GetFirst
           
            'add appointments
         On Error Resume Next
            With OLAppointment
        .Subject = mysub
        .Start = myStart
        .End = myEnd
        .Body = mydes
         
        If Not Cells(r, 1).Value = 0 Then
        
        If Not Cells(r, 8).Value = 0 Then
        mydes = mydes & Cells(1, 8).Value & " - " & Cells(r, 8).Value & vbCrLf
        End If
           
        .Body = mydes
        
        End If
        
        
        
        .Location = Cells(r, 4).Value    .Save
        
            End With
        r = r + 1
        b = b + 1
        Wend
            Set OLAppointment = Nothing
            Set OLNS = Nothing
            Set OLApp = Nothing
        
        End If
End Sub
    
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
orjansj
  • 21
  • 5
  • Possible duplicate of [HTMLBody Workaround For OlAppointment Object?](https://stackoverflow.com/questions/37014913/htmlbody-workaround-for-olappointment-object) – niton Sep 08 '20 at 22:22
  • This might be possible, but I do not manage adding an address in Excel, and add it to a text in another cell (hyperlink), and implement this in a calandar of my choosing. Thanks for trying – orjansj Sep 09 '20 at 13:03
  • With email address in H2 and `ThisWorkbook.Worksheets("Sheet1").Range("H2").Copy` in the code in the suggested duplicate. Right click in the appointment. There should be an option to Open Hyperlink. – niton Sep 09 '20 at 14:19
  • Thanks for helping. Is this what you mean: – orjansj Sep 10 '20 at 09:55
  • Simply replace Sheet1.ListObjects(1).Range.Copy with ThisWorkbook.Worksheets("Sheet1").Range("H2").Copy ? Then no adress is implemented in body – orjansj Sep 10 '20 at 10:00
  • That is it. Contents of H2 should have been pasted. To verify the paste, manually copy an email address ctrl + c, comment the `.Copy` line, then run the code. – niton Sep 10 '20 at 12:23
  • Thanks for any help, but this does not work. End up With a run-time error '287' – orjansj Sep 11 '20 at 19:50
  • There is other code that works for that responder and for me. Copy an email address then run https://stackoverflow.com/questions/34665581/formatting-appointment-body. If not successful you can ask a new question, with the code you have in your editor, linking to the older questions. – niton Sep 11 '20 at 20:45

1 Answers1

-1

You need to use .HTMLBody insted .Body

.HTMLbody = <a href="link">"link_Mask"</a>

I hope it'll help

usar93
  • 1
  • Hi, This only works for Outlook mail, not calendar. Thanks for trying – orjansj Sep 09 '20 at 13:01
  • Yeah :( I found something like this https://stackoverflow.com/questions/37014913/htmlbody-workaround-for-olappointment-object – usar93 Sep 09 '20 at 13:17