none
Unrelated post RRS feed

  • Question

  • I am having same issue. I am using a different code though, which converts text to word. somehow, when the text files are converted, it automatically takes font size 10.5 courier new, by default. i want font size 8. i tried to use your soulution but not working in this code. would you please help me figuring out what I am doing wrong. thank you in advance.

    [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
    						$wdDocument.Select()
    						$wdSelection.font.size =8
    						$wdSelection.Font.Name = "Courier New"
    						$wdSelection.Pagesetup.TopMargin = 50
    						$wdSelection.Pagesetup.LeftMargin = 20
    						$wdSelection.Pagesetup.RightMargin = 20
    						$wdSelection.Pagesetup.BottomMargin = 50 
    						$wdDocument.Select()
    						$wdSelection.font.size =8
    						$wdSelection.Font.Name = "Courier New"
    						$wdSelection.ParagraphFormat.SpaceBefore = 0
    						$wdSelection.ParagraphFormat.SpaceAfter = 0 
    						$wdSelection.PageSetup.Orientation = [Microsoft.Office.Interop.Word.WdOrientation]::wdOrientLandscape
    						#Inserting content of txt file
    						$wdSelection.InsertFile($txtFilePath)
    						$wdSelection.font.size =8
    						$wdSelection.Font.Name = "Courier New"
    						
    						#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."
    		}
    	}
    }

     
    • Edited by connectme01 Sunday, August 13, 2017 6:39 AM
    • Split by jrv Sunday, August 13, 2017 2:59 PM unrelated post
    Sunday, August 13, 2017 6:37 AM