I am trying to add a button to an Excel worksheet in PowerShell as part of a bigger already coded and working script.
The button is not part of the Excel.Application COM object instead it is in Microsoft.Office.Tools.Excel namespace.
I've created the following code but it gives a TypeNot found error on the last line when run.
$excel = New-Object -ComObject "Excel.Application"
$excel.visible = $True
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.worksheets.item(1)
$Range1 = $worksheet.Range("A1", "A1")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Tools.Excel")
$Btn = [Microsoft.Office.Tools.Excel]::AddButton( $worksheet, $Range1, "Test")
What am I doing wrong?
Thanks,
Dog
Related
Currently working on a robot where I want to download an excel file from a system, copy paste the contents of the excel file to a word file as an embedded excel table, but the table is wider than the page (it is already in landscape mode). I am using a powershell script in DAS to execute this step. To solve the problem normally, I would simply right-click on the table and use Autofit -> Fit to Window. How do I do this in powershell?
My script is currently like this:
$word = new-object -comobject Word.application
$word.visible = $true
$doc1 = $word.documents.open($destination)
$bookmark1 = $doc1.Bookmarks.Item("FacilitySheet")
$xl = New-Object -comobject Excel.Application
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Open("C:\Users\Pater\Downloads\spreadsheet.xlsx")
$ws = $wb.ActiveSheet
$Range1 = $ws.UsedRange.Cells
$RowCount = $Range1.rows.count
$CopyRange = $ws.Range("A1:O$RowCount").Copy()
$bookmark1.Range.Paste()
To solve this you must set the AutoFitBehavior to the desired value. Please check https://learn.microsoft.com/en-us/office/vba/api/word.table.autofitbehavior for the allowed values and take into account that the values wdAutoFitContent, etc may not be accessed from Powershell, so you must set the raw values (1 for wdAutoFitContent, for example)
$word = new-object -comobject Word.application
$word.visible = $true
$doc1 = $word.documents.open($destination)
$bookmark1 = $doc1.Bookmarks.Item("FacilitySheet")
$xl = New-Object -comobject Excel.Application
$xl.Visible = $true
$xl.DisplayAlerts = $False
$wb = $xl.Workbooks.Open("C:\Users\Pater\Downloads\spreadsheet.xlsx")
$ws = $wb.ActiveSheet
$Range1 = $ws.UsedRange.Cells
$RowCount = $Range1.rows.count
$CopyRange = $ws.Range("A1:O$RowCount").Copy()
$bookmark1.Range.Paste()
$bookmark1.Range.AutoFitBehavior(1)
I am using the following code to paste values from one sheet to another. It used to work but after migrating to EXCEL 2013 stopped working with error "Unable to get the Paste property of the Worksheet class"
$excelFrom = New-Object -ComObject Excel.Application
$excelFrom.visible = $false
$excelFrom.DisplayAlerts = $false
$excelTo = New-Object -ComObject Excel.Application
$excelTo.visible = $false
$excelTo.DisplayAlerts = $false
$excelFrom.Workbooks.OpenText("inputfile.xls")
$worksheetFrom = $excelFrom.Worksheets.Item(1)
$excelTo.Workbooks.OpenText("templatefile.xlsx")
$worksheetTo = $excelTo.Worksheets.Item(1)
write-host $worksheetTo.cells[1][2].text
$lastRow1 = $worksheetFrom.UsedRange.rows.count - 11
$lastRow2 = $lastRow1 + 1
#copy account numbers
$worksheetFrom.activate()
$range1 = $worksheetFrom.Range("B15:B17")
$range1.copy()
$worksheetTo.activate()
$range2 = $worksheetTo.Range("A2:A4")
write-host $range2.rows.count
$worksheetTo.Paste($range2)
$excelFrom.quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelFrom)){}
Remove-Variable excelFrom
$excelTo.quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelTo)){}
Remove-Variable excelTo
If it helps anyone I had to use this -
$worksheetTo.Range("A2:A$lastRow2").Value2 = $worksheetFrom.Range("B15:B$lastRow1").Value2
It seems .Value doesn't work if we are copying from an xls file to xlsx –
I am currently trying to take a csv file generated from a VBScript, and use powershell to convert it to an xls then save it. (Using it as an offline Database file) Problem, is that one column (Column E) is for SKU's and has leading 0's that get lost during the translation. I am getting errors when trying the following. Powershell is new to me, so I could be making a simple mistake:
$xl = new-object -comobject excel.application
$xl.visible = $true
$Workbook = $xl.workbooks.open("file location.csv")
$Worksheets = $Workbooks.worksheets
$Worksheets.Columns.("E").NumberFormat = "00000000000"
$Workbook.SaveAs("file location.xls",1)
$Workbook.Saved = $True
$xl.Quit()
EDIT: I got it to work! Here's the following, if anyone has any pointers:
$excel = new-object -comobject excel.application
$excel.visible = $true
$Workbook = $excel.workbooks.open("file location.csv")
$Worksheets = $Workbooks.worksheets
$Worksheet = $Workbook.Worksheets.Item(1)
$Range = $Excel.Range("E:E").EntireColumn
$Range.NumberFormat = "00000000000"
$Workbook.SaveAs("file location.xls",1)
$Workbook.Saved = $True
$excel.Quit()
You can achieve the same thing with changing
$Worksheets.Columns.("E").NumberFormat = "00000000000"F15
to
$workbook.ActiveSheet.Columns.Item("E").NumberFormat = "00000000000"
from the first example to have less code.
Good Evening everyone,
I have a problem that I am having some issues with and I really need some help. I took two csv files and compared them and converted them to an xls. Now the part I am confused about is how will I be able to take the hyperlinks from Column 1, Row 1 in one excel document and embed them into the text in the other document Column 1, Row2.
is there an easy way to do this? I found the follow link which left me a little confused : https://social.technet.microsoft.com/Forums/scriptcenter/en-US/123d673a-f9a7-4ae6-ae9c-d4ae8ef65015/powershell-excel-how-do-i-create-a-hyperlink-to-a-cell-in-another-sheet-of-the-document?forum=ITCG
I appreciate any guidance and help you can offer.
#Define the file path and sheet name
$FilePath= `enter
code"C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.csv"
$FilePath2="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls"
$FilePath3="C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test4.xls"
$SheetName="Test3"
$SheetName2="HyperLinks"
#Compare two CSV files to look for matches
$CSV1 = import-csv -path
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test1.csv
$CSV2 = import-csv -path
C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test2.csv
Compare-Object $CSV1 $CSV2 -property ShoppingList -IncludeEqual | where-
object {$_.SideIndicator -eq "=="}
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Enable the 'visible' property so the document will open in excel
$objExcel.Visible = $true
$objExcel.DisplayAlerts = $False
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet "Test3"
$WorkSheet = $WorkBook.sheets.item($SheetName)
# Delete data from column
[void]$WorkSheet.Cells.Item(1,2).EntireColumn.Delete()
#Auto fit everything so it looks better
$usedRange = $WorkSheet.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null
#Save and convert to XLS
$Workbook.SaveAs("C:\Users\cobre\Desktop\PowerShell\HomeWork2\Test3.xls",1)
$Workbook.Saved = $True
#Load
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $objExcel.Workbooks.Add()
$workbook.Worksheets.Item($FilePath2).Hyperlinks.Add( `
$workbook.Worksheets.Item($FilePath2).Cells.Item(1,2) , `
"" , $FilePath3, "https://community.spiceworks.com/topic/673034-powers
You can use something like this:
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $excel.Workbooks.Add()
$workbook.Worksheets.Item(1).Hyperlinks.Add($workbook.Worksheets.Item(1).Cells.Item(1,1) ,"" , "Sheet2!C4", "", "Link to sheet2")
Reference : Hyperlinks.Add Method
Hope it helps
I am new to Powershell and I need to parse text file into excel and edit content.
I managed to open file with the following script:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel.DisplayAlerts = $false
$Excel.Workbooks.OpenText( "Documents\myfile.txt")
It runs without exception, but the data I'm left with tells me the file is not loaded properly.
I need to set parameters like FieldInfo for export.
How do I do this?
Thanks for help.
You have to get the item from the file in order to load it in excel.
DO like this:
$excel = new-object -comobject excel.application
$file = get-item "E:\myfile.txt" # Mention the path of the file
$excel.Visible=$true
$excel.displayalerts = $False
$wb = $excel.workbooks.open($file)
Hope it helps you.
When you bring data into Excel via text file or manual input, Excel will try to interpret what type of data it is, text, date or number. You are bringing in text that looks like a number to Excel.
One solution is to make Powershell do all the work, get it to load the file and then loop through each row. You can then force the format before loading the data.
Something like this:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$wb = $Excel.Workbooks.add()
$ws = $wb.Worksheets[1]
Import-Csv "Documents\myfile.txt" | % {$i = 1}{
$ws.Range("A" + $i).NumberFormat = '#'
$ws.Range("A" + $i).Value = $_.Field1
#Repeat for each field
$i++
)