Outlook Rule will not run script unless manually ran - vbscript

Whenever I add my script to a rule i have setup in outlook it sets my rule to client-side only. The rule is used to get a specific word in the subject line and a specific word in the body then move the email to a subfolder of the Inbox then run a script. The current rule runs when I receive the email by moving the email to the directed folder but the script does not run unless I manually click the rule to run now. How could I make it to where it would be processed on server side only so I won't have to manually run the rule to run the script. Here is my script below:
Public Sub Application_NewMail(myMail As MailItem)
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim dbName As String
dbName = "M:\CRM\Custom CRM\CRM.accdb"
Set con = New ADODB.Connection
con.ConnectionString = _
"Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source = " & dbName & "; " & _
"Persist Security Info = False; " & _
"Mode = readwrite;"
con.Open
' Create 2 recordset objects for data manipulation throughout the project
Set rs = New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.ActiveConnection = con
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
End With
Dim ns As Outlook.NameSpace
Dim InBoxFolder As MAPIFolder
Dim InBoxItem As Object 'MailItem
Dim Contents As String, Delimiter As String
Dim Prop, Result
Dim i As Long, j As Long, k As Long
Dim myOlApp As Object
Set myOlApp = CreateObject("Outlook.Application")
'Setup an array with all properties that can be found in the mail
Prop = Array("Name", "Email", "Phone", "I am an")
'The delimiter after the property
Delimiter = ":"
Set ns = Session.Application.GetNamespace("MAPI")
'Access the inbox folder
Set InBoxFolder = ns.GetDefaultFolder(olFolderInbox)
Set InBoxFolder = InBoxFolder.Folders("MBAA LEADS")
For Each InBoxItem In InBoxFolder.Items
'Only process mails
If Not TypeOf InBoxItem Is MailItem Then GoTo SkipItem
'Already processed?
If Not InBoxItem.UnRead Then GoTo SkipItem
'Mark as read
InBoxItem.UnRead = False
'Get the body
Contents = InBoxItem.Body
'Create space for the result
ReDim Result(LBound(Prop) To UBound(Prop)) As String
'Search each property
i = 1
rs.Open ("Prospects")
rs.AddNew
For k = LBound(Prop) To UBound(Prop)
'MsgBox k
'Find the property (after the last position)
i = InStr(i, Contents, Prop(k), vbTextCompare)
If i = 0 Then GoTo NextProp
'Find the delimiter after the property
i = InStr(i, Contents, Delimiter)
If i = 0 Then GoTo NextProp
'Find the end of this line
j = InStr(i, Contents, vbCr)
If j = 0 Then GoTo NextProp
'Store the related part
Result(k) = Trim$(Mid$(Contents, i + Len(Delimiter), j - i - Len(Delimiter)))
If (k = 0) Then
'First Name
rs![First Name] = StrConv(Trim(Mid(CStr(Result(k)), 1, InStr(CStr(Result(k)), " "))), vbProperCase)
'Last Name
rs![Last Name] = StrConv(Trim(Mid(CStr(Result(k)), InStrRev(CStr(Result(k)), " ") + 1)), vbProperCase)
MkDir ("M:\CRM\PROSPECTS\" & StrConv(Trim(Mid(CStr(Result(k)), 1, InStr(CStr(Result(k)), " "))), vbProperCase) & " " & StrConv(Trim(Mid(CStr(Result(k)), InStrRev(CStr(Result(k)), " ") + 1)), vbProperCase) & "")
'Copy Initial Email Inquiry
InBoxItem.SaveAs "M:\CRM\PROSPECTS\" & StrConv(Trim(Mid(CStr(Result(k)), 1, InStr(CStr(Result(k)), " "))), vbProperCase) & " " & StrConv(Trim(Mid(CStr(Result(k)), InStrRev(CStr(Result(k)), " ") + 1)), vbProperCase) & "\Initial Email-MBAA WEBSITE.msg"
ElseIf (k = 1) Then
rs![E-mail Address] = Trim(Mid(CStr(Result(k)), 1, InStr(CStr(Result(k)), " ")))
ElseIf (k = 2) Then
rs![Home Phone] = Result(k)
ElseIf (k = 3) Then
'Check customer type
If CStr(Result(k)) Like "*Self Insured Group*" Then
rs![Lead Type] = 1 'Self Insured Group
ElseIf CStr(Result(k)) Like "*Insurance Company*" Then
rs![Lead Type] = 2 'Insurance Company
ElseIf CStr(Result(k)) Like "*Individual Patient*" Then
rs![Lead Type] = 3 'Consumer
ElseIf CStr(Result(k)) Like "*Attorney*" Then
rs![Lead Type] = 4 'Attorney
ElseIf CStr(Result(k)) Like "*Government*" Then
rs![Lead Type] = 5 'Attorney
ElseIf CStr(Result(k)) Like "*Physician*" Then
rs![Lead Type] = 6 'Physician
ElseIf CStr(Result(k)) Like "*International Company*" Then
rs![Lead Type] = 7 'International Company
ElseIf CStr(Result(k)) Like "*Broker*" Then
rs![Lead Type] = 8 'Broker
ElseIf CStr(Result(k)) Like "*Association/Organization*" Then
rs![Lead Type] = 19 'Association/Organization
ElseIf CStr(Result(k)) Like "*Other*" Then
rs![Lead Type] = 9 'Other
End If
End If
NextProp:
Next
rs![CreatedOn] = InBoxItem.SentOn
rs![Source] = 13 'MBAA WEBSITE
rs.Update
rs.Close
SkipItem:
Next
con.Close
End Sub

I'll assume that your mailbox is on an Exchange server or Office365 (which is also Exchange).
Server side rules only work for a limited set of actions. Mostly those actions that are simple, like moving items, replying, etc.
Anything more complicated than that becomes a client-only rule. In the case of a rule that runs a script, those will always be client-only rules because the script is actually part of, and executed by Outlook, not the mail server. So, even though the rule is stored in your mailbox, the execution is such that it requires Outlook to work some parts of the action.
You'll see when you finish making a rule, on the last page of the rule wizard, it will indicate whether it is a client-only rule or not.
The only option for a server side rule using a script, or some code as at the server level as either a Transport Rule, or a Transport Agent.
I would suggest that you break up the action in to 2 parts, one that will be a server-side rule and will run with or without Outlook, then a rule that you can run "on-demand" to do the more complicated bits. It's not fully automated, but at least you can get the items moved to some temporary folder and out of the way.

Related

How can I show a space between the first and last name in my email attachment file name?

My Excel database generates a word document (when a button is clicked) from data the user enters into the database and opens an outlook email with the document attached automatically. When the email pops up with the attachment, the attachment name always has %20 in between the first and last name. I know this is because there is a blank space between the first and last name. Is there a way to remove the %20 and keep the blank space?
My Code
Option Explicit
Sub CreateWordDocuments()
'CREATE A WORD DOCUMENT TO TRANSFER INFORMATION FROM FILTERED
DATA
INTO A WORD TEMPLATE
Dim VSCRow, VSCCol, LastRow, TemplRow, MonthNumber, FromMonth,
ToMonth, DaysOfMonth, FromDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet5
If .Range("B3").Value = Empty Then
MsgBox "Please select the correct template from the drop down
list"
.Range("F4").Select
Exit Sub
End If
TemplRow = .Range("B3").Value ' Set the Template Value
TemplName = .Range("F4").Value ' Set Template Name
MonthNumber = .Range("V4").Value 'Set the Month Number
FromMonth = .Range("W4").Value
ToMonth = .Range("Y4").Value
DaysOfMonth = .Range("AA4").Value
FromDays = .Range("AC4").Value
ToDays = .Range("AF4").Value
DocLoc = Sheet10.Range("F" & TemplRow).Value ' Word Document
Filename
'Open Word Template
On Error Resume Next 'If Word is already open
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
' Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("E99999").End(xlUp).Row 'Determine the last Row
For VSCRow = 8 To LastRow
MonthNumber = .Range("X" & VSCRow).Value
DaysOfMonth = .Range("AF" & VSCRow).Value
If TemplName <> .Range("Z" & VSCRow).Value And MonthNumber >=
FromMonth And MonthNumber <= ToMonth And DaysOfMonth >= FromDays
And DaysOfMonth <= ToDays Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc,
ReadOnly:=False) ' Open Template
For VSCCol = 5 To 42 'Move through the colunms for
information
TagName = .Cells(7, VSCCol).Value 'Tag Name
TagValue = .Cells(VSCRow, VSCCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Forward:True,
Wrap:=wdFindContinue
End With
Next VSCCol
If .Range("H4").Value = "PDF" Then
FileName = ThisWorkbook.Path & "\" & .Range("E" &
VSCRow).Value & ".pdf" ' Create full filename and path with
current
workbook
WordDoc.ExportAsFixedFormat OutputFileName:=FileName,
ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else:
FileName = ThisWorkbook.Path & "\" & .Range("E" &
VSCRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
.Range("Z" & VSCRow).Value = TemplName 'Template Name to use
.Range("AA" & VSCRow).Value = Now
If .Range("S4").Value = "Email" Then
Set OutApp = CreateObject("Outlook.Application") 'Create Outlook
Application
Set OutMail = OutApp.CreateItem(0) 'Create The Email
With OutMail
.To = Sheet5.Range("Y" & VSCRow).Value
.Subject = "Performance Metrics Verification, " &
Sheet5.Range("R"
& VSCRow).Value & " - " & Sheet5.Range("S" & VSCRow).Value & ", "
& Sheet5.Range("T" & VSCRow).Value.Body = "Good afternoon, " &
Sheet5.Range("E" & VSCRow).Value & ",here are your " &
Sheet5.Range("R" & VSCRow).Value & " - " &
Sheet5.Range("S" & VSCRow).Value & ", " & Sheet5.Range("T" &
VSCRow).Value & " performance metrics as captured by the WFW
database systems. Please review and sign. Comments may be
included
in the email body. Please return to me by COB " &
Sheet5.Range("AG"
& VSCRow).Value & ", If this date falls on a holiday, return on
the
next business day following the holiday."
.Attachments.Add FileName
.Display 'To send without displaying .Display to .Send
End With
Else
WordDoc.PrintOut
WordDoc.Close
End If
Kill (FileName) 'Deletes the PDF or Word that was just
created
End If '3 conditions are met
Next VSCRow
WordApp.Quit
End With
End Sub
Attachment with %20 between name

Out of Memory error when trying to connect to oracle database (VB6)

when I m trying to set Recordset using oracle connection string, I m getting OUt of memory error.
on line "rsLIS.Open sql, gConnLIS, adOpenStatic, adLockReadOnly"
However some time it works like once in 5-6 attempts
but when it works it gives error on some other line
on line "If rsLink.Fields(2).value = rsLIS.Fields(1).value Then"
here are the things which I tried :
instead of directly using recordset, I tried to create array (GetRows) method.
Even though recordset count is 26 but UBound of array is showing 1
I have trying changing 3rd argument value from static to forward only
in line ""rsLIS.Open sql, gConnLIS, adOpenStatic, adLockReadOnly""
it also didn't work, it was showing recordset.count as 0
Did try after restarting the client system still same
I m getting this error on client side and since at my place I don't have development environment to debug
Error is "OUT OF MEMORY"
Public Function GetResults_New(MachName As String, patid As String, bCheckDate As Boolean, SendAssay As Boolean) As ADODB.Recordset
On Error GoTo errdesc
Call ShowTempMsg("Line 1")
Dim bXVar As Boolean
Dim i, j As Integer
Dim tmplis, tmporder
Dim tmpresult
bXVar = False
Dim rec_result As New ADODB.Recordset
Dim rsLink As New ADODB.Recordset
Dim rsLIS As New ADODB.Recordset
Dim xSampleIdType As String
gAppPath = AddEditINIfile("VAHSIF.INI", "IF", "sLinkPath", "")
xSampleIdType = AddEditINIfile(gAppPath & "\sLinkConfig.ini", MachName, "SampleIdType", "SampleId1", False)
Call Open_Connection
Call Open_Connection_LIS
rec_result.CursorLocation = adUseClient
If SendAssay = True Then
rec_result.Fields.Append "machineparamid", adBSTR, 50
rec_result.Fields.Append "Assayno", adBSTR, 50
rec_result.Fields.Append "SType", adBSTR, 50
rec_result.Fields.Append "Dilution", adBSTR, 50
Else
rec_result.Fields.Append "machineparamid", adBSTR, 50
rec_result.Fields.Append "SType", adBSTR, 50
rec_result.Fields.Append "Dilution", adBSTR, 50
End If
rec_result.Open
\
'Link Query For Mapped Params.
sql = "SELECT EquipParamMapping.EquipId, EquipParamMapping.EquipParamCode, EquipParamMapping.LISParamCode, EquipParamMapping.EquipAssayNo from EquipParam, EquipParamMapping where equipParam.equipid = equipparammapping.equipid and equipparam.equipparamcode = equipparammapping.equipparamcode and EquipParam.EquipID = '" & MachName & "' and EquipParam.isProgram = 'Y'"
**rsLink.Open sql, gConn, adOpenStatic, adLockReadOnly**
If enumConnTo = connOracle Then
sql = "select " & xSampleIdType & " , LIS_Param_Code From SL_21CI_View_sampleid_Orders where " & xSampleIdType & " || SuffixCode = '" & patid & "' and isApplicable <> 'N' "
Else
sql = "select " & xSampleIdType & " , LIS_Param_Code From SL_21CI_View_sampleid_Orders where " & xSampleIdType & " + cast(SuffixCode as varchar(20)) = '" & patid & "' and isApplicable <> 'N' "
End If
rsLIS.Open sql, gConnLIS, adOpenStatic, adLockReadOnly
While Not rsLIS.EOF
If bXVar = True Then
rsLink.MoveFirst
bXVar = False
End If
While Not rsLink.EOF
bXVar = True
**If rsLink.Fields(2).value = rsLIS.Fields(1).value Then**
If SendAssay = True Then
rec_result.AddNew
rec_result("machineparamid") = rsLink.Fields("EquipParamCode")
rec_result("Assayno") = rsLink.Fields("EquipAssayNo")
rec_result("SType") = " "
rec_result("Dilution") = "0"
rec_result.Update
rec_result.MoveFirst
Else
rec_result.AddNew
rec_result("machineparamid") = rsLink.Fields("EquipParamCode")
rec_result("SType") = " "
rec_result("Dilution") = "0"
rec_result.Update
rec_result.MoveFirst
End If
GoTo NextParam
End If
rsLink.MoveNext
Wend
NextParam:
rsLIS.MoveNext
Wend
Set GetResults_New = rec_result
Exit Function
errdesc:
Call InsertIntoLogWithFileName("Transaction.GetResults_New" & vbNewLine & sql & vbNewLine & err.Description & "ErrLine : " & ErrLine)
End Function
Thanks
That still leaves the question on which line the error occurs. Also: "it also didn't work, it was showing recordset.count as 0". The RecordSet.Count property depends on the provider. Use a function similar to this instead:
Public Function RecordCount(ByVal cn As ADODB.Connection, ByVal sTable As String) As Long
Dim sSQL As String, lRetVal as Long
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
sSQL = "SELECT COUNT(1) AS RecCount FROM " & sTable & ";"
Call rs.Open(sSQL, cn)
If Not (rs.BOF And rs.EOF) Then
lRetVal = rs.Fields("RecCount").Value
Else
lRetVal = -1
End If
Call rs.Close
Set rs = Nothing
RecordCount = lRetVal
End Function
The .Count property might also very well be the cause of the Out of memory error, as I seem to remember that for determing the number of records, it loads all records (from the server) to count them. But I might be wrong there.

Recordset Operation is Not allowed when object is closed VBS

In the code bellow I get an error on the line reading recset.Close.
Char: 5
Error: Operation is not allowed when the object is closed.
Code: 800A0E78
Source: ADODB.Recordset
If the program reaches the line PQ_ID_number = InputBox("Enter PQ Database ID number, Do not use / ? < > \ : * | !", "PQ ID Number", "Replace Text") it seems to work fine (taking manual input) but when it tries to get the ID from the URL of a browser (automaticaly) it gives the error.
valid_name = 0
Dim objInstances, objIE, counterTab
Do While valid_name = 0 'Flag to exit the loop if the Id number has content in the SQL Database'
'-----------------------------------------------------------------------------------------'
Set objInstances = CreateObject("Shell.Application").windows
If objInstances.Count > 0 Then '/// make sure we have IE instances open.
'Loop through each tab.'
For Each objIE In objInstances
'Split the url of current tab using /'
splitURL = Split(objIE.LocationURL,"/")
'Count how many sub strings are in the URL when split'
counter = UBound(splitURL)
if counter = 7 Then
lastSplit = Split(splitURL(7),".")
lastURL = splitURL(0) & "//" & splitURL(2) & "/" & splitURL(3) & "/" & splitURL(4) & "/" & splitURL(5) & "/" & splitURL(6) & "/" & lastSplit(0)
if lastURL = "URL" Then
Set IE = objIE
counterTab = counterTab + 1
end if
end if
'End if
Next
Else
'If no internet explorer window is open.'
MsgBox "No Internet Explorer window found."
wscript.quit
End if
'Check if no [] is open in internet explorer'
if IsObject(IE) Then
url = Split(IE.LocationURL,"=")
url2 = Split(url(1),"&")
PQ_ID_number = url2(0)
else
MsgBox "No incident found."
wscript.quit
end if
'counterTab counts how many [] are open. If there is more than 1, ask for confirmation of last [] number.'
if counterTab > 1 Then
confirm = msgbox("Please confirm Incident ID: " & incidentID,vbYesNo,"Confirmation")
'If no is pressed, ask for manual entry.'
if confirm = vbNo Then
PQ_ID_number = InputBox("Enter PQ Database ID number, Do not use / ? < > \ : * | !", "PQ ID Number", "Replace Text")
On Error Resume Next
If PQ_ID_number = False Then
wscript.quit
End If
end if
end if
'-----------------------------------------------------------------------------------------'
'Open connection in Database'
dbConnectStr = "connection string"
Set con = CreateObject("ADODB.Connection")
Set recset = CreateObject("ADODB.Recordset")
con.Open dbConnectStr
'Get PQ Database title and status of incident number provided.
SQL_String = "Select title, status_id from incident_TBL where incident_ID = " & PQ_ID_number
recset.Open SQL_String, con
title = recset.Fields(0).Value
incidentStatus = recset.Fields(1).Value
con.Close
recset.Close
If title = False Then 'check if PQ_ID given has content in SQL Database
wscript.echo "Invalid PQ Database ID number, please type correct number"
valid_name = 0
Else
valid_name = 1
End If
Loop
Thanks for the help!
you need close Recordset first and only after that close connection
con.Close
recset.Close
change to:
recset.Close
con.Close

Auto generate outlook signature by GPO using multiple signatures

A while back i have searched for a flexible way to automatically generate a signature for different types of users. Our domain has multiple companies and each have different needs. The default scripts you find when googeling simply shows how to create a basic signature.
The script below takes is a few steps further. It fills in the username & initials when first starting up Microsoft Office. If no outlook profile is present it will set up outlook using a PRF file (you can use cached for laptops/tablets and non cached for desktops / servers). It then checks which signatures a user should get and builds them using the information in the signature file, template file and user information from active directory. All status information gets written to the application event log (filter WSH). When a signature file gets updated, the signature will be re-applied.
I am posting the entire script here hoping that someone else might find it usefull. Feel free to comment (or donate ofcourse :-)). It works for Outlook 2000, 2003, 2010 and 2013 (and should be pretty future proof).
Create a new Global Securitygroup in your domain. In my example i will use Signature-Marketing and Signature-HR.
Create a new GPO that applies on these groups. Place the Outlook.vbs script in User \ Policy \ Windows \ Scripts \ Logon.
'\\MyDomain.local\SysVol\WGIT.local\Policies\{MyPolicyID}\User\Scripts\Logon\Outlook.vbs
On Error Resume Next
' ##### CHANGE THESE SETTINGS #####
setup_GroupPrefix = "Signature-"
setup_Path_SignatureVBS = "\\MyDomain.local\NETLOGON\Outlook\Signatures" 'Signatures with the same name as this group (+.VBS) will be searched within this
setup_Path_Template = "\\MyDomain.local\NETLOGON\Outlook\Templates" 'Signatures with the same name as this group (+.VBS) will be searched within this
setup_PRF_CacheOn = "\\MyDomain.local\NETLOGON\Outlook\PRF\Outlook_Cached.PRF"
setup_PRF_CacheOff = "\\MyDomain.local\NETLOGON\Outlook\PRF\Outlook_NotCached.PRF"
' ##### START OF SCRIPT #####
Set oShell = CreateObject("WScript.Shell")
Set oAD = CreateObject("ADSystemInfo")
Set oFile = CreateObject("Scripting.FileSystemObject")
Set oOutlook = CreateObject("Outlook.Application")
Set oUser = GetObject("LDAP://" & oAD.UserName)
'Quit if no outlook is present!
If oOutlook = false Then
oShell.LogEvent 1, "Signature script error. Outlook application object was not found."
Wscript.Quit
End If
'Quit if version is lower then 10
v = Split(oOutlook.Version, ".")
outlook_Version = v(0) & "." & v(1)
If cInt(v(0)) < 10 Then
oShell.LogEvent 1, "Signature script error. Outlook version " & outlook_Version & " is not supported."
Wscript.Quit
ElseIf (cInt(v(0)) >= 10) And (cInt(v(0)) < 15) Then
reg_DefaultProfile = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\DefaultProfile"
Else
reg_DefaultProfile = "HKCU\Software\Microsoft\Office\" & outlook_Version & "\Outlook\DefaultProfile"
End If
'Check if Office's Userinfo already exists
t1 = oShell.RegRead("HKCU\Software\Microsoft\Office\Common\UserInfo\UserName")
If t1 = "" Then
'Add userinfo to registry.
oShell.RegWrite "HKCU\Software\Microsoft\Office\Common\UserInfo\UserName", oUser.FullName
oShell.RegWrite "HKCU\Software\Microsoft\Office\Common\UserInfo\UserInitials", oUser.sAMAccountName
oShell.RegWrite "HKCU\Software\Microsoft\Office\Common\UserInfo\Company", oUser.company
oShell.LogEvent 0, "Office userinformation was imported from AD."
End If
'Check for a default mail profile
t2 = oShell.RegRead(reg_DefaultProfile)
oShell.LogEvent 0, "Default profile (" & reg_DefaultProfile & ") said " & t2
If t2 = "" Then 'No default profile was found! Import PRF file!
'Detect mobile devices. Enabled cached outlook if there is a battery present
MobileDevice = false
Set oBattery = GetObject("Winmgmts:").ExecQuery("Select * from Win32_Battery")
For Each x in oBattery
MobileDevice = true
Next
'Import PRF location into registry
If MobileDevice Then
oShell.RegWrite "HKCU\Software\Microsoft\Office\" & outlook_Version & "\Outlook\Setup\ImportPRF", setup_PRF_CacheOn
oShell.LogEvent 0, "Office Outlook has been set-up with cache."
Else
oShell.RegWrite "HKCU\Software\Microsoft\Office\" & outlook_Version & "\Outlook\Setup\ImportPRF", setup_PRF_CacheOff
oShell.LogEvent 0, "Office Outlook has been set-up without cache."
End If
'Delete First-Run key to simulate a first run for outlook. (i.e. if a profile was configured and deleted)
oShell.RegDelete "HKCU\Software\Microsoft\Office\" & outlook_Version & "\Outlook\Setup\First-Run"
'Outlook does not need to be run.
'When a signature is being applied outlook will fire up it's initial boot and import the PRF settings.
'With this PRF applied the signature will be applied immediately
End If
'Compare users' group membership against available signature settings
Set GroupsOfUser = GetMembership(oUser.distinguishedName, null)
tGroups = Array()
For Each GroupName in GroupsOfUser.Items()
If Mid(GroupName, 1, Len(setup_GroupPrefix)) = setup_GroupPrefix Then
ReDim Preserve tGroups(UBound(tGroups) + 1)
tGroups(UBound(tGroups)) = GroupName
end if
Next
tGroups = SortArray(tGroups)
For Each group in tGroups
sFile = setup_Path_SignatureVBS & "\" & group & ".VBS"
If oFile.FileExists(sFile) = True Then 'File containing specific signature settings were found
Set Signature = new Defaults 'Use defaults
'Evaluate signature settings
executeGlobal oFile.openTextFile(sFile).readAll()
'Check if signature needs updating
sUpdate = false
If oFile.FileExists(Signature.sPath) Then
Set f = oFile.GetFile(Signature.sPath)
If Signature.sVersion > f.DateLastModified Then
sUpdate = true
End If
Else
sUpdate = true
End If
If sUpdate Then 'Apply signature
'Replace defaults with user specific data
If Not oUser.FullName = "" Then Signature.uName = oUser.FullName
If Not oUser.mail = "" Then Signature.uMail = LCase(oUser.mail)
If Not oUser.telephoneNumber = "" Then Signature.uPhone = oUser.telephoneNumber
If Not oUser.mobile = "" Then Signature.uCell = oUser.mobile
If Not oUser.facsimileTelephoneNumber = "" Then Signature.uFax = oUser.facsimileTelephoneNumber
If Not oUser.Title = "" Then Signature.uTitle = oUser.Title
If Not oUser.department = "" Then Signature.uDepartment = oUser.department
If Not oUser.info = "" Then Signature.uDisclaimer = oUser.info
'Build signature
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Add()
Set oSelection = oWord.Selection
executeGlobal oFile.openTextFile(setup_Path_Template & "\" & Signature.sTemplate).readAll() 'Evaluate signatre template
Set oSelection = oDoc.Range()
'Add signature to outlook
oWord.EmailOptions.EmailSignature.EmailSignatureEntries.Add Signature.sName, oSelection
WScript.Sleep 200 'Give outlook the time to create the necessary files
'Set as default signature
If Signature.sCompanyNew = "*" OR StrComp(oUser.company, Signature.sCompanyNew, vbTextCompare) = 0 Then oWord.EmailOptions.EmailSignature.NewMessageSignature = Signature.sName
If Signature.sCompanyReply = "*" OR StrComp(oUser.company, Signature.sCompanyReply, vbTextCompare) = 0 Then oWord.EmailOptions.EmailSignature.ReplyMessageSignature = Signature.sName
'Closure
oDoc.Saved = True
oWord.Quit
WScript.Sleep 300 'Give work some time to clean up
'Logging
oShell.LogEvent 0, "Signature " & Signature.sName & " applied with success!"
Else
oShell.LogEvent 0, "Signature " & Signature.sName & " is up to date."
End If
Else
oShell.LogEvent 1, "Signature script error. Cannot load signature settings from " & sFile
End If
Next
' ##### CLASSES AND FUNCTIONS ######
Class Defaults
'Signature properties
Public sName 'Name of this signature
Public sVersion 'Apply signature if version date is newer then client's signature date
Public sTemplate 'Signature template to be used
Public sCompanyNew 'If the user company field matches this value it will be set as the default 'new' signature
Public sCompanyReply 'If the user company field matches this value it will be set as the default 'reply' signature
'Company properties
Public cName
Public cStreet
Public cBox
Public cPostal
Public cCity
Public cState
Public cCountry
Public cMail
Public cVat
Public cWebsite
Public cUrl
Public cLogo
Public cLogoPath
Public cPhone
Public cFax
'User properties
Public uName
Public uMail
Public uPhone
Public uCell
Public uFax
Public uTitle
Public uDepartment
Public uDisclaimer
Private Sub Class_Initialize()
me.sName = "The name of my signature"
me.sVersion = CDate("1/10/2012")
me.sTemplate = "Default.vbs"
me.cName = "MY COMPANY NAME"
me.cStreet = "Street"
me.cBox = "123"
me.cPostal = "ZIP"
me.cCity = "CITY"
me.cMail = "info#company.com"
me.cVat = "VAT NUMBER"
me.cWebsite = "www.company.com"
me.cUrl = "http://www.company.com"
me.cPhone = "+32 3 456 780"
me.cFax = "+32 3 456 789"
me.uName = "John Doe"
me.uPhone = "+32 3 456 780"
me.uFax = "+32 3 456 780"
End Sub
Public Property Get sPath()
sPath = oShell.ExpandEnvironmentStrings("%AppData%") + "\Microsoft\" & oShell.RegRead("HKCU\Software\Microsoft\Office\" & outlook_Version & "\Common\General\Signatures") & "\" & me.sName & ".htm"
End Property
End Class
Function SortArray(arrShort)
Dim i, j, temp
For i = UBound(arrShort) - 1 To 0 Step -1
For j= 0 To i
If arrShort(j)>arrShort(j+1) Then
temp=arrShort(j+1)
arrShort(j+1)=arrShort(j)
arrShort(j)=temp
End If
Next
Next
SortArray = arrShort
End Function
Function GetMembership(sChild, dMembership)
'Get AD info on the given Child
Set oChild = GetObject("LDAP://" & sChild)
If TypeName(oChild) = "Object" Then
'Add the Child's canonical name to the array IF it's a group
If TypeName(dMembership) = "Dictionary" Then
dMembership.Add oChild.distinguishedName, oChild.CN
Else
Set dMembership = CreateObject("Scripting.Dictionary")
End If
'If the Child has any parents (=groups), run the same loop for these parents.
If TypeName(oChild.memberOf) = "Variant()" Then
oParents = oChild.GetEx("memberOf")
For Each sParent in oParents
If Not dMembership.Exists(sParent) Then
Set dMembership = GetMembership(sParent, dMembership)
End If
Next
End If
End If
Set GetMembership = dMembership
End Function
Below the signature 'guide'. These scripts MUST have the same name as the group created in AD to work. When a user is a member of the AD group Signature-Marketing, it will run \\MyDomain.local\NETLOGON\Outlook\Signatures\Signature-Marketing.vbs
'\\MyDomain.local\NETLOGON\Outlook\Signatures\MyGroupName.vbs
'Set specific default values
Signature.sVersion = CDate("3/12/2012 15:35")
Signature.sName = "The name of my signature"
Signature.sCompanyNew = "MY COMPANY NAME"
Signature.sCompanyReply = "MY COMPANY NAME"
Signature.cName = "MY COMPANY NAME"
Signature.cStreet = "Street"
Signature.cBox = "123"
Signature.cPostal = "ZIP"
Signature.cCity = "City"
Signature.cMail = "info#company.com"
Signature.cVat = "VAT NUMBER"
Signature.cWebsite = "www.company.com"
Signature.cUrl = "http://www.company.com"
Signature.cLogo = "\\MyDomain.local\NETLOGON\Outlook\IMG\MyCompanyLogo.png"
Signature.cPhone = "+32 3 456 780"
Signature.cFax = "+32 3 456 789"
Signature.uName = "John Doe"
Signature.uPhone = "+32 3 456 780"
Signature.uFax = "+32 3 456 789"
Below the default template. This script gets evaluated within Outlook.vbs
'\\MyDomain.local\NETLOGON\Outlook\Templates\Default.vbs
oSelection.Font.Name = "Calibri"
oSelection.Font.Size = 11
oSelection.TypeText Signature.uName
If Not Signature.uTitle = "" Then
oSelection.TypeText Chr(11)
oSelection.TypeText Signature.uTitle
End If
If Not Signature.uDisclaimer = "" Then oSelection.TypeText " (*)"
' ### Add company table & info
oSelection.TypeParagraph()
Set tbl = oDoc.Tables.Add(oSelection.Range, 1, 2)
Set oTable = oDoc.Tables(1)
tWidth = oTable.Cell(1, 1).width + oTable.Cell(1, 2).width
' Add company logo to cell 1
Set oCell = oTable.Cell(1, 1)
Set oCellRange = oCell.Range
oCell.Select
Set oLogo = oSelection.InlineShapes.AddPicture(Signature.cLogo)
oLogo.LockAspectRatio = true
oLogo.height = oWord.PixelsToPoints(50)
oCell.width = oLogo.width
' Add company info to cell 2
If Signature.cVat = "" Then
arrAddressInfo = Array(Signature.cName, Signature.cStreet & " " & Signature.cBox, Signature.cPostal & " " & Signature.cCity)
Else
arrAddressInfo = Array(Signature.cName, Signature.cStreet & " " & Signature.cBox, Signature.cPostal & " " & Signature.cCity, Signature.cVat)
End If
strAddressInfo = Join(arrAddressInfo, " | ")
Set oCell = oTable.Cell(1, 2)
Set oCellRange = oCell.Range
oCell.Select
oCell.width = tWidth - oLogo.width
oSelection.Font.Size = 10
oSelection.TypeText strAddressInfo
' Add phone number information
arrUserInfo = Array()
If Not Signature.uPhone = "" Then
ReDim Preserve arrUserInfo(UBound(arrUserInfo) + 1)
arrUserInfo(UBound(arrUserInfo)) = "T " & Signature.uPhone
End If
If Not Signature.uCell = "" Then
ReDim Preserve arrUserInfo(UBound(arrUserInfo) + 1)
arrUserInfo(UBound(arrUserInfo)) = "G " & Signature.uCell
End If
If Not Signature.uFax = "" Then
ReDim Preserve arrUserInfo(UBound(arrUserInfo) + 1)
arrUserInfo(UBound(arrUserInfo)) = "F " & Signature.uFax
End If
strUserInfo = Join(arrUserInfo, " | ")
If Not strUserInfo = "" Then
oSelection.TypeText Chr(11)
oSelection.TypeText strUserInfo
End If
oSelection.TypeText Chr(11)
' Add user mail address to cell 2
Set oLink = oSelection.Hyperlinks.Add(oSelection.Range, "mailto:" & Signature.uMail, , , Signature.uMail)
oLink.Range.Font.Color = oSelection.Font.Color
oLink.Range.Font.Size = 10
' Add company weblink to cell 2
oSelection.TypeText " | "
Set oLink = oSelection.Hyperlinks.Add(oSelection.Range, Signature.cUrl, , , Signature.cWebsite)
oLink.Range.Font.Color = oSelection.Font.Color
oLink.Range.Font.Size = 10
If Not Signature.uDisclaimer = "" Then oSelection.TypeText " | (*) " & Signature.uDisclaimer
tbl.Rows(1).Cells.VerticalAlignment = 1
oTable.AutoFitBehavior(1)
\\MyDomain.local\NETLOGON\Outlook\PRF\Outlook_Cached.PRF
\\MyDomain.local\NETLOGON\Outlook\PRF\Outlook_NotCached.PRF
Create your own PRF file (quote from Microsoft TechNet):
To create a PRF file by using the Office Customization Tool
From the root of the network installation point, run the following command line to start the Office Customization Tool: \server\share\setup.exe /admin
To edit an existing customization file (.msp), in the Select Product dialog box, click Open an existing Setup customization file. Or to create a new customization file, select the Office suite that you want to customize, and then click OK.
In the Outlook area, click Outlook Profile. Select how you want to customize profiles for users. To specify settings to be included in a .prf file, choose Modify Profile or New Profile.
To add and configure new accounts or to modify or remove existing accounts, click Add accounts, and then click Customize additional Outlook profile and account information.
Once you complete the Outlook profile configurations, in the Outlook area, click Export settings.
Click the Export Profile Settings button to create a new .prf file. Enter a file name and the path on which to save the file, and then click Save.
The open source project GenerateSignatureFromLDAP can generate Outlook signatures based on a template and values taken from Active Directory.
This can be set in a startup script (e.g. via GPO).
The templates can contain "if" statements to modify the template based on specific criteria (e.g. the current date or an AD attribute like location).
See: https://sourceforge.net/projects/gensignfromldap/

Passing objects as arguments in VBScript

I'm working on a project to capture various disk performance metrics using VBScript and would like to use a sub procedure with an object as an argument. In the following code samples the object I'm referring to is objitem.AvgDiskQueueLength which will provide a value for the disk queue length. I haven't found a way to make it work since it is recognized as a string and then doesn't capture the value. My goal is to make it easy for anyone to change the counters that are to be captured by only having to make a change in one location(the procedure call argument). The way I'm going about this may not be the best but I'm open to suggestions. The sub procedure call is below.
PerfCounter "Average Disk Queue Length", "disk_queueLength", "objItem.AvgDiskQueueLength"
The following code is the sub procedure.
Sub PerfCounter(CounterDescription, CounterLabel, CounterObject)
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48)
args_index = args_index + 1
arrCriteria = split(command_line_args(args_index),",")
strDriveLetter = UCase(arrCriteria(0))
intCriticalThreshold = arrCriteria(1)
intWarningThreshold = arrCriteria(2)
For Each objItem in colItems
With objItem
WScript.Echo "objitem.name = " & objitem.name
If InStr(objItem.Name, strDriveLetter & ":") > 0 Then
intChrLocation = InStr(objItem.Name, strDriveletter)
strInstanceName = Mid(objItem.Name, intChrLocation, 1)
End If
If strDriveLetter = strInstanceName AND InStr(objItem.Name, strDriveLetter & ":") > 0 Then
If intActiveNode = 1 OR Len(intActiveNode) < 1 Then
WScript.Echo "CounterDescription = " & CounterDescription
WScript.Echo "CounterLabel = " & CounterLabel
WScript.Echo "CounterObject = " & CounterObject
If CInt(CounterOjbect) => CInt(intCriticalThreshold) Then
arrStatus(i) = "CRITICAL: " & strDriveLetter & ": " & CounterDescription
arrTrendData(i) = CounterLabel & "=" & CounterObject
intExitCode = 2
arrExitCode(i) = intExitCode
ElseIf CInt(CounterOjbect) => CInt(intWarningThreshold) AND CInt(CounterObject) < CInt(intCriticalThreshold) Then
arrStatus(i) = "WARNING: " & strDriveLetter & ": " & CounterDescription
arrTrendData(i) = CounterLabel & "=" & CounterObject
intExitCode = 1
arrExitCode(i) = intExitCode
Else
arrStatus(i) = "OK: " & strDriveLetter & ": " & CounterDescription
arrTrendData(i) = CounterLabel & "=" & CounterObject
intExitCode = 0
arrExitCode(i) = intExitCode
End If
Else
PassiveNode CounterDescription, CounterLabel
End If
End If
End With
Next
i = i + 1
ReDim Preserve arrStatus(i)
ReDim Preserve arrTrendData(i)
ReDim Preserve arrExitCode(i)
End Sub
Why cant you do this...
PerfCounter "Average Disk Queue Length", "disk_queueLength", objItem.AvgDiskQueueLength
To pass an object you have to pass an object, not a string. To make this method work as expected you would have to have the object prior to the procedure call, but in your code example it looks like you are trying to pass an object that you don't have. A working example:
Set objFSO = CreateObject("Scripting.FileSystemObject")
UseFileSystemObject objFSO
Sub UseFileSystemObject( objfso)
'Now I can use the FileSystemObject in this procedure.
End Sub
But calling the UseFileSystemObject procedure like this will not work,
UseFileSystemObject "objFSO"
because you are passing in a string not an object.
The only way I can think of to accomplish what you want is to use a select statement to write the appropriate attribute of the object, something like this.
Call PerfCounter "Average Disk Queue Length", "disk_queueLength", "AvgDiskQueueLength"
Sub PerfCounter(CounterDescription, CounterLabel, CounterObjectAttribute)
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48)
For Each objItem in colItems
Select Case CounterObjectAttribute
Case "ObjectAttribute1"
Case "ObjectAttribute2"
Case "AvgDiskQueueLength"
Wscript.Echo objItem.AvgDiskQueueLength
End Select
Next
End Sub
So in the select you would have to add a case for each attribute that can be used, but it would allow you to pass a string into the procedure. I might be way off on this, but I don't know how you can pass an object if you don't have the object first.

Resources