0

I want to change the time zone of all items in an Outlook 2010 calendar.

I am confused as to how one would work with the items of a collection as they are iterated in the loop. My main background is in Java, and as I understand loops there a single variable is used as a dummy variable that will take the value of all items in the collection, in turn. No special assignment is usually required for such FOR loops. Do you need to manually advance the variable in some way so as to keep the loop going?

Here is my code:

Public Sub TZFix()

    Dim oAppointmentItem As Outlook.AppointmentItem
    Dim tzs As Outlook.TimeZones
    Dim tzCentral As Outlook.TimeZone
    Dim oAppointments As Object
    Dim oNS As Outlook.NameSpace

    Set oNS = oOutlook.GetNamespace("MAPI")
    Set oAppointments = oNS.GetDefaultFolder(olFolderCalendar)
    Set tzs = Application.TimeZones
    Set tzCentral = tzs("Central Standard Time")

    For Each oAppointmentItem In oAppointments.Items
        Set oAppointmentItem.StartTimeZone = tzCentral
        Set oAppointmentItem.EndTimeZone = tzCentral
    Next

End Sub

I believe that there is an issue with variable assignment within the loop, as I get an Error 91: Object Variable or With block variable not set error whenever I run it.

Community
  • 1
  • 1
  • Delete the line `Dim oOutlook As Outlook.Application` and change the line `Set oNS = oOutlook.GetNamespace("MAPI")` to `Set oNS = Outlook.GetNamespace("MAPI")` and it will work perfectly. – Siddharth Rout Jun 29 '12 at 22:20
  • @SiddharthRout: I just tried your solution, but unfortunately I still receive the same error. I am continuing my research into the issue, please let me know if you happen to find something else. Thanks! – flyingscotsman74656 Jul 02 '12 at 19:49

3 Answers3

0

oOutlook is never assigned to and is therefore Nothing. You probably meant to set it to Application.

Also, setting local variables to Nothing in the end is redundant, remove that.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Ah, I had read somewhere that it was necessary to free up the variables and aid garbage collection. I thought it odd that such a high-level language would bother with such things, but didn't think too much of it. – flyingscotsman74656 Jun 29 '12 at 18:44
  • Just ran it again, get a "Run-time error '440' Property is read-only" error. Is this because Outlook is locking down the appointment's time zone property? Is there a way to gain write access to that? All of my data is stored in an .ost file. – flyingscotsman74656 Jun 29 '12 at 18:45
  • @flyingscotsman74656 I don't have outlook installed. Please use the debugger. `F8` steps into, `Shift+F8` steps over. `F9` sets a breakpoint. See http://stackoverflow.com/a/336784/11683 for how error reporting is configured. `F2` brings up the object browser, from which you can see if a property is read only. `Shift+F2` brings up the object browser and jumps to the property/method the caret is currently in in your code module. – GSerg Jun 29 '12 at 19:15
  • Thanks for the tips, those are proving quite useful. In the object browser, it would seem that StartTimeZone and EndTimeZone are not read-only, I will fully step through the program and see what is truly at fault. Incidentally, is it better to do this in Visual Studio 2010? I have that program, and I am curious if it would be a better and more full-featured program to use. – flyingscotsman74656 Jun 29 '12 at 19:51
  • It is indeed the Set statements for both TimeZone properties that are failing, get the same 440 error every time. I will check if I have access to my OST file, hopefully that is not an issue (especially with my macro security settings turned off for the time being). – flyingscotsman74656 Jun 29 '12 at 20:09
  • @flyingscotsman74656 But you don't have `Set` statements for `TimeZone` properties. You have assignment statements which are used to assign values, not references to objects. You are using `Set` statements above the loop so I thought you knew what you were doing, but because `TimeZone` properties represent an object, you must use `Set` to assign it. Otherwise you are trying to assign a value to the default property of the `TimeZone` object returned by the `StartTimeZone`/`EndTimeZone`, which is read-only. – GSerg Jun 29 '12 at 20:47
  • Thanks for finding that, I have added the appropriate Set statements and I still get the 440 error. Any other ideas? – flyingscotsman74656 Jul 02 '12 at 14:03
0

I also had this problem in my Script. For me the solution was setting the Macro-Security-Settings to the lowest and ran it again and it worked. Maybe it's worth giving it a try!

Benni
  • 1
  • 1
0

I made the code working with few changes of your code. This is worked:

Public Sub TZ_change_to_Hawaii()
''''Changing  the selected appointments' time zones to Hawaii

Dim tzs As Outlook.TimeZones
Dim tzCentral As Outlook.TimeZone

Set tzs = Application.TimeZones
Set tzCentral = tzs("Hawaiian Standard Time")

Dim objOL As Outlook.Application
Dim objSelection As Outlook.Selection
Dim objItem As Object

Set objOL = Outlook.Application
Set objSelection = objOL.ActiveExplorer.Selection

For Each objItem In objSelection
    Set objItem.StartTimeZone = tzCentral
    Set objItem.EndTimeZone = tzCentral
    objItem.Save
Next
End Sub
Haider
  • 179
  • 3
  • 8