Hi, I am trying to send different excel files to their respective email recipients by including the table from the excel file in the email body. I am having problem with the bold lines of code below where it doesn’t loop to each excel file available but work perfectly fine if I specify the range as follows ( $rgeSource=$UserWorksheet.range(“A1”,“D11”)) instead of $rgeSource=$UserWorksheet.range($firstrow,$lastCol). My code as below:<\/p>\n
$RefineryConfiguration = Get-Content -Path \"path.json\" -Raw | ConvertFrom-Json\n$RefineryList = $RefineryConfiguration.refineries\n\nforeach ($Refinery in $RefineryList) {\n $x1 = New-Object -comobject Excel.Application\n $UserWorkBook = $x1.Workbooks.Open($Refinery.filepath)\n $UserWorksheet = $UserWorkBook.Worksheets.Item(1)\n $UserWorksheet.activate()\n #change here to get last row and column, original: $rgeSource=$UserWorksheet.range(“A1”,“D11”)#\n [int]$firstRowvalue = ($UserWorksheet.UsedRange.rows.count + 1)\n $firstrow = $UserWorksheet.Cells.Item($firstRowvalue, 1).Value\n [int]$lastColvalue = ($UserWorksheet.UsedRange.columns.count + 1) - 1\n $lastCol = $UserWorksheet.Cells.Item($lastColvalue, 1).Value\n $rgeSource=$UserWorksheet.range($firstrow,$lastCol)\n #done change##\n $rgeSource.Copy() | Out-Null\n $Outlook = New-Object -comObject Outlook.Application\n $Mail = $Outlook.CreateItem(0)\n $Mail.Recipients.Add($Refinery.email)\n $Mail.Attachments.Add($Refinery.filepath)\n $Mail.Subject=“enter subject”\n $oDoc = $Mail.GetInspector.WordEditor\n $oRange = $oDoc.Range()\n $oRange.InsertParagraph\n $oRange.InsertBefore(“Hi Team,”)\n $oRange.InsertParagraphAfter()\n $oRange.InsertParagraphAfter()\n $oRange.InsertAfter(\" enter the mail body\")\n $oRange.InsertParagraphAfter()\n $oRange.InsertParagraphAfter()\n $oRange.Collapse($wdCollapseStart)\n $oRange.Paste()\n $oRange.Collapse($wdCollapseEnd)\n $oRange.move(4,3)\n $oRange.InsertParagraphAfter()\n $oRange.InsertAfter(“Thanks & Regards,”)\n $oRange.InsertParagraphAfter()\n $Mail.Display()\n $Mail.Send()\n}\n<\/code><\/pre>\n
Advertisement
Your help is much appreciated. Thank you in advance.<\/p>","upvoteCount":6,"answerCount":6,"datePublished":"2019-09-17T00:33:17.000Z","author":{"@type":"Person","name":"rain123","url":"https://community.spiceworks.com/u/rain123"},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
Hi, I am trying to send different excel files to their respective email recipients by including the table from the excel file in the email body. I am having problem with the bold lines of code below where it doesn’t loop to each excel file available but work perfectly fine if I specify the range as follows ( $rgeSource=$UserWorksheet.range(“A1”,“D11”)) instead of $rgeSource=$UserWorksheet.range($firstrow,$lastCol). My code as below:<\/p>\n
$RefineryConfiguration = Get-Content -Path \"path.json\" -Raw | ConvertFrom-Json\n$RefineryList = $RefineryConfiguration.refineries\n\nforeach ($Refinery in $RefineryList) {\n $x1 = New-Object -comobject Excel.Application\n $UserWorkBook = $x1.Workbooks.Open($Refinery.filepath)\n $UserWorksheet = $UserWorkBook.Worksheets.Item(1)\n $UserWorksheet.activate()\n #change here to get last row and column, original: $rgeSource=$UserWorksheet.range(“A1”,“D11”)#\n [int]$firstRowvalue = ($UserWorksheet.UsedRange.rows.count + 1)\n $firstrow = $UserWorksheet.Cells.Item($firstRowvalue, 1).Value\n [int]$lastColvalue = ($UserWorksheet.UsedRange.columns.count + 1) - 1\n $lastCol = $UserWorksheet.Cells.Item($lastColvalue, 1).Value\n $rgeSource=$UserWorksheet.range($firstrow,$lastCol)\n #done change##\n $rgeSource.Copy() | Out-Null\n $Outlook = New-Object -comObject Outlook.Application\n $Mail = $Outlook.CreateItem(0)\n $Mail.Recipients.Add($Refinery.email)\n $Mail.Attachments.Add($Refinery.filepath)\n $Mail.Subject=“enter subject”\n $oDoc = $Mail.GetInspector.WordEditor\n $oRange = $oDoc.Range()\n $oRange.InsertParagraph\n $oRange.InsertBefore(“Hi Team,”)\n $oRange.InsertParagraphAfter()\n $oRange.InsertParagraphAfter()\n $oRange.InsertAfter(\" enter the mail body\")\n $oRange.InsertParagraphAfter()\n $oRange.InsertParagraphAfter()\n $oRange.Collapse($wdCollapseStart)\n $oRange.Paste()\n $oRange.Collapse($wdCollapseEnd)\n $oRange.move(4,3)\n $oRange.InsertParagraphAfter()\n $oRange.InsertAfter(“Thanks & Regards,”)\n $oRange.InsertParagraphAfter()\n $Mail.Display()\n $Mail.Send()\n}\n<\/code><\/pre>\nYour help is much appreciated. Thank you in advance.<\/p>","upvoteCount":6,"datePublished":"2019-09-17T00:33:17.000Z","url":"https://community.spiceworks.com/t/insert-excel-table-into-email-body-using-for-each-loop/730337/1","author":{"@type":"Person","name":"rain123","url":"https://community.spiceworks.com/u/rain123"}},{"@type":"Answer","text":"
If you post code, please use the insert code button </><\/p>\n