Viktor Cvijic
Viktor Cvijic

Reputation: 1

Power shell Outlook interop assembly problem

I'm trying to use PowerShell script to extract attachments from outlook item I have in a folder. The code is :`

# Specify the folder containing Outlook item files (.msg)
$sourceFolder = "C:\GRAINGER\GRAINGER\06-07-2023 Placements"

# Specify the parent folder where you want to save attachments
$saveFolder = "C:\GRAINGER\GRAINGER\Attachments\06-07-2023 Placements A"

# Load the Outlook COM Object
Add-Type -TypeDefinition @"
    using System;
    using Microsoft.Office.Interop.Outlook;
"@

# Create an Outlook Application object
$outlook = New-Object -ComObject Outlook.Application

# Get a list of .msg files in the source folder
$msgFiles = Get-ChildItem $sourceFolder -Filter *.msg

# Loop through each .msg file and extract attachments
foreach ($msgFile in $msgFiles) {
    $mailItem = $outlook.Session.OpenSharedItem($msgFile.FullName)

    # Create a folder with the same name as the Outlook item file
    $itemFolder = New-Item -Path "$saveFolder\$($msgFile.BaseName)" -ItemType Directory

    # Loop through each attachment in the email
    foreach ($attachment in $mailItem.Attachments) {
        $attachment.SaveAsFile("$itemFolder\$($attachment.FileName)")
    }

    # Close the mail item
    $mailItem.Close()
}

# Release Outlook objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($attachment) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null

And I keep getting errors like : The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except ion + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

I have office 365 installed, and outlook also. But I'm stuck here ...

Upvotes: 0

Views: 278

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49405

The error message says that an assembly reference is missing:

 The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

So, you need to add assembly references before using the Outlook object model in the code. For example, here is how you may connect to the running Outlook instance or create a new Outlook Application instance if none is running:

Add-Type -assembly "Microsoft.Office.Interop.Outlook"
Add-Type -assembly "System.Runtime.Interopservices"
try
{
$outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
    $outlookWasAlreadyRunning = $true
}
catch
{
    try
    {
        $Outlook = New-Object -comobject Outlook.Application
        $outlookWasAlreadyRunning = $false
    }
    catch
    {
        write-host "You must exit Outlook first."
        exit
    }
}
$namespace = $Outlook.GetNameSpace("MAPI")

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

You need to add the interop dll first

Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook"

Or you can skip the whole Add-Type -TypeDefinition @" block and use late binding without relying on interop.

Upvotes: 1

Related Questions