Powershell generic error: Exception from HRESULT: 0x800A03EC - excel

I know this is a very generic error message and I have spent many hours looking through similar questions without any luck.
Here is my PowerShell script producing this error:
$ExcelObject = New-Object -ComObject excel.application
$excelProcessId = ((get-process excel | select MainWindowTitle, ID, StartTime | Sort StartTime)[-1]).Id
$ExcelObject.visible = $true
$ExcelObject.DisplayAlerts = $true
$folderpath = "C:\myfiles\TESTER.xlsx"
$Workbook = $ExcelObject.Workbooks.Open($folderpath)
$WorkSheet = $Workbook.Worksheets.Item("TAB1")
$sourceList = "value1,value2,value3"
$missing = [system.type]::missing
$sourceRange = $WorkSheet.Range("A3:A22")
$sourceRange.Validation.add(3,1,$missing,$sourceList,$missing)
$Workbook.save()
$Workbook.close()
$ExcelObject.Quit()
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($Workbook)
[void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject)
[GC]::Collect()
Remove-Variable ExcelObject
Stop-Process -Id $excelProcessId -Force
resulting error:
Exception from HRESULT: 0x800A03EC
At line:11 char:1
+ $sourceRange.Validation.add(3,1,$missing,$sourceList,$missing)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
I have tried to use the value list directly but same error:
$sourceRange.Validation.add(3,1,$missing,"value1,value2,value3",$missing)
I replaced the $missing but same error:
$sourceRange.Validation.add(3,1,1,$sourceList)
I have no more ideas on how to resolve this issue and would appreciate any suggestions.
Thanks a lot for your time.

I have found the problem: the specified range had to be cleared of any existing validation:
$sourceRange = $WorkSheet.Range("A3:A22")
$sourceRange.Validation.Delete()
$sourceRange.Validation.add(3,1,$missing,$sourceList,$missing)
I feel a little embarrassed, but maybe it is of use to someone down the line.

Related

How to Insert Excel Formula using PowerShell?

The formula =LEFT(AB4,FIND(" ",AB5)-1 works perfectly in Excel, but seems to be causing errors in PowerShell where I get this error:
Exception from HRESULT: 0x800A03EC
At C:\Scripts\Excel_NUID2.ps1:21 char:1
+ $worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4,FIND(" ",AB5)-1"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
My PowerShell Script Code;
#Open Up the Workbook#
$excel = new-object -comobject Excel.Application
$excel.visible = $false
$workbook =
$excel.workbooks.open("c:\Users\Jack\documents\NUID_Status_Report.xlsx")
$worksheet = $workbook.Worksheets.Item(1)
$rows = $worksheet.range("A1").currentregion.rows.count
### Set up a filter ###
$headerRange = $worksheet.Range("a4","aj4")
$headerRange.AutoFilter() | Out-Null
#### Trims Password Expiration Date Name ###
$worksheet.range("AH4").formula = "Shortened Expiration Date"
[void]$worksheet.Cells.Item(1,1).select()
$excel.visible = $true
#### Trims Password Expiration Date Formula ###
$worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4,FIND(" ",AB5)-1"
[void]$worksheet.Cells.Item(1,1).select()
$excel.visible = $true
Quotes within a quoted string need to be doubled-up.
$worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4,FIND("" "",AB5)-1)"
'you can also get rid of the inside quotes with the CHAR function
$worksheet.range("AH5:AH$rows").formula = "=LEFT(AB4, FIND(CHAR(32), AB5)-1)"
ASCII character 32 is a space. I've also added a bracket to make a legal formula.

Automate exporting of Excel worksheets to PDF

Editor's note: The OP has abandoned this question and asked a variation at https://stackoverflow.com/questions/38766898/saving-excel-worksheets-to-pdf-using-powershell.
I have been trying to implement the following PowerShell script but without any luck. Can someone please help?
I know this script will save 1 PDF file per Excel workbook, however once I get this working I will look at exporting each Excel worksheet to separate PDF files.
ExportTo-ExcelPDF.ps1 from http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/06/save-a-microsoft-excel-workbook-as-a-pdf-file-by-using-powershell.aspx
$path = "c:\fso"
$xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type]
$excelFiles = Get-ChildItem -Path $path -include *.xls, *.xlsx -recurse
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false
foreach($wb in $excelFiles)
{
$filepath = Join-Path -Path $path -ChildPath ($wb.BaseName + ".pdf")
$workbook = $objExcel.workbooks.open($wb.fullname, 3)
$workbook.Saved = $true
"saving $filepath"
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
$objExcel.Workbooks.close()
}
$objExcel.Quit()
Error messages:
The property 'Saved' cannot be found on this object. Verify that the
property exists and can be set.
At C:\ExportExcel.ps1:23 char:2
+  $workbook.Saved = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
saving C:\ExportExcel.pdf
Method invocation failed because [System.__ComObject] does not contain a
method named 'ExportAsFixedFormat'.
At C:\ExportExcel.ps1:25 char:2
+  $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $filepath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
You cannot call a method on a null-valued expression.
At C:\ExportExcel.ps1:26 char:2
+  $objExcel.Workbooks.close()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Quit" with "0" argument(s): "Call was rejected by callee.
(Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED))"
At C:\ExportExcel.ps1:28 char:1
+ $objExcel.Quit()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
tl;dr
The most likely cause is that Excel's primary interop assembly isn't installed.
The error messages suggest that even though you managed the create the Excel-related objects per se, accessing their members (properties and methods) failed.
Sometimes, setting $VerbosePreference='Continue' helps; for instance, on my machine I see the following when I instantiate the Excel application object (New-Object -ComObject excel.application):
VERBOSE: The object written to the pipeline is an instance of the type "Microsoft.Office.Interop.Excel.ApplicationClass" from the component's primary interoperability assembly. If
this type exposes different members than the IDispatch members, scripts that are written to work with this object might not work if the primary interoperability assembly is not
installed.
Perhaps a missing interoperability assembly is your problem.
On my machine (PSv5, Excel 2010), this interoperability assembly is (a) not loaded by default, (b) is only loaded when you instantiate the Excel application object.
Thus, your code wouldn't work on my machine, because it references type [Microsoft.Office.Interop.Excel.xlFixedFormatType] - which is part of that interop assembly - before the Excel application object is created, which fails - silently, because the indirect type reference "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type] simply returns $null - without complaining - if the type doesn't exist (yet).
If you instead use [Microsoft.Office.Interop.Excel.xlFixedFormatType] directly, you'll instantly get an error if the type doesn't exist / its assembly has not been loaded (yet).
In short:
Remove line $xlFixedFormat = "Microsoft.Office.Interop.Excel.xlFixedFormatType" -as [type].
In its stead, place $xlFixedFormat = [Microsoft.Office.Interop.Excel.xlFixedFormatType] after the objExcel = New-Object -ComObject excel.application line.
Debug your script and run $objExcel | Get-Member and $wb | Get-Member right after the first $objExcel.workbooks.open($wb.fullname, 3) call to see what members PowerShell can see.
If the $xlFixedFormat = [Microsoft.Office.Interop.Excel.xlFixedFormatType] line produces an error right away, or you don't see the members of interest (Saved, ExportAsFixedFormat, ...), the most likely cause is that the interop assembly isn't installed.
Another way to see if the interop assembly is loaded is to execute [Microsoft.Office.Interop.Excel.ApplicationClass] and see if type information is returned (as opposed to an error message).

Powershell could not saveas excel after data written

I am a brand newbie to powershell. I am trying to generate a report to gather all printers' information in our environment. The report is well generated by opening Excel worksheet; however, it fails to save and simply jumps onto close
Below script I used to generate a report:
Add-Type -AssemblyName Microsoft.Office.Interop.Excel
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
$xl = New-Object -ComObject Excel.Application
$xl.Visible = $True
$xls = $xl.Workbooks.Add()
$Sh = $xls.Worksheets.Item(1)
$Sh.Name = "Printer Info"
.
.
.
.
$xls.SaveAs("C:\temp\Test.xlsx", $xlFixedFormat)
$xls.Close()
$xl.Quit()
Error message received is what below:
PS C:\WINDOWS\system32> $Sh.Name = "Printer Info"
PS C:\WINDOWS\system32> $xls.SaveAs("C:\temp\Test.xlsx", $xlFixedFormat)
Exception calling "SaveAs" with "2" argument(s): "SaveAs method of Workbook class failed"
At line:1 char:1
+ $xls.SaveAs("C:\temp\Test.xlsx", $xlFixedFormat)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
The script works find in Windows Server 2008 R2 without any issues; however, it simply fails to work in Windows 7 64 bit.
I would really appreciate if I get an assistance asap.

Assigning a string with escaped characters to a property

I want to set a string path to a string property of a $Shortcut object and it doesn't seam to work.
Running this code :
$WshShell = New-Object -comObject WScript.Shell ;
$Shortcut = $WshShell.CreateShortcut('c:\aaa.lnk');
$Shortcut.TargetPath = '"c:\Program Files\MyApp.exe" /param1: (2) /param2 "val"'
I get this error while setting $Shortcut.TargetPath:
Exception setting "TargetPath": "The parameter is incorrect. (Exception from HRESULT:
0x80070057 (E_INVALIDARG))"
At line:1 char:11
+ $Shortcut. <<<< TargetPath = '"c:\Program Files\MyApp.exe" /param1: (2) /param2 "val"'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The strange thing is that trying to reproduce this behavior on a new custom object, the problem doesn't appear. Just run the commands below:
$object = New-Object -TypeName PSObject
Add-Member -MemberType NoteProperty -Name prop -Value "aaa"
$object.prop = '"c:\Program Files\MyApp.exe" /param1: (2) /param2 "val"'
When in doubt, read the documentation. The TargetPath property takes just the path to the excutable. The arguments go into the Arguments property:
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut('c:\aaa.lnk')
$Shortcut.TargetPath = 'c:\Program Files\MyApp.exe'
$Shortcut.Arguments = '/param1: (2) /param2 "val"'

How to read cell data in excel and output to command prompt

I'm a sys admin and I am trying to learn how to use powershell... I have never done any type of scripting or coding before and I have been teaching myself online by learning from the technet script centre and online forums.
What I am trying to accomplish is to open an excel spreadsheet get information from it (usernames and password) and then output it into the command prompt in powershell. When ever I try to do this I get an Exception calling "InvokeMember" anyway, here is the code I have so far:
function Invoke([object]$m, [string]$method, $parameters)
{
$m.PSBase.GetType().InvokeMember(
$method, [Reflection.BindingFlags]::InvokeMethod, $null, $m, $parameters,$ciUS )
}
$ciUS = [System.Globalization.CultureInfo]'en-US'
$objExcel = New-Object -comobject Excel.Application $objExcel.Visible = $False $objExcel.DisplayAlerts = $False
$objWorkbook = Invoke $objExcel.Workbooks.Open "C:\PS\User Data.xls" Write-Host "Numer of worksheets: " $objWorkbook.Sheets.Count
$objWorksheet = $objWorkbook.Worksheets.Item(1) Write-Host "Worksheet: " $objWorksheet.Name
$Forename = $objWorksheet.Cells.Item(2,1).Text $Surname = $objWorksheet.Cells.Item(2,2).Text
Write-Host "Forename: " $Forename Write-Host "Surname: " $Surname
$objExcel.Quit() If (ps excel) { kill -name excel}
I have read many different posts on forums and articles on how to try and get around the en-US problem but I cannot seem to get around it and hope that someone here can help!
Here is the Exeption problem I mentioned:
Exception calling "InvokeMember" with "6" argument(s): "Method 'System.Management.Automation.PSMethod.C:\PS\User Data.x
ls' not found."
At C:\PS\excel.ps1:3 char:33
+ $m.PSBase.GetType().InvokeMember <<<< (
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Numer of worksheets: You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:18 char:45 + $objWorksheet = $objWorkbook.Worksheets.Item <<<< (1) + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Worksheet: You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:21 char:37 + $Forename = $objWorksheet.Cells.Item <<<< (2,1).Text + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression. At C:\PS\excel.ps1:22 char:36 + $Surname = $objWorksheet.Cells.Item <<<< (2,2).Text + CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Forename:
Surname:
This is the first question I have ever asked, try to be nice! :))
Many Thanks
Max
I find OLE DB and the Office 2007 drivers a little easier to use. If you don't have Office 2007, you can download the drivers from MS.
$filepath = 'C:\Users\u00\documents\backupset.xlsx'
$connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$filepath`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
#Connection String for Excel 2003:
#$connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=`"$filepath`";Extended Properties=`"Excel 8.0;HDR=Yes;IMEX=1`";"
$qry = 'select * from [sheet1$]'
$conn = New-Object System.Data.OleDb.OleDbConnection
$conn.ConnectionString = $connString
$conn.open()
$cmd = new-object System.Data.OleDb.OleDbCommand($qry,$conn)
$da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
$dt = new-object System.Data.dataTable
[void]$da.fill($dt)
$conn.close()
$dt
thanks for your help...I have fixed the problem! I was using PowerShell V2 CTP2, now I have upgraded to CTP3 this has fixed the language barrier problem and I can now run scripts without worrying about the invoke function and all is fine!
Many Thanks
Max

Resources