Eds
Eds

Reputation: 575

If statement within VBscript function not working

I am hoping someone can help me possibly debug the below code, as I am confused as to why it is happening.

I have a fairly simple VBScript that runs when a user logs onto a server/PC, that will create some signatures based on their active directory details. I have decided to move each section of the signature creation into a function, in order to make things a bit easier when creating new signatures.

Here is the function I am having issues with:

'Function to add job title and company
Function AddTitle
    objSelection.Font.Name = "Calibri"
    objSelection.Font.Bold = False
    objSelection.Font.Italic = False
    objSelection.Font.Size = "11"
    objSelection.Font.Color = RGB(0,0,0)
    If(strTitle) Then
        objSelection.TypeText strTitle & Chr(11)
    End If
    objSelection.TypeText strCompany & Chr (11)
End Function

Now, when calling the function later on using:

'Add job title and company
AddTitle

It ignores the section within the If statement. I know that some variables need to be defined globally for them to work in a function, and strTitle is definitely defined at the beginning of my script.

Am I missing something totally obvious, as the section inside the if statement functions correctly if taken out of the if statement. Likewise, If I were to add the if statement to my script inline, and take it out of the function, it works correctly.

It is only when running from the Function.

Upvotes: 0

Views: 1741

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

Apparently you have a global On Error Resume Next somewhere in your script, which suppresses the "Type Mismatch" error that the line If(strTitle) Then normally would raise.

strTitle probably contains a string value, so you can't use it like a boolean value in a conditional. Change the line to something like this:

If Trim(strTitle) <> "" Then

and your code should work as expected.

Upvotes: 2

Related Questions