PDA

View Full Version : VBA Code - I cannot compute



AtTheDMZ
05-20-2022, 02:01 PM
Hey earth people,

I have a problem understanding my own code. I left it at a stop only to investigate more before coding more. Its weird sometimes when you can create something but you're confused how you did it. I haven't programmed in VBA for a little over a year. All knowledge is asleep. I'm hoping someone might be able to compile the code in english to tell me what its doing, and how it's doing it. Mainly my focus is on, a variable declared as "oFolder" and the my For Each loop, where it prints "10XBilling", what I'm confused is how is it that I only declared oFolder, but it can see this calendar; it somehow is strangly entangled with set command, "Set item = Outlook.ActiveExplorer.CurrentFolder"

Here's the code. Hopefully I explained myself. I'm not exactly all there. I need energy juice after writing this.



Sub Whatever2()
Dim Outlook As Object 'Outlook.Application
Dim oNS As Object 'Outlook.Namespace
Dim oFolder As Object 'Outlook.folder
Dim item As Object
Dim objOwner As Outlook.Recipient
' ---- set Outlook Instance
On Error Resume Next
Set Outlook = GetObject(, "Outlook.Application") 'Bind an existing instance of Outlook
If Err.Number <> 0 Then 'Could not get instance, so create a new one
Err.Clear
Set Outlook = CreateObject("Outlook.Application")
End If
On Error GoTo Error_Handler
Set oNS = Outlook.GetNamespace("MAPI")
'use the default folder
Set item = Outlook.ActiveExplorer.CurrentFolder
'print test
Debug.Print "Hello 1"
'do whatever'
On Error Resume Next
For Each item In oFolder.items
Debug.Print item.GetFirst
Next item
On Error GoTo Error_Handler
Error_Handler_Exit:
On Error Resume Next
If Not oPrp Is Nothing Then Set oPrp = Nothing
If Not oItem Is Nothing Then Set oItem = Nothing
If Not oFolder Is Nothing Then Set oFolder = Nothing
If Not oNameSpace Is Nothing Then Set oNameSpace = Nothing
If Not oOutlook Is Nothing Then Set oOutlook = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Outlook_ExtractAppointments" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub

arnelgp
05-21-2022, 07:02 AM
did you really build the code or just frankenstein it somewhere?