I have a powershell script to create a pdf from a docx. I can call it manually an it works fine:
param(
[string]$File, # Path to document
[string]$DestinationFolder, # Path of folder
[string]$NewName # New name for converted file
)
# ======== check if parameter are valid ========
if (-not $File) {
Write-Error "Parameter '-File' must not be empty"
exit 1
}
if (-not $DestinationFolder) {
$DestinationFolder = Split-Path -Path $File
}
if (-not $NewName) {
$NewName = [System.IO.Path]::GetFileNameWithoutExtension($File) + ".pdf"
}
# ======== create destination folder if not exists ========
if (-not (Test-Path -Path $DestinationFolder)) {
$null = New-Item -Path $DestinationFolder -ItemType Directory -Force
Write-Output "Path did not exist. Created folder: $DestinationFolder"
}
# ======== export pdf ========
Write-Output "Export pdf"
$Word = New-Object -ComObject 'Word.Application'
$Document = $Word.Documents.Open($File)
$NewFilePath = [string](Join-Path $DestinationFolder $NewName)
$Document.SaveAs($NewFilePath, 17)
$Document.Close()
$Word.Quit()
Write-Output "Stored to $NewFilePath"
but if I call it from a GitLab pipeline, I get the following exception at the line $Document.SaveAs($NewFilePath, 17):
At D:\GitLab-Runner\builds\yxFfC2Egu\0\SPS_Entwicklung\test\ci-dev-test\ci\release\docx_to_pdf.ps1
:44 char:5
+ $Document.SaveAs($NewFilePath, 17)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At D:\GitLab-Runner\builds\yxFfC2Egu\0\SPS_Entwicklung\test\ci-dev-test\ci\release\docx_to_pdf.ps1
:45 char:5
+ $Document.Close()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The script does not finish, but word takes more and more memory. I can only stop it by ending the task with the task manager.
- The gitlab runner service is started with an admin user
- I tried to execute the script manually with the same user, and it worked fine
- I tried the same thing with python and had a similar error
ValueError('NULL COM pointer access',), tried this -> same error
[System.Runtime.Interopservices.Marshal]::ReleaseComObject(). No wonder Word is gobbling up your memory