Get attach from outlook return error

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("")
$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


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
$emails = $inbox.items

This is probably the problem. You are getting all the items in the inbox and assuming that they’re of type MailItem. Some of the items in the collection won’t have the CreationTime property, hence the error.

You should filter your Items collection to ensure it contains only objects of class MailItem.