1

I'd like to make power automate desktop flow for making PDF from Image. I can't find direct method from image to PDF, so, I tried to make new word document and insert image. After that I tried to convert document to PDF with below code. However it doesn't work. Can you please advise what is wrong?

(Error) You cannot call a method on a null-valued expression. At C:\Users\m\AppData\Local\Temp\Robin\3q3a3wvbqx1.tmp.ps1:33 char:2

  • $ObjectSelection.InlineShapes.AddPicture($ImageName)
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

I've tried Power automate desktop Code

Display.SelectFileDialog.SelectFile Title: $'''Select file''' IsTopMost: False CheckIfFileExists: False SelectedFile=> SelectedFile ButtonPressed=> ButtonPressed
Scripting.RunPowershellScript Script: $'''#############################################
# Doc2PDF                                   #
# Created: April 30, 2016                   #
# Last Modified: June 16, 2016              #
# Version: 1.0                              #
# Supported Office: 2010*, 2013, 2016       #
# Supported PowerShell: 4, 5                #
# Copyright © 2016 Erick Scott Johnson      #
# All rights reserved.                      #
#############################################
#Input
$Input = $args[0]

#Define Office Formats
$Wrd_Array = \'*.docx\', \'*.doc\', \'*.odt\', \'*.rtf\', \'*.txt\', \'*.wpd\'
$Img_Array = \'*.png\', \'*.jpg\', \'*.bmp\'
$Off_Array = $Wrd_Array + $Img_Array
$ExtChk    = [System.IO.Path]::GetExtension($Input)

#Convert Image to PDF
Function Img-PDF($f, $p)
{
    $Wrd = New-Object -ComObject word.application
    $Wrd.Visible = $False
    $Version     = $Wrd.Version
    $Doc    = $Wrd.Documents.add()
    $objSelection  = $Wrd.Selection
    $ImageName =\'%SelectedFile.FullName%\'
    $ObjectSelection.InlineShapes.AddPicture($ImageName)
        

    If ($Version -eq \'16.0\' -Or $Version -eq \'15.0\') {
        $Doc.SaveAs($p, 17) 
        $Doc.Close($False)
    }
    ElseIf ($Version -eq \'14.0\') {
        $Doc.SaveAs([ref] $p,[ref] 17)
        $Doc.Close([ref]$False)
    }
    [gc]::Collect()
    [gc]::WaitForPendingFinalizers()
    $Wrd.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Wrd)
    Remove-Variable Wrd
}
        


#Check for Image Formats
Function Img-Chk($f, $e, $p){
    $f = [string]$f
    For ($i = 0; $i -le $Img_Array.Length; $i++) {
        $Temp = [string]$Img_Array[$i]
        $Temp = $Temp.TrimStart(\'*\')
        If ($e -eq $Temp) {
            Img-PDF $f $p
        }
    }
}

#Check for Word Formats
Function Wrd-Chk($f, $e, $p){
    $f = [string]$f
    For ($i = 0; $i -le $Wrd_Array.Length; $i++) {
        $Temp = [string]$Wrd_Array[$i]
        $Temp = $Temp.TrimStart(\'*\')
        If ($e -eq $Temp) {
            Wrd-PDF $f $p
        }
    }
}



    $File     = \'%SelectedFile.fullname%\'
    $Path     = [System.IO.Path]::GetDirectoryName($File)
    $Filename = [System.IO.Path]::GetFileNameWithoutExtension($File)
    $Ext      = [System.IO.Path]::GetExtension($File)
    $PDF      = $Path + \'\\\' + $Filename + \'.pdf\'
    Wrd-Chk $File $Ext $PDF
    Img-Chk $File $Ext $PDF


#Cleanup
Remove-Item Function:Img-PDF, Function:Img-Chk
Remove-Item Function:Wrd-PDF, Function:Wrd-Chk
''' ScriptOutput=> PowerSheelOutput ScriptError=> ScriptError

CodingBaby
  • 25
  • 6

0 Answers0