home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / CHIPCD_3_98.iso / software / testsoft / exchange / bin / exchange.dsm < prev    next >
Text File  |  1997-08-25  |  10KB  |  360 lines

  1. '------------------------------------------------------------------------------
  2. ' FILE DESCRIPTION: Macros for development of Exchange Server Applications 
  3. ' Copyright (c) Microsoft Corporation 1993-1997. All rights reserved.
  4. '------------------------------------------------------------------------------
  5. Option Explicit 
  6.  
  7. '------------------------------------------------------------------------------
  8. ' Localized strings
  9. '------------------------------------------------------------------------------
  10. ' Put all localizable strings and constants here
  11. Dim ErrFailedToCreateMsg 
  12. Dim MsgOpenTitle
  13. Dim MsgSaveAsTitle
  14.  
  15. ErrFailedToCreateMsg = "Failed to create script message"
  16. MsgOpenTitle = "Exchange Event Script - Open"
  17. MsgSaveAsTitle = "Exchange Event Script - Save As"
  18.  
  19. '------------------------------------------------------------------------------
  20. ' Macros
  21. '------------------------------------------------------------------------------
  22. ' Dictionary is used to map temporary files to script messages
  23. Dim fDictInitialized
  24. Dim dictFilesToScriptMsgs
  25.  
  26. ' MAPI Session variables
  27. Dim amSession
  28. Dim amStore
  29.  
  30. ' Script Locator
  31. Dim aeChooser
  32.  
  33. ' Debugging Aid
  34. Dim fDebug 
  35. '    fDebug = true
  36.     fDebug = false
  37.  
  38. ' DESCRIPTION: Select and open an Exchange Event Service script in the IDE
  39. Sub OpenExchangeEventScript()
  40.     On Error Resume Next
  41.  
  42.     Dim oBinding
  43.     Dim strScriptFileName
  44.  
  45.     InitGlobals
  46.     If Err.Number = 0 Then
  47.         ChooseScript MsgOpenTitle, oBinding, strScriptFileName
  48.         If Err.Number = 0 Then 
  49.             AddFileToMap strScriptFileName, oBinding
  50.             If Err.Number = 0 Then
  51.                 Application.Documents.Open( strScriptFileName )
  52.                 If Err.Number <> 0 Then
  53.                     RemoveFileFromMap strScriptFileName
  54.                     
  55.                     ReportError "Application.Documents.Open"
  56.                 End If
  57.             Else
  58.                 ReportError "AddFileToMap"
  59.             End If
  60.         ElseIf Err.Number <> &H80040113 Then
  61.             ReportError "ChooseScript"
  62.         End If 
  63.     ElseIf Err.Number <> &H80040113 Then
  64.         ReportError "InitGlobals"
  65.     End If
  66. End Sub
  67.  
  68. ' DESCRIPTION: Save an Exchange Event Service script.  
  69. Sub SaveAsExchangeEventScript()
  70.     On Error Resume Next
  71.  
  72.     Dim strOldScriptFileName
  73.     Dim strNewScriptFileName
  74.     Dim oBinding
  75.     Dim amFolder
  76.  
  77.     strOldScriptFileName = ActiveDocument.FullName
  78.     If Err.Number = 0 Then
  79.         InitGlobals
  80.         If Err.Number = 0 Then
  81.             ChooseScript MsgSaveAsTitle, oBinding, strNewScriptFileName
  82.             If Err.Number = 0 Then
  83.                 aeChooser.DeleteTempScriptFile strNewScriptFileName
  84.  
  85.                 ActiveDocument.Save(strNewScriptFileName)
  86.                 If Err.Number = 0 Then
  87.                     If Not strOldScriptFileName Is Empty Then
  88.                         RemoveFileFromMap strOldScriptFileName
  89.                         aeChooser.DeleteTempScriptFile strOldScriptFileName
  90.                     End If
  91.  
  92.                     AddFileToMap strNewScriptFileName, oBinding
  93.                     If Err.Number = 0 Then
  94.                         PutScript strNewScriptFileName
  95.                     Else
  96.                         ReportError "AddFileToMap"
  97.                     End If
  98.                 Else
  99.                     ReportError "Document.Save"
  100.                 End If
  101.             ElseIf Err.Number <> &H80040113 Then
  102.                 ReportError "ChooseScript"
  103.             End If
  104.         ElseIf Err.Number <> &H80040113 Then
  105.             ReportError "InitGlobals"
  106.         End If
  107.     Else
  108.         Err.Description = "No document selected"
  109.         ReportError "SaveAs"
  110.     End If
  111. End Sub
  112.  
  113. '------------------------------------------------------------------------------
  114. ' Events
  115. '------------------------------------------------------------------------------
  116.  
  117. ' DESCRIPTION: Document has been saved to disk.  Now copy that to the Exchange Store    
  118. Sub Application_DocumentSave(theDocument) 
  119.     On Error Resume Next
  120.  
  121.     If FileExistsInMap( theDocument.FullName ) Then
  122.         PutScript( theDocument.FullName )
  123.         If Err.Number <> 0 Then
  124.             ReportError "PutScript"
  125.         End If
  126.     End If
  127. End Sub
  128.  
  129. ' DESCRIPTION: Document is saved and closing. Remove temporary file
  130. Sub Application_BeforeDocumentClose(theDocument)
  131.     On Error Resume Next
  132.         
  133.     If FileExistsInMap( theDocument.FullName ) Then
  134.         RemoveFileFromMap theDocument.FullName 
  135.         aeChooser.DeleteTempScriptFile theDocument.FullName 
  136. ' *** commented out because this is booted til osmium RTM ***
  137. '    ElseIf InStr(theDocument.Name, ".asp", 1) > 1 Then
  138. '        InitChooser
  139. '        If Err.Number = 0 Then
  140. '            aeChooser.SetFileCloseEvent theDocument.FullName
  141. '            If Err.Number <> 0 And Err.Number <> &H80070002 And Err.Number <> &H8007007B Then
  142. '                ReportError "SetFileCloseEvent"
  143. '            End If
  144. '        Else
  145. '            ReportError "InitChooser"
  146. '        End If
  147.     End If
  148. End Sub 
  149.  
  150. ' DESCRIPTION: VS is exiting. Final cleanup
  151. Sub Application_BeforeApplicationShutDown()
  152.     On Error Resume Next
  153.  
  154.     ClearMap
  155.  
  156.     If Not aeChooser Is Nothing Then
  157.         Set aeChooser = Nothing
  158.     End If
  159.  
  160.     If Not amSession Is Nothing Then
  161.         amSession.Logoff
  162.         Set amSession = Nothing
  163.     End If
  164.  
  165.     Err.Clear
  166. End Sub
  167.  
  168. '------------------------------------------------------------------------------
  169. ' Helpers
  170. '------------------------------------------------------------------------------
  171.  
  172. ' DESCRIPTION: Prompt user for an Exchange Event Service script 
  173. Private Sub ChooseScript(strTitle, ByRef oBinding, ByRef strScriptFileName)
  174.     On Error Resume Next
  175.  
  176.     Err.Clear
  177.  
  178.     aeChooser.ChooseScript strTitle
  179.     If Err.Number = 0 Then
  180.         Set oBinding = aeChooser.Binding
  181.         If Err.Number = 0 Then
  182.             strScriptFileName = aeChooser.FileName
  183.             If Err.Number <> 0 Then
  184.                 ReportError "ScriptChooser.FileName"
  185.             End If
  186.         Else
  187.             ReportError "ScriptChooser.Binding"
  188.         End If
  189.     ElseIf Err.Number <> &H80040113 Then
  190.         ReportError "ScriptChooser.ChooseScript"
  191.     End If
  192.  
  193.     Application.Active = true
  194. End Sub
  195.  
  196. ' DESCRIPTION: Save an Exchange Event Service script to the Exchange Store
  197. Private Sub PutScript(ByVal strScriptFileName)
  198.     On Error Resume Next
  199.  
  200.     Dim oBinding
  201.  
  202.     FindScriptMsgInMap strScriptFileName, oBinding
  203.     If Err.Number = 0 Then
  204.         aeChooser.PutScript oBinding, strScriptFileName
  205.     Else
  206.         ReportError "FindScriptMsgInMap"
  207.     End If
  208. End Sub
  209.  
  210. Private Sub ReportError(strWhatFailed)
  211.     Dim strErrMsg
  212.  
  213.     strErrMsg = strWhatFailed & " failed: " & Err.Description & " (" & Hex(Err.Number) & ")"
  214.  
  215.     MsgBox( strErrMsg )
  216. End Sub
  217.  
  218. '------------------------------------------------------------------------------
  219. ' MAPI Functions
  220. '------------------------------------------------------------------------------
  221.  
  222. ' DESCRIPTION:  Initialize MAPI, logon to Exchange Store, and initialize script chooser
  223. Private Sub InitChooser
  224.     On Error Resume Next
  225.  
  226.     If aeChooser Is Nothing Then
  227.         Err.Clear
  228.  
  229.         Set aeChooser = CreateObject("ActiveEx.ScriptChooser")
  230.     End If
  231. End Sub
  232.  
  233. Private Sub InitGlobals
  234.     On Error Resume Next
  235.  
  236.     Dim amTempSession
  237.     
  238.     If amSession Is Nothing Then
  239.         Err.Clear
  240.  
  241.         Set amTempSession = CreateObject("MAPI.Session")
  242.         If Err.Number = 0 Then
  243.             ' Logon with showDialog:=true, newSession:=false, NoMail:=true
  244.             amTempSession.Logon "", "", true, true, -1, true
  245.             If Err.Number = 0 Then
  246.                 InitChooser
  247.                 If Err.Number = 0 Then
  248.                     aeChooser.Session = amTempSession
  249.                     If Err.Number = 0 Then
  250.                         aeChooser.Root = amTempSession
  251.                         If Err.Number = 0 Then
  252.                             Set amSession = amTempSession
  253.                         Else
  254.                             ReportError "ScriptChooser.Root"
  255.                         End If
  256.                     Else
  257.                         ReportError "ScriptChooser.Session"
  258.                     End If
  259.                 Else
  260.                     ReportError "InitChooser"
  261.                 End If
  262.             ElseIf Err.Number <> &H80040113 Then
  263.                 ReportError "Session.Logon"
  264.             End If
  265.  
  266.             Set amTempSession = Nothing
  267.         Else
  268.             ReportError "CreateObject"
  269.         End If
  270.     End If
  271. End Sub
  272.  
  273. '------------------------------------------------------------------------------
  274. ' Map of Files to Script Messages
  275. '------------------------------------------------------------------------------
  276.  
  277. ' DESCRIPTION: Adds a File/ScriptMsg pair to the dictionary
  278. Private Sub AddFileToMap( ByVal keyFile, item)
  279.     On Error Resume Next
  280.  
  281.     If Not fDictInitialized Then
  282.         Set dictFilesToScriptMsgs = CreateObject("Scripting.Dictionary")
  283.         If Err.Number = 0 Then
  284.             fDictInitialized = true
  285.         Else
  286.             ReportError "CreateObject"
  287.             Exit Sub
  288.         End If
  289.     End If
  290.  
  291.     dictFilesToScriptMsgs.CompareMode = vbTextCompare
  292.  
  293.     dictFilesToScriptMsgs.Add keyFile, item
  294.     If Err.Number <> 0 Then
  295.         ReportError "Dictionary.Add"
  296.     End If
  297. End Sub
  298.  
  299. 'DESCRIPTION: Removes a File/ScriptMsg pair from dictionary
  300. Private Sub RemoveFileFromMap( ByVal keyFile )
  301.     On Error Resume Next
  302.  
  303.     If Not fDictInitialized Then
  304.         Exit Sub
  305.     End If
  306.  
  307.     dictFilesToScriptMsgs.Remove(keyFile)
  308.     If Err.Number <> 0 And Err.Number <> &H802b Then
  309.         ReportError "Dictionary.Remove"
  310.     End If
  311. End Sub
  312.  
  313. ' DESCRIPTION: Checks if the File exists in the dictionary
  314. Private Function FileExistsInMap( ByVal keyFile )
  315.     On Error Resume Next
  316.  
  317.     If Not fDictInitialized Then
  318.         FileExistsInMap = false
  319.         Exit Function
  320.     End If
  321.  
  322.     FileExistsInMap = dictFilesToScriptMsgs.Exists(keyFile)
  323.     If Err.Number <> 0 Then
  324.         ReportError "Dictionary.Exists"
  325.     End If
  326. End Function
  327.  
  328. ' DESCRIPTION: Finds the ScriptMsg corresponding to the File in the dictionary
  329. Private Sub FindScriptMsgInMap(ByVal keyFile, ByRef item)
  330.     On Error Resume Next
  331.  
  332.     If Not fDictInitialized Then
  333.         Exit Sub
  334.     End If
  335.  
  336.     Set item = dictFilesToScriptMsgs.Item(keyFile)
  337.     If Err.Number <> 0 Then
  338.         ReportError "Dictionary.Item"
  339.     End If
  340. End Sub
  341.  
  342. ' DESCRIPTION: Cleans the map up
  343. Private Sub ClearMap()
  344.     On Error Resume Next
  345.  
  346.     If Not fDictInitialized Then
  347.         Exit Sub
  348.     End If
  349.  
  350.     fDictInitialized = false
  351.  
  352.     dictFilesToScriptMsgs.RemoveAll
  353.     If Err.Number <> 0 Then
  354.         ReportError "Dictionary.RemoveAll"
  355.     End If
  356.  
  357.     Set dictFilesToScriptMsgs = Nothing
  358. End Sub
  359.