Skip to content

Instantly share code, notes, and snippets.

@justinloring
Created April 27, 2020 17:54
Show Gist options
  • Save justinloring/4baebea4aec613e30ed9870e540e28b7 to your computer and use it in GitHub Desktop.
Save justinloring/4baebea4aec613e30ed9870e540e28b7 to your computer and use it in GitHub Desktop.
Convert TXT to DOCX
Function ConvertTo-OSCWord
{
<#
.SYNOPSIS
ConvertTo-OSCWord is an advanced function which can be used to covert txt file to Word document file.
.DESCRIPTION
ConvertTo-OSCWord is an advanced function which can be used to covert txt file to Word document file.
.PARAMETER Path
Specifies the path of Word document.
.EXAMPLE
C:\PS> ConvertTo-OSCWord -Path C:\TXTFile
File Name Convert to Word
--------- ---------------
Microsoft1.txt Finished
Microsoft2.txt Finished
Microsoft3.txt Finished
Microsoft4.txt Finished
.EXAMPLE
C:\PS> ConvertTo-OSCWord -Path C:\TXTFile\Microsoft1.txt
File Name Convert to Word
--------- ---------------
Microsoft1.txt Finished
#>
[CmdletBinding(SupportsShouldProcess = $true)]
Param
(
[Parameter(Mandatory = $true)]
[Alias('p')]
[String]$Path
)
If ($PSCmdlet.ShouldProcess("Convert txt file to Word Document file."))
{
If (Test-Path -Path $Path)
{
#get all related to txt files
$txtFiles = Get-ChildItem -Path $Path -Recurse -Include *.txt
If ($txtFiles)
{
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$wdApplication = New-Object -ComObject "Word.Application"
Foreach ($txtFile in $txtFiles)
{
$txtFileName = $txtFile.Name #get the file name
$txtFileDirectory = $txtFile.DirectoryName #get the directory of file
$txtFileBaseName = $txtFile.BaseName
$txtFilePath = $txtFile.FullName
$Objs = @()
#get the number of txt file(s)
$txtFileCounts = $txtFiles.Count
If (!$txtFileCounts)
{
$txtFileCounts = 1
}
$intNumberTXT++
Try
{
#Displays a progress bar within a Windows PowerShell command window.
Write-Progress -Activity "Converting txt file [$wdDocumentName] to Word Document" `
-Status "$intNumberTXT of $txtFileCounts TXT File(s)" -PercentComplete $($intNumberTXT/$txtFileCounts * 100)
#Open the Word document
$wdDocument = $wdApplication.Documents.Add()
$wdSelection = $wdApplication.Selection
#Inserting content of txt file
$wdSelection.InsertFile($txtFilePath)
#Save as word document
$wdDocument.SaveAs([REF]"$txtFileDirectory\$txtFileBaseName")
$wdDocument.Close()
$Properties = @{
'File Name' = $txtFileName
'Convert to Word' = If (Test-Path -Path "$txtFileDirectory\$txtFileBaseName.docx")
{ "Finished" }
Else
{ "Unfinished" }
}
$objWord = New-Object -TypeName PSObject -Property $Properties
$Objs += $objWord
$Objs
}
Catch
{
Write-Warning "'$txtFileName' cannot convert to word document."
}
}
######release the object######
$wdApplication.Quit()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($wdApplication)
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
Else
{
Write-Warning "Please make sure that at least one TXT file in the '$Path'."
}
}
Else
{
Write-Warning "The path does not exist, plese input the correct path."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment