Switch to late binding Outlook (msoutl.olb) (1 Viewer)

foshizzle

Registered User.
Local time
Today, 05:58
Joined
Nov 27, 2013
Messages
277
If someone could help, I need the following code changed to late binding.
My database worked great until a 32 bit machine tried to open it. I had the outlook reference set in Access for this to work. I

Currently trying to compare what I have with the following link, but looks like he has extra things in his code I dont use, or I am lacking something for it to work.
http://social.msdn.microsoft.com/Fo...166/switching-to-late-binding?forum=accessdev

Thanks

Code:
Private Sub btnEmail_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fs, f
Dim RTFBody, strTo
Dim MyApp As New Outlook.Application
Dim MyItem As Outlook.MailItem

'DoCmd.OutputTo acOutputReport, "rptBlotterEntryEmail", acFormatRTF, "rptBlotterEntryEmail"
DoCmd.OutputTo acOutputReport, "rptBlotterEntryEmail", acFormatHTML, "rptBlotterEntryEmail.htm"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("rptBlotterEntryEmail.htm", ForReading)
'Set f = fs.OpenTextFile("rptBlotterEntryEmail", ForReading)
RTFBody = f.ReadAll
f.Close

Set MyItem = MyApp.CreateItem(olMailItem)
With MyItem
.To = "myropctr@HorryCounty.org"
.CC = ""
.Subject = "Police Update"
.HTMLBody = RTFBody
End With
MyItem.Display
End Sub









Private Sub Form_BeforeUpdate(Cancel As Integer)
'If the form data has changed a message is shown asking if
   'the changes should be saved. If the answer is no then
   'the changes are undone
 
   On Error GoTo BeforeUpdate_Error
  
   If Me.Dirty Then
    'if record has been changed the dirty property
    ' is set to true Display message to save the record
      If MsgBox("Record has changed. Save changes?", _
      vbYesNo + vbQuestion, "Save Changes") = vbNo Then
         Me.Undo
      End If
   End If

BeforeUpdate_Exit:
   Exit Sub

BeforeUpdate_Error:
   MsgBox Err.Description
   Resume BeforeUpdate_Exit
End Sub
 

CJ_London

Super Moderator
Staff member
Local time
Today, 10:58
Joined
Feb 19, 2013
Messages
16,629
If you are moving to late binding (I presume for outlook) then things like

Dim MyApp As New Outlook.Application
Dim MyItem As Outlook.MailItem

need to be declared as objects and then set after creating the outlook object using createobject as you have for filescripting

and things like

olMailItem

need to either be declared as constants with the appropriate value or just use the appropriate value - you can find this value using the object explorer (hit F2 when in the vba window, then navigate to the appropriate class) - you'll need to temporarily add back the outlook library in references first.
 

foshizzle

Registered User.
Local time
Today, 05:58
Joined
Nov 27, 2013
Messages
277
Thanks - This was actually a sample code I picked up after much research on the internet, so I'm not much into programming. Can you elaborate please? I think I get some of what you say - I'm also not sure if Dim MyApp As New Object needs to include the "New" when setting it as an object. (I assume not because it gave me an error when typing it)

As for using the object explorer. When I hit F2, I get the All Libraries window but Im not sure what Im looking for. I found Outlook MailItem saying member of OutlookVersion..

Not sure what to do after that. please see screenshot

This is what I have so far (I think this is what you mean) Please review
Code:
Private Sub btnEmail_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fs, f
Dim RTFBody, strTo
'Dim MyApp As New Outlook.Application
Dim MyApp As Object
'Dim MyItem As Outlook.MailItem
Dim MyItem As Object

'DoCmd.OutputTo acOutputReport, "rptBlotterEntryEmail", acFormatRTF, "rptBlotterEntryEmail"
DoCmd.OutputTo acOutputReport, "rptBlotterEntryEmail", acFormatHTML, "rptBlotterEntryEmail.htm"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("rptBlotterEntryEmail.htm", ForReading)
'Set f = fs.OpenTextFile("rptBlotterEntryEmail", ForReading)
RTFBody = f.ReadAll
f.Close

'Set MyItem = MyApp.CreateItem(olMailItem)
Set MyItem = MyApp.CreateObject(olMailItem)
With MyItem
.To = "myropctr@HorryCounty.org"
.CC = ""
.Subject = "Police Update"
.HTMLBody = RTFBody
End With
MyItem.Display
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
'If the form data has changed a message is shown asking if
   'the changes should be saved. If the answer is no then
   'the changes are undone
 
   On Error GoTo BeforeUpdate_Error
  
   If Me.Dirty Then
    'if record has been changed the dirty property
    ' is set to true Display message to save the record
      If MsgBox("Record has changed. Save changes?", _
      vbYesNo + vbQuestion, "Save Changes") = vbNo Then
         Me.Undo
      End If
   End If

BeforeUpdate_Exit:
   Exit Sub

BeforeUpdate_Error:
   MsgBox Err.Description
   Resume BeforeUpdate_Exit
End Sub
 

Attachments

  • F2.jpg
    F2.jpg
    97.7 KB · Views: 183

CJ_London

Super Moderator
Staff member
Local time
Today, 10:58
Joined
Feb 19, 2013
Messages
16,629
The item you have found is mailitem, not olmailitem - which has a value of 0.

So when late binding you either need to declare olMailItem as follows

Const olMailItem=0

or change

Set MyItem = MyApp.CreateObject(olMailItem)

to

Set MyItem = MyApp.CreateObject(0)

with regards learning about late binding there are plenty of tutorials out there - here are a few links to get you going

http://support.microsoft.com/kb/245115
http://www.access-programmers.co.uk/forums/showthread.php?t=206526&highlight=test+if+outlook
http://stackoverflow.com/questions/19184650/late-binding-to-open-outlook-from-access
 

foshizzle

Registered User.
Local time
Today, 05:58
Joined
Nov 27, 2013
Messages
277
Awesome, thanks that works
And thanks for the links
 

Users who are viewing this thread

Top Bottom