Passing VBScript Variables

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Passing VBScript Variables

Post by kpassaur » Sun Aug 13, 2006 1:43 pm

I need a little help from someone that is familar with VBScript and MS

The script below will work if I take out the remarks, name it as a Sub Routine instead of a function. However, I want to send it data and recieve a response. When I convert it to a Function (or event leave it as a Sub Routing I get a Name Redefined Error. I know you need to pass the varialble to VB Script in "" when using VBEval, however I have tried it both ways.

The purpose of the script it to see if a Contact in Outlook already exists. I want to return 0 if no greater than 0 if yes.

Code: Select all

VBSTART
Function FindContact (strInput)
Dim objOutlook
Dim objNameSpace
Dim objFolder

Dim strInput
'Dim strMsg
Dim cItem
Dim strOutput 
Dim counter 

Const olFolderContacts = 10 
Const cTextCaseInsensitive = 1

  ' strMsg = "Enter a search string." & vbcrlf & "(Example: Dan)"
  ' strInput = InputBox(strMsg,"Search For ...","Dan")
'strInput = "wertert"
    Set objOutlook = CreateObject("Outlook.application")
    Set objNameSpace = objOutlook.GetNameSpace("MAPI")
    Set objFolder = objNameSpace.GetDefaultFolder(olFolderContacts)
counter = 0
    For Each cItem in objFolder.Items
        If Instr(1,cItem.FullName,strInput,cTextCaseInsensitive) > 0 Then
		counter=counter+1
            strOutput = String(45,"~") & vbCrLf
            strOutput = strOutput & "Full Name:" & vbTab & cItem.FullName & vbCrLf 


            strOutput = strOutput & strAddress & vbCrLf   
            strOutput = strOutput & String(45,"~") & vbCrLf & counter

	'  Msgbox strOutput, , cItem.Subject
        End If
    Next

  If strOutput = "" Then
'Msgbox "No contacts match search requirements."& vbCrLf  & counter, vbInformation,"Contact Search" 
  End If

' ****  Clean up
'
    Set objFolder = Nothing
    Set objNameSpace = Nothing
    set objOutlook = Nothing
	END Function
	VBEND
	VBEval>FindContact("John"),answer
MDL>%answer%

So, the message boxes go and just the value needs to be returned. I have written a part that adds a contact and a task (similar to the one Marcus Posted) and that all works. However, I want to prompt the user frist if the Contact exists.

Any idea as to where I am going wrong?

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Figured out the first error

Post by kpassaur » Sun Aug 13, 2006 6:28 pm

The Name defined error is because of the line dim strInpput is the same as the variable that I am passing; however, I still need a way to get the results back into MS

hvdberg
Junior Coder
Posts: 39
Joined: Wed Jul 12, 2006 1:50 pm
Location: Netherlands

Post by hvdberg » Mon Aug 14, 2006 7:31 am

Hi kpassaur,

If you change strOutput to FindContact you will get what you need. Returning values from a function requires that you assign your result to a variable with the same name as the function name. Don't dim a variable FindContact within your function, however. The declaration is taken care of automagically.

Example:
VBSTART
Function Cubed(Number2Cube)
Dim result
result = Number2Cube * Number2Cube * Number2Cube
Cubed = result
End Function
VBEND
VBEVAL>Cubed(3),CubedResult
MDL>%CubedResult%

This results in a modal dialogbox with 27
Granted, it's a trivial example but it clarifies how functions work in MS.

HTH,
Henk

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Mon Aug 14, 2006 12:18 pm

perfect answer Henk.

and Kieth, here is your code cleaned up with an input> added for the name...

Code: Select all

VBSTART
Function FindContact (Name2Find)
Dim objOutlook
Dim objNameSpace
Dim objFolder

Dim cItem
Dim strOutput
Dim counter

Const olFolderContacts = 10
Const cTextCaseInsensitive = 1

    Set objOutlook = CreateObject("Outlook.application")
    Set objNameSpace = objOutlook.GetNameSpace("MAPI")
    Set objFolder = objNameSpace.GetDefaultFolder(olFolderContacts)
    counter = 0
    For Each cItem in objFolder.Items
        If Instr(1,cItem.FullName,Name2Find,cTextCaseInsensitive) > 0 Then
		counter=counter+1
            strOutput = String(45,"~") & vbCrLf
            strOutput = strOutput & "Full Name:" & vbTab & cItem.FullName & vbCrLf
            strOutput = strOutput & strAddress & vbCrLf
            strOutput = strOutput & String(45,"~") & vbCrLf & counter
        End If
    Next

    Set objFolder = Nothing
    Set objNameSpace = Nothing
    set objOutlook = Nothing
	END Function
VBEND
    input>olName,Enter a name
	VBEval>FindContact(%olName%),answer
MDL>%answer%


Let us know if it works ok or you need a little more 'tweaking'...

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Works

Post by kpassaur » Mon Aug 14, 2006 12:37 pm

Thank you Both, and Henk, you provided an Execellent description, I feel as I finally am begining to understand it.

I just got it working about five minutes before I recieved this. However, what I cannot seem to find is a method to open the Contact if it exists. For instance what if there are two John Smiths? I was going to use this to say one exists, create another instance? or Abort.

What it should do in the future is if there are two John Smiths, is display a message there are two John Smiths then open one and display it, display a message box if no display the next, if no create a new one.

However that is long term, as it would be very difficult for me to figure out with VBScript.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts