0

In regard to nixda's answer in How can I set Excel to always import all columns of CSV files as Text?, how can I set the workbook name in PS when I am using this script (PowerShell script to open CSVs directly from Windows Explorer) to open a text or csv file? Using the script provided by nixda, the workbook name is book1 (the default).

I can't seem to find a good reference that discusses workbook naming in PowerShell.

  • I was hoping for a workbook.name function to set it without saving it so I can use the linked ps script to import text and use an existing macro on the open file. If I open the file in excel directly it retains the name of the file, using the linked ps script it gets changed to book1. I guess I Should be been more specific about that. I will check the link you sent (I may have installed it already in my travails looking for answers). – Jurro Aug 31 '17 at 1:18
2

EDIT: I have clearly misunderstood your question. You want to change workbook name. I think that workbook name is set when saved. You can use ComObjects SaveAs() method:

$workbook.SaveAs("C:\Users\user\Desktop\TEST.xlsx")

By inspecting Excel.Application com object which was used in mentioned answer, it seems you can set the Excel sheet name with following code:

$excel = new-object -ComObject excel.application
$workbook = $excel.Workbooks.Add()
$workbook.ActiveSheet.Name = "TEST"

When you check it by changing Visible property from false to true:

$excel.Visible = $true

Name should change.


Additionaly, you can check Doug Finke's ImportExcel Powershell module, it provides very nice way of manipulating excel files (only new xlsx format)

https://github.com/dfinke/ImportExcel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.