@dafyre said in Mail Allignment Issue:
$filename = 'file.xlsx'
$From ="[email protected]"
$to = "[email protected]"
$SMTP= "mail.domain.com"
$filepath = "C:\Location$filename"$excel = New-Object -comobject Excel.Application
$ReportWorkBook = $excel.Workbooks.Open($filePath)
$excel.Visible = $false
$ws = $ReportWorkBook.Worksheets.Item(1)
$SelectedRange = $ws.UsedRange
$SelectedRange.Copy()
#$newmail.GetInspector.WordEditor.Range().Paste()$body = @"
My email content goes here
"@
#initate message
$email = New-Object System.Net.Mail.MailMessage
$email.From = $emailFrom
$email.To.Add($emailTo)
$email.Subject = $subject
$email.Body = $body#initiate email attachment
$emailAttach = $SelectedRange.Paste()
$email.Attachments.Add($emailAttach)#initiate sending email
$smtp = new-object Net.Mail.SmtpClient($smtp)
$smtp.Send($email)
Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\Reports\excel.ps1:26 char:1
+ $email.To.Add($emailTo)
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Method invocation failed because [System.__ComObject] does not contain a method named 'Paste'.
At C:\Reports\excel.ps1:31 char:1
+ $emailAttach = $SelectedRange.Paste()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Paste:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\Reports\excel.ps1:32 char:1
+ $email.Attachments.Add($emailAttach)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Exception calling "Send" with "1" argument(s): "A recipient must be specified."
At C:\Reports\excel.ps1:37 char:1
+ $smtp.Send($email)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException