Start Excel with command line and parameters - excel

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")

Related

Powershell Run Excel Macros

hello im trying to open an excel document then open an excel Macro Document, then have powershell run the specific macro that i want and let the macro do its magic and call it a day.
the script i have is this:
# start Excel
$excel = New-Object -comobject Excel.Application
#open file
$FilePath = 'C:\Users\Username\Desktop\ExcelWorkbook.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)
#make it visible (just to check what is happening)
$excel.Visible = $true
#access the Application object and run a macro
$app = $excel.Application
$app.Run("Macro")
$excel.Quit()
#Popup box to show completion - you would remove this if using task scheduler
$wshell = New-Object -ComObject Wscript.Shell $wshell.Popup("Operation Completed",0,"Done",0x1)
So my issue is im getting the error "all macros may be disabled"
what code do i use to make them enabled, i'm having issues with that.
$app it's not defined, so try to replace $app.Run("Macro") with $Excel.Run("Macro")

Workbook.SaveAs method is popping Save As dialog box

This was working fine until this weekend, now it's stopped..
I have a powershell script that opens an Excel file as read only, copies a sheet from that file into a new file, then saves the new file to a different location. The script still does all of that, gets to the very end, saves the file to the new location, but now it is opening a "Save as" dialog box, which prevents the script from ever completing. Something change with Powershell? Or the SaveAs method?
# Specify file names/paths
$sourceFile = "C:\source.xlsx"
$exportFile = "C:\export.xlsx"
# Open Excel, hidden/suppress alerts
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $false
$Excel.DisplayAlerts = $false
# Open the source workbook in read-only mode
$sourceWorkbook = $Excel.Workbooks.Open($sourceFile,$null,$true)
# Create a new/blank workbook
$newWorkbook = $Excel.Workbooks.Add()
# Copy sheet from source workbook to new workbook
$sourceWorkbook.Sheets("SheetName").Copy($newWorkbook.Sheets[1])
# Save the new workbook
$newWorkbook.SaveAs($exportFile)

How do I prevent Excel from attempting to connect to its links when opening a file with links in Powershell?

I have a PS script that runs from the Task Scheduler. When a .xlsm file is added to a watched folder it opens this file and gathers specific data from it, then outputs it to a CSV. The problem I've been having recently is with XLSM files that have links to an external SharePoint site. Any time these files are opened with the script it just hangs. If I try to open the file manually Excel first asks me to "Enable Content", then once I click to enable a prompt to input my credentials to connect to the SharePoint site associated with the link.
I can confirm after breaking the link and then running the script that the problem is resolved, so it is definitely this link that is hanging the script up. I've tried looking into methods to break the link before opening the file, but I was not able to gather much. All the resources I could find were in references to updating links via Powershell, not breaking them.
Here are the pieces of my code related to opening the file:
$watchedfolder = "C:\Watched"
$filedirectory = Get-ChildItem $watchedfolder | Where-Object {($_.Extension -eq ".xlsm")} | Select-Object -ExpandProperty Name
foreach ($file in $filedirectory){
$sheetName = "Daily Dash"
#OPEN EXCEL WORKBOOK
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open("C:\Watched\$file")
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
$rowMax = ($sheet.UsedRange.Rows).count
I am unsure of what I can add to this opening portion of my script that will prevent the attempt to connect to the SharePoint site. Any recommendations?
I cannot test this at the moment, but you need to do the settings for the Excel object before opening the file.
Setting the AskToUpdateLinks property to $false should do what you ask for.
#OPEN EXCEL WORKBOOK
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
$objExcel.AskToUpdateLinks = $false
$workbook = $objExcel.Workbooks.Open("C:\Watched\$file")
$sheet = $workbook.Worksheets.Item($sheetName)
$rowMax = ($sheet.UsedRange.Rows).count

PowerShell + Excel.Application add sheets problem

I am trying to implement a script to update some Excel sheets/cells.
For this I am using Windows PowerShell ISE with the following code:
# open application
$document = New-Object -ComObject excel.application
$document.Application.Visible = $true
$document.DisplayAlerts = $false
# Create workbook
$workbook = $document.Workbooks.Add()
After this, I can use the following command to see which sheets I have:
$workbook.Sheets | Select-Object -Property Name
And it works perfectly. The problem is when I add a new sheet with the following:
$workbook = $document.Sheets.Add()
It creates the new sheet, but… when I use the command to see the sheet names, it does not show anything, looks like the Sheets.Add() crashes something…
Can someone help me with this topic? Am I doing something wrong?

When embedding an OLEObject (Word/XLS/PDF doc) into Excel via powershell, unable to save that

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)

Resources