I want to put some Raw Data into a new Excel File with PowerShell. I'm aware how to use the comobject Excel.Application which PowerShell delivers.
Now my problem is the Data has to be processed in different ways inside the Excel (while the script is running). I already created some VBA Macros to do so a while back and now I'd like to reuse them. Is it possible to "use" the VBA through the Script inside the newly created and filled up Excel? Or do I need to recreate the VBA in Powershell?
Essentially, VBA is a language that connects to the Excel object library. PowerShell is also a language that can connect to Excel object library. Still others including Java, C#, Python, PHP, R, and more have this facility via Component Object Model (COM) available in Windows environments.
Therefore, simply translate your VBA code to PowerShell as any Excel method should be available. VBA tends to be tied to MS Office products because it is the defacto connected language but is actually a separate component (see how it is first reference checked under Tools\References... in IDE). Below are examples of simpler VBA to PowerShell translations:
Initialize Office objects
VBA
' EARLY BINDING
Set xlObj = New Excel.Application
Set accObj = New Access.Application
Set wrdObj = New Word.Application
...
' LATE BINDING
Set xlObj = CreateObject("Excel.Application")
Set accObj = CreateObject("Access.Application")
Set wrdObj = CreateObject("Word.Application")
...
PowerShell
$xlObj = new-object -comobject excel.application
$accObj = new-object -comobject access.application
$wrdObj = new-object -comobject word.application
...
Open Office objects with Excel's Workbooks.Open, Access' OpenCurrentDatabase, Word's Documents.Open. Note: None of these methods are restricted to VBA.
VBA
Set xlWB = xlObj.Workbooks.Open("C:\Path\To\Workbook.xlsx")
Set accDB = accObj.OpenCurrentDatabase("C:\Path\To\Access.accdb")
Set wrdDOC = wrdObj.Documents.Open("C:\Path\To\Document.docx")
...
PowerShell
$xlWB = $xlObj.Workbooks.Open("C:\Path\To\Workbook.xlsx")
$accDB = $accObj.OpenCurrentDatabase("C:\Path\To\Access.accdb")
$wrdDOC = $wrdObj.Documents.Open("C:\Path\To\Document.docx")
...
Handle collections (e.g., Excel sheets, Access tables, Word paragraphs).
VBA
' SELECT SINGLE OBJECTS
Set xlSheet = xlWB.Worksheets("mySheet")
Set accTable = accObj.Currentdb().TableDefs("myTable")
Set wrdParag = wrdDOC.Paragraphs(1)
...
PowerShell
# EXCEL WORKSHEETS LOOP
$xlObj = new-object -comobject excel.application
$xlWB = $xlObj.Workbooks.Open("C:\Path\To\Workbook.xlsx")
Foreach($wsh in $xlWB.Worksheets)
{
Write-host $wsh.name
}
$xlWB.Close($false)
$xlObj.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlObj )
# ACCESS DB TABLES LOOP
$accObj = new-object -comobject access.application
$accDB = $accObj.OpenCurrentDatabase("C:\Path\To\Access.accdb")
Foreach($tbl in $accObj.CurrentDb().TableDefs())
{
Write-host $tbl.name
}
$accDB.DoCmd.CloseDatabase
$accObj.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($accObj )
Related
I want to create a Windows script to execute excel and open a file p and using the password
Example
Start excel c:\Documents\ExcelFile.xls
but also I want to add the password
Start excel c:\Documents\ExcelFile.xls Password
But when I try to do that is not recognized
You can create a PowerShell script to perform that using Workbook Open function.
$path = "c:\temp\1.xls"
$password = "123"
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($path,$false,$false,5,$password)
$excel.Visible = $true
Save the script to a text file OpenExcel.ps1.
Usage:
powershell -file OpenExcel.ps1
Excel has no command line switch to submit a password.
See Command-line switches for Microsoft Office products
But you can use a VBScript StartExcel.vbs instead of a batch file StartExcel.bat to do that:
Set oExcel = CreateObject("Excel.Application")
Set oWorkbook = oExcel.Workbooks.Open("c:\Documents\ExcelFile.xls", 0, 0, 5, "<myPassword")
I cannot retrieve VBA function result from an Excel document into Powershell script.
Powershell file
$suite = "<some_path>"
$excel = new-object -comobject excel.application
$workbook = $excel.workbooks.open($suite)
$specs = $excel.Run("ThisWorkbook.ftp_specs")
IF([string]::IsNullOrEmpty($specs)) {
Write-Host "Your string is EMPTY or NULL"
} else {
Write-Host "Your string is not EMPTY"
}
Excel file (in ThisWorkbook)
Function ftp_specs()
ftp_specs = "hello"
End Function
Powershell run result
Your string is EMPTY or NULL
MS Office VBA Reference states Application.Run returns a Variant.
Nothing i've seen on the web in the past few hours hints at anything relevant.
You don't need ThisWorkbook (?) namespace. Just use Run method with the function name. Like so:
$specs = $excel.Run("ftp_specs")
When I embedding an OLEObject (Word/XLS/PDF doc) into Excel via powershell, I'm unable to save the excel file (I'd like to save manually, not via PShell - but does not work automatically either). If I insert object like *.txt, png,jpeg - there is no problem with saving the target excel file (both auto and manually saving works).
Any idea? Thanks.
The relevant part of the code is:
$missing=[System.Type]::missing
$excel = New-Object -ComObject Excel.Application
$excel.visible=$true
$wrkbook = $excel.Workbooks.Add()
$wrksheet = $wrkbook.Worksheets.Item(1)
$file = ("link")
$icon =$wrksheet.OLEObjects().Add($missing,$file,$false,$false,$missing,$missing,$missing,$missing,$missing,$missing,$missing)
I have built a powershell object in a script. In this same script I need to take that object (or parts of it) and load them into an Excel spreadsheet. The excel spreadsheet is already saved and has headers. I am able to find my .xlsx file, open it, save and close but not sure how to actually get my object into the file. This script will be run a lot so ultimately I need the data to just append to the first available/empty row.
Here is the script that is opening the excel file:
$excel_file_path = 'C:\Users\ME\Documents\Test\SFTPTest.xlsx'
# Instantiate the COM Object
$Excel = New-Object -ComObject Excel.Application
$Excel.Application.Visible = $true
$Excel.DisplayAlerts = $false
$ExcelWorkBook = $Excel.Workbooks.Open($excel_file_path)
$ExcelWorkSheet = $Excel.Worksheets.item("SFTPTransfers")
$ExcelWorksheet.activate() | Out-Null
The headers in my excel spreadsheet are on row 1 and there are 9 columns (A-I)
My PS Object is a large oject converted from a JSON object. I only need 9 properties of this object to go into this spreadsheet. For example, I have a property called Object1.ServerName that needs to go into the my excel file under ServerName column...etc.
I'm currently trying to grab a specific worksheet from an Excel file in PowerShell and I'm receiving the error "You cannot call a method on a null-valued expression". PowerShell picks up the workbook and even that the property Worksheet exists, yet it does not recognize item(1).
$excel = "C:\budget.xlsx"
$ex = New-Object -ComObject Excel.Application
$ex.Visible = $true
$wb = $ex.Workbooks.Open($excel)
$ws = $wb.Worksheets.item(1) # This line errors
When accessing Excel worksheets, this is the syntax (for instance, see this code, and this is just one of many examples). Is there something else that needs to be loaded in order to do this? After searching the 'net, I can't find anyone else loading or using another syntax.