1

I'm passing selected Outlook.MailMessage properties from a VBA script to PowerShell:

Public Sub InvokeMessageParser()

    For Each Item In Application.ActiveExplorer.Selection
        If TypeName(Item) = "MailItem" Then

            Dim MailItem As Outlook.MailItem: Set MailItem = Item

            // arrange
            Dim cmd As String: cmd = "powershell -NoExit -NoProfile -File C:\Users\[user]\Desktop\Invoke-MessageParser.ps1 -Subject """ & MailItem.Subject & """ -Verbose"

            // act
            i = Shell(cmd, vbNormalFocus)

        End If
    Next

End Sub

Invoke-MessageParser.ps1:

param (
    [string]$Subject
)

Write-Host "Subject: $Subject"

Which prints the expected value to the console.

I would prefer to pass the entire message, but haven't been able to get the syntax correct:

// arrange
Dim cmd As String: cmd = "powershell -NoExit -NoProfile -File C:\Users\[user]\Desktop\Invoke-MessageParser.ps1 -Message " & MailItem

Invoke-MessageParser.ps1:

param (
    [object]$Message
)
Write-Verbose "Subject: $($Message.Subject)"

Which results in Subject: being printed to the console.

Is this possible?

3
  • Do you debug your code? Do you run it in Visual Studio Code or PowerShell ISE? That way you can easily see what members $Message have. Commented Feb 7, 2019 at 16:26
  • I'm using Outlook for the VBA part and VSCode for the PowerShell part. $Message is a System.String. Commented Feb 7, 2019 at 16:28
  • craig wrote: $Message is a System.String. In that case there's no member .subject.Back to the drawing board Commented Feb 7, 2019 at 16:32

1 Answer 1

1

You can only pass a string via a command line. You cannot pass a COM object. The best you can do is pass the value of the MailItem.EntryID property and use it to open the object by teh entry id in the child process using Namespace.GetItemFromID.

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.