Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple question from barely novice vba programmer - sensitivity field 1

Status
Not open for further replies.

finny388

Programmer
Jul 16, 2001
19
CA
I need to write a routine for Outlook XP (2002) that will loop through all appointments and change the Sensitivity field values from normal to private. In fact all to private.
I have been searching the web for the past few days and coming up short. There must be a simple way to do this. Outlook itself may have a way but I can't find it if it does.

Any help will be very much appreciated.
Thanks.
 
This will give you a start. Note that this will change ALL appointments to Private.

Sub MakePrivate()
Dim olApp As outlook.Application
Dim objNameSpace As NameSpace
Dim objCalendar As MAPIFolder
Dim i As Integer
Dim var
Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar)
i = 1
On Error Resume Next
For var = 1 To objCalendar.Items.Count
objCalendar.Items.Item(i).Sensitivity = olPrivate
i = i + 1
Next
Set objCalendar = Nothing
Set objNameSpace = Nothing
Set olApp = Nothing
End Sub

Gerry
 
I will try that tonight.
Thanks Gerry!
Get me started? It looks like it will change all to private. That's really all I need to do.
I'll post with results. Thanks.
 
Hi Gerry,
I went into Outlook. Alt-F11 opened the macro window and this is what happened:

Window title bar: Microsoft Visual Basic - VbaProject.OTM -[ThisOutlookSession (Code)]

And in the Project folder tree view on the left:
- Project1(VbaProject.OTM)
- Microsoft Outlook Objects
ThisOutlookSession

I double clicked ThisOutlookSession on the left which opened the code window to the right.
I pasted the code in.
The code window had (General) in the first dropdown and MakePrivate in the second.
I ran Macro (F5).

It looped through without a bug and took about 20 sec so it seemed to loop through them all. Anxiously I went to Calendar view to see if it worked. It didn't.

What do I have to do to get this code to apply to my .pst?

Thanks.
 
The I value in the end is exactly 1 greater than all the appointments in my calendar folder. So it is looping through the right folder.
 
I found this code elsewhere on the net. Didn't work either but didn't blow up either. I tried adding:
objCalendar.Items.Item(i).Save
within the loop but it didn't make a difference. Is there a commit or save setting or command I'm missing?

Thanks again.


This was that code I found:
' On Error Resume Next
' Set objOL = CreateObject("Outlook.Application")
' Set objNS = objOL.GetNamespace("MAPI")
' Set colCal = objNS.GetDefaultFolder(olFolderCalendar) ' literal 9
' For Each objAppt In colCal
' objAppt.Sensitivity = olPrivate
' objAppt.Save
' Next
 
Can anyone else explain what I'm missing?
Thanks.
 
Did you try running fumei's code through a module in Access?"

No, are you suggesting that I do? I'll try it. Why would I have to do it from Access? Hmm. I'll try:

I get a Compile Error at the first line (olApp As outlook.Application):

User-defined type not defined.

I looked in the Object Browser and couln't find anything to do with outlook. Access (library) has a class called Application but I need the outlook one I guess.

Can you expand on this?

Thanks so much.
 
You have to reference the Outlook Object Model:
Menu Utils --> References ...
Click the CheckBox on Microsoft Outlook x.0 object library.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thanks for the post PH,
I checked off the MS Outlook 10.0 object library.
The code then worked like it did in Outlook without a hitch. But it still doesn't commit those changes. It runs through them all, but they remain unchanged.

So you guys don't see any missing "save changes"/"commit changes" code here? I'm just grasping at straws now.
 
Hi guys,

Try this then. The only difference is it creates a specific instance of each object in the Calendar. So if you got LOTS, this will slow down. I put in a check for recurring items and this ignores them. I am sure there is a way to reset recurring items to private, but I am kinda busy right now. You can do that manually though by changing the initialing series item. I am interested to find out if this works for you finny388. It works on mine. It changes every one to private.

Sub MakePrivate()
Dim olApp As outlook.Application
Dim objNameSpace As NameSpace
Dim objCalendar As MAPIFolder
Dim objItem As AppointmentItem
Dim i As Integer
Dim var
Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objCalendar = objNameSpace.GetDefaultFolder(olFolderCalendar)
i = 1
On Error Resume Next
For var = 1 To objCalendar.Items.Count
Set objItem = objCalendar.Items.Item(i)
If objItem.IsRecurring = False Then
objItem.Sensitivity = olPrivate
objItem.Save
Set objItem = Nothing
Else
Set objItem = Nothing
End If
i = i + 1
Next
Set objCalendar = Nothing
Set objNameSpace = Nothing
Set olApp = Nothing
End Sub

Gerry
 
Thanks Gerry,
I ran it from within Outlook and it worked like a charm! I'm not clear on why you've coded it to ignore recurring items though. Wouldn't removing that condition just change all?
But big thanks to you, I can get to those recurr'ers later.

I hope I am in a position to help in the future,
regards,
Finny.
 
Yes changing the originating item would (I think) change the series. However, changing specific instance of the series does not change the parent. Inheritence is....well inherited, not sent back up. makes sense?

As recurring items could be (but then again could not be) required to be private, in my need for brevity I left out some error logic to deal eith it. You could remove the thing about recurrences...just remember that this would still open EACH on to check it is is private or not. But if you open just the originating item, changing it changes all the others.

So yes, it would be easy to detext if it is the original or not, pop up a question asking if you want it private, if yes make it private. Then get the macro to ignore all other instances of the recurrence.

Whatever, this is certainly something you an play arouns with. Good luck!



Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top