0

I need to run a script that just opens an excel file, calculates an excel cell connected with a Pi DataLink, then tells me the value.

If I try to do that in the way that's standard:

$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True

$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item("Sheet1")

write-host $worksheet.Range("A1").Text

$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()

I get a #NAME? error. And even if I just use the first three lines to just open an excel file and look at it, I can't run calculations, =PICurrVal("TAGNAME",0,"SERVERNAME") is just a dead formula that excel doesn't understand if I open it this way. I've also tried to UpdateLinks when I open the file, but no dice.

However, if I open the file like so:

Invoke-Item "C:\Users\crclayton\sheet.xlsx"

I don't get a #NAME? error and I can run the calculations and excel understands this formula.

Maybe something like this?

Invoke-Item "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"

Start-Sleep 10

$objExcel = Get-Process "EXCEL.EXE"
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item("Sheet1")
write-host $worksheet.Range("A1").Text

Is there some way to get the value in cell A1 having opened the spreadsheet using Invoke-Item?

Community
  • 1
  • 1
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120

1 Answers1

1

I'm not sure why you're getting #NAME? as Excel should be doing all the calculations within the sheet all we're doing in Powershell is getting the value of the cell.

However what you can try is outputting the value of your formula to a nearby cell and getting the value of it instead, for example:

Your formula is in D18 -> =PICurrVal("TAGNAME",0,"SERVERNAME")
Your value is in D19 -> =D18

Call the value in your Powershell:

$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True

$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item(1)

write-host $worksheet.Range("D18").Text

$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()

Update

Excel addins can be added in powershell by using the Addins property like so:

$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"

Your new complete code might look something like

$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True

$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"
$WorkSheet = $WorkBook.Sheets.Item(1)

write-host $worksheet.Range("D18").Text

$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()

Edit 2:

Yes, add-ins were the problem. I needed to add each the following files:

$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\PITrendXL.xla", $True)
$ExcelAddin.Installed = "True"
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\pipc32.xll", $True)
$ExcelAddin.Installed = "True"
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\OSIsoft.PIDataLink.UI.dll.manifest", $True)
$ExcelAddin.Installed = "True"
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
Bluecakes
  • 2,069
  • 17
  • 23
  • I'm afraid setting the value equal to another cell didn't work. I just get `#NAME?` in both. I'm thinking what I need to do is manually import the macro or the xla containing the function `PICurrVal` and run it or something like that. – Charles Clayton Aug 19 '14 at 22:10
  • Ah yes, addins. I should have known but it's too early for me xD (7:30 here) I'll update my answer to include the Addins property and we'll see if that works. – Bluecakes Aug 19 '14 at 22:17
  • So I've added the add-ins but still nothing's changed. Do you know how to force it to run any macros in the addin? – Charles Clayton Aug 19 '14 at 22:26
  • Not sure about forcing but you can run a macro in a loaded sheet by called `Run` like so `$objExcel.Run("MYMACRONAME")`. Give that one a try, Excel Macros is where my knowledge is thin but hopefully you're a lot closer to the solution then you were 30 mins ago :) – Bluecakes Aug 19 '14 at 22:37
  • Yeah, unfortunately I've been giving that a go but I keep getting errors along the lines of this `Exception calling "Run" with "31" argument(s): "Cannot run the macro 'Pi-DataLink'. The macro may not be available in this workbook or a ll macros may be disabled."` Man, I'm contemplating going back to VBScript. – Charles Clayton Aug 19 '14 at 22:41