0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ansgar, you absolute gem! This is my first time work in VBScript, and the base of the script was lifted from example code. There is indeed an On Error Resume Next set globally, and I should have know about the boolean conditional from my work with PHP. Thanks again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.