Hello, I used this code, but returned error. Can you please advise me how to fix the code ? Thank you
start-process -FilePath "C:\Program Files (x86)\Microsoft Office\Office16\OUTLOOK.exe"
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace("MAPI");
$recipient = $namespace.CreateRecipient("email@company.com")
$inbox = $namespace.GetSharedDefaultFolder($recipient, 6)
$emails = $inbox.items
$limit = (get-date).Date.ToString("yyyyMMdd")
foreach ($email in $emails) {
if (($email.CreationTime.ToString("yyyyMMdd") -eq $limit) -and (($email.Subject -match "price") -or ($email.Subject -match "image"))) {
foreach ($attachment in $email.attachments) {
$filename = $attachment.filename
$attachment.saveasfile((join-path $savefilepath $filename))
Write-Host $filename
}
}
}
error
You cannot call a method on a null-valued expression.
At C:\script.ps1:34 char:7
+ if (($email.CreationTime.ToString("yyyyMMdd") -eq $limit) -an ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull