Script to convert text to WORD produces empty document

I’ve never used Powershell until now.
I downloaded a script from Sergei Dorogin’s technical blog .It’s supposed to create a WORD document from a .txt file. But the output is a single blank page. I see that the forum new topic process inserted a link that points to the script, so I guess I don’t need to copy it into this Topic. I made no changes to the code

Can someone point me to the place in the code that might explain wy the output is blank? I prefer to troubleshoot it myself rather than have someone do it for me.

TIA. AintCMD. Happy to be here. :smiley:

Phil,
Welcome to the forum. :wave:t4:

First of all - the link does point to a script. :wink:

If you have a question about a particular script and you know the author - why not asking the author? If there’s an issue with his code he will probably be thankful if you notice him about.

Here’s the script. As I said previously I don’t want someone to just hand me the solution without me doing any work on my part. I just want a clue as to where to look within the script. Is there a way to step through the execution one line at a time?

Param(
	[String]$basePath,
	[String[]]$Include=@("*.cs"),
	[string[]]$ignoreFiles=@(),
	[string[]]$ignorePath=@()
)

function Resolve-RelativePath($path, $fromPath) {
	$path = Resolve-Path $path
	$fromPath = Resolve-Path $fromPath
	$fromUri = new-object -TypeName System.Uri -ArgumentList "$fromPath\"
	$pathUri = new-object -TypeName System.Uri -ArgumentList $path
	return $fromUri.MakeRelativeUri($pathUri).ToString().Replace('/', [System.IO.Path]::DirectorySeparatorChar);
}

$word = New-Object -ComObject word.application
$word.Visible = $false
$doc = $word.documents.add()
$doc.Styles["Normal"].ParagraphFormat.SpaceAfter = 0
$doc.Styles["Normal"].ParagraphFormat.SpaceBefore = 0
$margin = 36 # 1.26 cm
$doc.PageSetup.LeftMargin = $margin
$doc.PageSetup.RightMargin = $margin
$doc.PageSetup.TopMargin = $margin
$doc.PageSetup.BottomMargin = $margin
$selection = $word.Selection


Get-ChildItem -Path $basePath -r -Include $Include | ForEach-Object {
	$filePath = $_.fullname
	$relativePath = Resolve-RelativePath $filePath $basePath
	$fileName = $_.name

	# Fitler out files
	if ($ignoreFiles -contains $fileName) {
		Write-Host "Skipping file $fileName" -ForegroundColor DarkGray
		return
	}
	if (($ignorePath | where { $relativePath -like $_}).Length -gt 0) {
		Write-Host "Skipping path $filePath" -ForegroundColor DarkGray
		return
	}

	$temp = Get-Content $filePath -Encoding UTF8 -Raw

	$selection.Style = "Heading 1"
	$selection.TypeText($relativePath)
	$selection.TypeParagraph()
	$selection.Style = "Normal"
	$selection.TypeText($temp);
	$selection.TypeParagraph()
}

$outputPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$outputPath = $outputPath + "\sources.docx"
$doc.SaveAs($outputPath)
$doc.Close()
$word.Quit()

I would have checked with the author but I don’t see an option there to reply.
Thanks. AintCMD.

First thing I’d do is simplify the file path magic work. If you know the path, remove all that and just pass the exact path in. Same with all the include/exclude logic, if you handle that prior to calling the function, and only call the function with files you really want to convert, you remove tons of unnecessary complexity.