Find mailfolder in Outlook with Redemption - vba

I try to find a folder in an Outlook account (I use Multiple accounts) using VBA and Redemption by using the FIND method but I cannot get it to work. On the Redemption webpage there is a reference made to an example and this may help but unfortunately the example isn't there.
Here's my code so far:
Public Function FindFolderRDO(strCrit As String) As String
If Not TempVars![appdebug] Then On Error GoTo Err_Proc
Dim objRdoSession As Redemption.RDOSession
Dim objRdoFolder As RDOFolder
Dim strFoundFolder As String
Dim objFoundFolder As RDOFolder
Dim strFolderName As String
Set objRdoSession = CreateObject("Redemption.RDOSession")
objRdoSession.Logon
objRdoSession.MAPIOBJECT = Outlook.Session.MAPIOBJECT
strFolderName = "\\[mailbox name]\[foldername]\[foldername]" 'actual names removed
Set objRdoFolder = objRdoSession.GetFolderFromPath(strFolderName)
Debug.Print objRdoFolder.Parent.Name 'Prints the folder name
Set objFoundFolder = objRdoFolder.Folders.Find("LIKE 'strCrit%' ") 'Does not work
Debug.Print objFoundFolder.Name
strFoundFOlder = objRdoFolder.Folders.Find("LIKE 'strCrit%' ") 'Does not work
Debug.Print strFoundFOlder
Exit_Proc:
On Error Resume Next
Set objRdoFolder = Nothing
Set objRdoSession = Nothing
Set objFoundFolder = Nothing
Exit Function
Err_Proc:
Select Case Err.Number
Case Else
MsgBox "Error: " & CStr(Err.Number) & vbCrLf & _
"Desc: " & Err.Description & vbCrLf & vbCrLf & _
"Source: " & Err.Source & vbCrLf & _
"Library: " & Application.CurrentProject.Name & vbCrLf & _
"Module: Mod_RDO" & vbCrLf & _
"Function: FindFolderRDO" & vbCrLf, _
vbCritical, "Error"
End Select
Resume Exit_Proc
End Function
Purpose of this function is to find a subfolder (can be up to 4 dimensions deep) having an unique case number of 6 numbers (for example "200332") on the first 6 positions. This function should provide NULL if not found or the full path and the name of the found folder.
I can create the full path with a seperate function (calling the parent folder until top level) but maybe there is a procedure in Redemption such as "fullpath" which I overlooked.
Eventually I want to use this function to delete, move or rename the mailbox folder.
My main question is how to use the "Find(Filter)" method. But any reply on the full path is welcome as well.
Thx! Art.

You are you trying to find a suborder with a name that starts with "strCrit"?
You are almost there:
Set objFoundFolder = objRdoFolder.Folders.Find("Name LIKE 'strCrit%' ")

Related

ListObject Error upon applying an Unlist Method

Basically, I have an Excel Formatted Table called "TestTable" in my activesheet. That's the only table in that sheet. I'm trying to convert it to a normal range. From looking up online, this should be simple, all I have to do is Unlist that table object.
However, my VBA code is throwing an error. Any pointers in the right direction would be greatly appreciated.
Sub ConverToNormalRange()
Dim objListObj As ListObject
Set objListObj = ActiveSheet.ListObjects(1)
objListObj.Unlist
End Sub
When I run the above macro, I get the following error:
Convert First Table to a Range
Sub ConvertToRange()
Const ProcName As String = "ConvertToRange"
On Error GoTo ClearError
With ActiveSheet ' improve!
If .ListObjects.Count > 0 Then
Dim tblName As String
With .ListObjects(1)
tblName = .Name
.Unlist
End With
MsgBox "Table '" & tblName & "' converted to a range.", _
vbInformation
Else
MsgBox "No table found in worksheet '" & .Name & "'.", _
vbExclamation
End If
End With
ProcExit:
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Sub
I tried converting the table manually and it wasn't doing anything either. So then I figured it wasn't a VBA problem. It turns out that I had connections open in Power Query, and it was preventing the table from converting back to normal range.

Extracting Data Relating To Reminders Snoozed

I have no VBA knowledge but am on a passage of learning. I have obtained the following coding from a public source (Diane Peremsky) of outlook forums. It has a bug I am working on to resolve and strangely returns different data on successive iterations.
Could somebody try provide (or guide me) to add the first 3 lines of the message body to which it refers?
Sub SnoozedReminders()
Dim oReminder As Reminder
Dim oReminders As Outlook.Reminders
Dim RemItems As String
Set oReminders = Outlook.Reminders
For Each oReminder In oReminders
If (oReminder.OriginalReminderDate <> oReminder.NextReminderDate) Then
RemItems = RemItems & oReminder.Caption & vbCrLf & _
"Original Reminder time: " & oReminder.OriginalReminderDate & vbCrLf & _
"Snoozed to: " & oReminder.NextReminderDate & vbCrLf _
& vbCrLf
End If
Next oReminder
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Generated on " & Now
oMail.Body = RemItems
oMail.Display
End Sub
The Reminder.Item property returns a corresponding Outlook item. So, you may get the message body from there.
Sub SnoozedReminders()
Dim oReminder As Reminder
Dim oReminders As Outlook.Reminders
Dim RemItems As String
Set oReminders = Outlook.Reminders
For Each oReminder In oReminders
If (oReminder.OriginalReminderDate <> oReminder.NextReminderDate) Then
RemItems = RemItems & oReminder.Caption & vbCrLf & "Original Reminder time: " &
oReminder.OriginalReminderDate & vbCrLf & "Snoozed to: " & oReminder.NextReminderDate & vbCrLf
& vbCrLf
End If
MsgBox oReminder.Item.Body
Next oReminder
...
End Sub
The Outlook object model provides three main ways for working with item bodies:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.
See Chapter 17: Working with Item Bodies for more information.

How to set a variable referring to the library database

Currently I'm working on a library database for all my Access apps which have common code and forms in it. It is going quite well, but the only thing I cannot discover is how to set a variable referring to the reference. The library name is set by a constant and at startup is checked if the file exists in the app folder and if this is equal to the path stored in the reference.
However, I want to refer to the library with a short variable "dbl", because I don't want to use the library name every time. This name might change as well. How do I set the variable to do this? I have tried Set dbl = Application.References(APP_LIB), and this is accepted as valid, but I cannot call any routine or form with this variable.
Thx!
Art.
Public Function CkLib()
'Needs to stay in the App Module!
Dim ref As Reference
Dim strLib As String
On Error GoTo Err_Proc
For Each ref In Application.References
If ref.Name = APP_LIB Then
strLib = CurrentProject.Path & "\" & ref.Name & "." & APP_LIB_TYPE
If Dir(strLib, vbNormal) = "" Then 'File does not exist
MsgBox "The app library " & APP_LIB & " is missing. Please reinstall the app or ask for support!", vbCritical + vbOKOnly, APP_NAME & " App Error"
DoCmd.Quit acQuitSaveNone
Else
If ref.FullPath <> strLib Then 'Path needs to be updated
References.Remove ref
References.AddFromFile strLib
End If
Exit For
End If
End If
Next ref
If Nz(strLib, "") = "" Then
'Try to add ref
If Dir(CurrentProject.Path & "\" & APP_LIB & "." & APP_LIB_TYPE, vbNormal) <> "" Then
References.AddFromFile strLib
Else
MsgBox "Missing reference to app library " & APP_LIB & "! Please reinstall the app or ask for support!", vbCritical + vbOKOnly, APP_NAME & " App Error"
DoCmd.Quit acQuitSaveNone
End If
End If
Set dbl = Application.References(APP_LIB)
Exit_Proc:
Exit Function
Err_Proc:
Select Case Err.number
Case Else
MsgBox "Error: " & Trim(Str(Err.number)) & vbCrLf & _
"Desc: " & Err.description & vbCrLf & vbCrLf & _
"Module: Mod_Generic" & vbCrLf & _
"Function: CkLib", _
vbCritical, "Error!"
End Select
Resume Exit_Proc
End Function

Access VBA save object Report.pdf to a specific path with a unique name

I'm familiar with VBA but I am not a programmer so any help I can get in this matter is greatly appreciated. I have a report object that is mailed as a .pdf file. This portion of the code works fine but I would like to be able to save a copy of this file to a specific location with a unique name that includes the date and time the file was created. The first set of code is the SendObject that works the second set of code does not work, it is a separate procedure I have been testing to save the object. Once I can get it working I was going to integrate it into first. I would appreciate any help.
Private Sub Command21_Click()
DoCmd.SetWarnings (False)
Dim mailto As String
Dim ccto As String
Dim bccto As String
mailto = "Safety-RiskGroup#bargeacbl.com"
ccto = ""
bccto = ""
emailmsg = "The attached document is the updated Case Log." & vbNewLine
& "Please review the report, contact me and you find any discrepancies. "&vbNewLine & vbNewLine & "Thank You, " & vbNewLine & vbNewLine & vbNewLine & "Cary S. WInchester" & vbNewLine & "American Commercial Barge Line" & vbNewLine & "Safety Department"
mailsub = "Updated Case Log Report"
On Error Resume Next
DoCmd.SendObject acSendReport, "rpt_CaseLog-CurrentYear", acFormatPDF, mailto, ccto, bccto, mailsub, emailmsg, True
DoCmd.SetWarnings (True)
End Sub
This is the second set of code to attempt to save the object to a specific path with a unique name.
Private Sub Command23_Click()
On Error GoTo Command23_Click_Err
Dim filePath As String
filePath = "C:\Work\ACBL\Access Dbase\DayCount" & "CaseLog" _
& Format(Date, " yyyy/mm/dd") _
& Format(Time, " hh:MM:ss") & ".pdf"
DoCmd.OutputTo acOutputReport, "rpt_CaseLog-CurrentYear", _
"PDFFormat(*.pdf)", filePath, _
False, "", , acExportQualityPrint
Command23_Click_Exit:
Exit Sub
Command23_Click_Err:
MsgBox Error$
Resume Command23_Click_Exit
End Sub
Thanks Bit Accesser but that was not the problem, the code was laid out as it should be; however, the Date and Time formats were using characters that could be used for a file name, specifically, the colons and the backslashes were causing it to fail. Below is the corrected code. There are a few other spots I tweaked but this works great.
Private Sub Command25_Click()
On Error GoTo Command25_Click_Err
Dim filePath As String
filePath = "C:\Work\ACBL\Access Dbase\DayCount\Reports\"
DoCmd.OutputTo acOutputReport, "rpt_CaseLog-CurrentYear", acFormatPDF, _
filePath & " Case Log Update" & Format(Now(), " dd-mm-yyyy hhmmss") & ".pdf"
Command25_Click_Exit:
Exit Sub
Command25_Click_Err:
MsgBox Error$
Resume Command25_Click_Exit
End Sub

MSXML2.DOMDocument load function fails in VBA

I've been struggling with the below issue for a while now and couldn't find the solution yet.
There is an iShare page with an XML file that I want to download using VBA code, then later process the XML file and save into MS Access database.
I've been using the below code for about 4 years now, it worked perfectly without any issues. But suddenly it stopped working this week.
Any ideas why?
the code:
Private Function GetRequests() As Boolean
On Error GoTo ErrHandler
Dim oDoc As MSXML2.DOMDocument
Dim Url As String
Dim sFileName As String
Set oDoc = New MSXML2.DOMDocument
oDoc.async = False
Url = cUrlDatabase & "/" & cApplicationName & "/In/" & cReqXmlFile
UpdateStatus "Loading " & cReqXmlFile
If Not oDoc.Load(Url) Then
c_sLastError = "Could not load XML " & Url
GoTo EndProc
End If
sFileName = sPath & "\Data\requests.xml"
oDoc.Save sFileName
GetRequests = True
End Function
The code fails at the oDoc.Load(Url) part, it comes back false.
Here's an example of how to gather error details:
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument
If xDoc.Load("C:\My Documents\cds.xml") Then
' The document loaded successfully.
' Now do something intersting.
Else
' The document failed to load.
Dim strErrText As String
Dim xPE As MSXML.IXMLDOMParseError
' Obtain the ParseError object
Set xPE = xDoc.parseError
With xPE
strErrText = "Your XML Document failed to load" & _
"due the following error." & vbCrLf & _
"Error #: " & .errorCode & ": " & xPE.reason & _
"Line #: " & .Line & vbCrLf & _
"Line Position: " & .linepos & vbCrLf & _
"Position In File: " & .filepos & vbCrLf & _
"Source Text: " & .srcText & vbCrLf & _
"Document URL: " & .url
End With
MsgBox strErrText, vbExclamation End If
Set xPE = Nothing
End If
Example taken from here.
For other people finding this post:
The xml parser by now has implemented different error types (see here).
You would have to use the following code
Set objXML = CreateObject("Msxml2.DOMDocument.6.0")
ObjXML.async=true
objXML.load "/path/to/xml"
If objXML.parseError.errorCode <> 0 Then
MsgBox "Error was " + objXML.parseError.reason
End If
This should help you debug your .xml file.
For anyone else struggling with this, I found this error to be caused by text encoded in a format which could not be parsed in VBA (some weird E symbol). The objXML was nothing after the .load. I'm sure there are many possible causes, but I'll share what I found in case this helps someone. Thanks to the guys above for the error handling routines.