1

I have the following VB Script:

Set MSOutlook = CreateObject("Outlook.Application")
Set MailMsg = MSOutlook.CreateItem(0)

With MailMsg            
        .To = "[email protected]"
        .Subject = "my subject"
        .HTMLBody = "my text"
        .Display
        SendKeys "{TAB}{DOWN}{DOWN}{ENTER}", True
        .Send
End With

Set MSOutlook = Nothing
Set MailMsg = Nothing

This script runs fine when executed within a VB compiler, such as Microsoft Visual Basic for Applications.

The SendKeys is required because of this damn Titus classification system we have in place at work to classify everything, such as emails and documents.

The email sends fine and it is classified correctly.

Now I am trying to call this script from the following Powershell script:

& cscript 'C:\Documents\sendEmail.vbs' //nologo

But the following error is received either in the Powershell ISE or when calling the vb script directly from a PowerShell console:

cscript.exe : C:\Documents\sendEmail.vbs Microsoft VBScript runtime error: Type mismatch: 'SendKeys'

Anyone know much about this and why the SendKeys function apparently cannot be used this way?

To pre-empt comments, yes, I would really like to move this vb functionality into the PowerShell script but the TITUS plugin is causing all sorts of headaches around that right now.

Thanks

3
  • 1
    We used to use Titus until we swapped to email classification, then dset. Why not just Powershell to do it all for you instead of calling on a VBScript? Commented May 18, 2021 at 15:16
  • 1
    I agree. You're asking for trouble by chaining together so many scripts.... with a sendkey on the end. I suggest you write the entire thing in Powershell. OR take Powershell out of it and just use cscript.exe Commented May 19, 2021 at 8:00
  • That's the plan, I just wanted to get something working "in practice" first - the idea being to port all this legacy vb stuff to PS Commented May 19, 2021 at 8:07

1 Answer 1

3

In VBA, the SendKeys command is a member of the global Interaction module and, thus, always in scope.

In VBScript, you need to use the SendKeys method of the WScript.Shell object instead:

Dim shell
Set shell = CreateObject("WScript.Shell")
shell.SendKeys "{TAB}{DOWN}{DOWN}{ENTER}", True
Sign up to request clarification or add additional context in comments.

Comments

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.