I am working on a script to create a PDF from PowerShell. I have it working using Word, using the ComObject. But that means that computer I run it on has to have Word. I was wandering if there was a way to make a PDF file from notepad maybe. This is the code I have making the PDF from Word.
<`#make pdf
# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0
# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false
# Add a Word document
$doc = $word.documents.add()
# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
foreach($line in $txt){
$selection.typeText($line) | Format-wide
$selection.typeparagraph()
}
# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
if($?){write-host 'Users and Groups saved to ' $pdfPath -ForegroundColor Cyan}
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()`>
Related
I have script that updates query in excel file
$filePath = "C:\Scripts\SheetToRefresh.xlsx"
$excelObj = New-Object -ComObject Excel.Application
$excelObj.Visible = $true
$workBook = $excelObj.Workbooks.Open($filePath)
$workSheet = $workBook.Sheets.Item("Data")
$workSheet.Select()
$workBook.RefreshAll()
$workBook.Save()
Original script comes from here
Now i need to loop it wihtin folder, i came up with:
$files = Get-ChildItem "C:\path" -Filter *.xlsx
foreach ($f in $files){
}
but struggling with changing filename for each file.(newbie with ps)
Let's break down what needs to happen:
Before:
Open Excel
Enumerate files
During, for each file:
Open workbook
Run the relevant part of your existing script
Save and close workbook
After:
Close Excel
So, let's start by moving the "Before" actions to the top of your new script:
# Open Excel
$excelObj = New-Object -ComObject Excel.Application
$excelObj.Visible = $true
# Enumerate files
$files = Get-ChildItem "C:\path" -Filter *.xlsx
Now we need to move the relevant parts of the existing script into the new loop. To get the full path of the file object returned by Get-ChildItem, use the FullName property:
foreach($file in $files){
# Open workbook from $file
$workBook = $excelObj.Workbooks.Open($file.FullName)
# Refresh query results
$workSheet = $workBook.Sheets.Item("Data")
$workSheet.Select()
$workBook.RefreshAll()
# Save updated workbook to file
$workBook.Save()
# Close workbook
$workBook.Close()
}
And finally we just need to quit Excel:
$excelObj.Quit()
sample excel image I need export data from a column in excel and exclude some characters then export to a txt file.(excel Sample attached). Basically I need to extract ONLY names in the Orders column and output to a text file, here is what I have so far:
#Specify the Sheet name
$SheetName = "Today Order"
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'Change Auditor Events'
$WorkSheet = $WorkBook.sheets.item($SheetName)
#====
I can use the replace command below to trim off unneeded characters in the Orders column, I only need the names
-replace "order from " -replace " California"
How can I assign variable to the orders column and process each line then use the out-file to export? Or do you have any other good suggestion to do this.
Thanks in Advance.
I assumed your data is in column A. Correct as needed.
I used regex to pull the name out from your sentence. -Match writes to the magic variable "$matches"
It's worth mentioning that using COM objects is the "hard" way to do this.
The very easy way is saving as csv.
The easy way is using a module that handles .xlsx files.
#Specify the Sheet name
$SheetName = "Today Order"
$FilePath = "C:\whatever.xlsx"
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
# Open the Excel file and save it in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load the WorkSheet 'Change Auditor Events'
$WorkSheet = $WorkBook.sheets.item($SheetName)
$MyData = [System.Collections.Generic.List[string]]::new() #Potentially better performance than immutable array if you're running this on thousands of rows.
for($i = 2; $i -le $WorkSheet.UsedRange.Rows.Count; $i++) {
($Worksheet.Range("a$i").text) -match 'from (?<name>.+) in'
$MyData.Add($Matches.name)
}
$MyData | Out-File "C:\output.txt"
My PowerShell script opens the My_Paper.doc file, applies font, and then saves it as My_Paper.docx.
The issue is the docx is being saved in the Documents folder instead of the current destination, which is C:\Place\.
What is the fix for this?
$Filename = 'C:\Place\My_Paper.doc'
$Word = New-Object -ComObject Word.Application
$Word.Visible = $False #$True makes Word Open
$Document = $Word.Documents.Open($Filename)
$Selection = $word.Selection
$Document.Select()
$Selection.Font.Name = "Calibri"
$Selection.Font.Size = 12
# This saves to the Documents folder
$Report = 'My_Paper.docx'
$Document.SaveAs([ref]$Report, [ref]$SaveFormat::wdFormatDocument)
$Document.Close()
$Word.Quit()
The Word COM object uses a different working directory than your PowerShell script, so you need to specify the full path if you want to save the file elsewhere. Use the automatic variable $PWD for getting the path to the current working directory of your script.
$Report = Join-Path $PWD.Path 'My_Paper.docx'
I have to examine all of .docx files in a folder and i have to display the name of files which is contain that word I added as param. How can I do it in powershell?
try someting like this:
#Instance of word
$Word=NEW-Object –comobject Word.Application
$Word.visible = $False
#take list of .docx
Get-ChildItem "c:\temp" -file -Filter "*.docx" | %{
$Filename=$_.FullName
#open file and take content of word file
$Document=$Word.documents.open($Filename, $false, $true)
$range = $document.content
#if content have your word, print path of word file
If($range.Text -like "*tot*"){
$Filename
}
$word.Documents.Close($false)
}
Am trying to automate certain tasks that I have to do, that although simple are tedious, due to the number of files. I currently have a script that will refresh every file within a folder, now these files have more worksheets that what my client needs, so after refreshing, I need to copy/paste the first two sheets in a new workbook, save in a general location where the client pick's it up. I have added what I thought was good code to do this copy/paste, but unfortunately, I'm getting errors in the copy/paste section as well as the SaveAs part. I did some research here and at "powershell.org", but couldn't find anything that helped :(.
This is my code:
Measure-Command {
$excel = new-object -comobject excel.application
$excel.DisplayAlerts = $false
$excelFiles = Get-ChildItem -Path "Network folder location" -Include *.xls, *.xlsm,*.xlsx, *.lnk -Recurse
Foreach($file in $excelFiles) {
$workbook = $excel.workbooks.open($file.fullname)
foreach ($Conn in $workbook.Connections){
$Conn.OLEDBConnection.BackgroundQuery = $false
$Conn.refresh()
}
$workBook.RefreshAll()
$workbook.save()
$wb2 = $excel.Workbooks.Add()
$sheetToCopy = $workbook.sheets.item(1),$workbook.sheets.item(2) #Source
$sheetToCopy.CopyTo($wb2) #Destination
$filename = $wb2.Sheets.Item(2).Cells.Item(4,2) #Destination file, 2nd sheet, column D row 2 has what I want to call the file (RVP John Doe - Dashboard)
$wb2.SavesAs("Networkfolder\$filename.xlsx")
$workbook.close()
$wb2.close()
}
$excel.quit()
}