Replace matched text in Word by a picture

Hello everyone,

I have a Powershell script that creates the Outlook signatures automatically retrieving information on AD.

I am having a little trouble (I said I’m no expert in Powershell)

My company has two logos, and here’s how I’d like to do (instead of two scripts and two docx)

Without “Comapny” the AD says I Logo1 Logo2 or and in this, I would like an image is placed at the point the text is placed in my template docx.

So I created two variables $ Logo1 and Logo2 $ with the path of the two images. Unfortunately when I do a “ReplaceText” by these two variables, I have the path that appears and not the logo …

Someone could give me a hand please? :slight_smile:

I do not know how to search images in powershell and I have not found much help on the web.

Thank you in advance !

PS. : Sorry for my bad english :s

Can you post the code you’re using? It would really help if we could see the script you’re using.
It sounds like you might simply be inserting the text rather than a picture. To insert a picture to a docx with the Word.Application COM object you would use:


Where $word references an instance of the Word.Application COM object.


You can find my script on this URL : #Custom variables $SignatureName = 'SFR'... - TextUp
I test lot of code, so, all test for the logo is realy bad.


So it looks like you are just replacing “Company” with the string that points to the picture rather than inserting the picture in the document.

You’ll need to modify lines 88-96 to something like:

If ($ADCompany = "SFR") {

Else {

You may well need to play with the formatting to get the picture exactly where you want it and, of course, you’ll still need to replace the “Company” text so that “Company” doesn’t appear in the signature.

Full documentation for the AddPicture() method is on MSDN.


Thanks for your help !

Now I have the image that is added! By cons, when I put anything in “Company”, the logo 2 does not turn, it is still the logo 1 …
Image more goes anywhere … I’ll have to play with pixels to place it correctly?

And I also have to adapt the code for “Company” disparaise signature … I do not know how to do this see my little level in Powershell, but I will try!

If you have other tips, feel free :slight_smile:
Thank you !

EDIT : f I reverse the two ($ SFR SFR and $ BUSINESS) but in Company I still “SFR” and not “SFRB” (or something to insert the logo 2, I what happens, the IF logo that appears first!
So in there a little mistake … HELP :frowning:
EDIT 2 : I think i can just remove “Company” text from Word, i just see i dont use him with the script … I want just now to know ho deplace a picture and why the 2nd logo dont work

If logo 2 isn’t appearing then I presume everyone you’re testing against is matching the first condition of your IF ELSE statement. i.e. for all your test accounts $ADCompany = ‘SFR’.

As for positioning, the image gets inserted at the current position of the cursor. I found an old scripting guy article which shows how to set the cursor to a specific line.

In the example below, the picture will be inserted on line 3.

Note that the line must ‘exist’ in the document, shouldn’t be a problem if you’re using a template but with a new document you’ll find the cursor won’t move - at least it didn’t for me.

If ($ADCompany = "SFR") {

Else {


I can confirm you … If i write other word that “SFR” in the “Company” in AD, the logo dont change :confused:
I dont know why …

And good news, and I do not know why … But if I put (9,1,9), the picture comes back on line 1! If I put 8 it’s ok but not 9! You should know that line 8 I have a picture, that would be the reason ??

I despair :frowning:

To troubleshoot you should try adding the picture with no logic (IF ELSE statements) around it. This will rule out any problems with the path, permissions or picture itself.

As I said, the line must exist in the document. You probably don’t have a line 9 so the cursor can’t move there so it’s placed at position 1.

Given the difficulty positioning the image with COM objects, I would suggest having two templates: one for each logo with the logo already inserted in the correct position. Your script can then load the appropriate template based on the AD details and only modify the text. I think this is going to be simpler to code and more reliable.

I added a line 9 (with text to try) and as soon as I use line 9 it back on line 1 without stopping …

I had thought to do both models, it will probably actually easier …

If ($ADCompany = "SFR") {
$SigSource = "\\test-BE\dfs-mapping\Technical\2-Engineering\6-IT\TestSignature\SFR.docx" #Path to the *.docx file, i.e "D:\set_outlook_signature\SFR.docx"
$SigSource = "\\test-BE\dfs-mapping\Technical\2-Engineering\6-IT\TestSignature\SFRB.docx" #Path to the *.docx file, i.e "D:\set_outlook_signature\SFR.docx"

Dont work :frowning: i put “test” in Company in AD but he dont use the 2end template :confused:

The equality test in PowerShell is -eq not =. Apologies, I didn’t notice that before.

Change your test to

If ($ADcompany -eq "SFR")


I have tested and now (I do not understand why) I get errors … If I put in SFR “Company” i model “SFR” and if I put such “BBBB” and although I ’ still have the “SFR” model …
Just a little lost … I do not understand why it does not work …

Just a little lost … I do not understand why it does not work …

My script here : #Custom variables $SignatureName = 'SFR'... - TextUp

EDIT : Style of error i get :

You cannot call a method on a null-valued expression.
At \\TEST-BE\dfs-mapping\Technical\2-Engineering\6-IT\TestSignature\set_outlook_signature.ps1:161 char:1
+ $MSWord.Selection.Find.Execute($FindText, $MatchCase, $MatchWholeWord,    $MatchWil ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Looks like the document you’re trying to edit isn’t open i.e. there is no object assigned to $MSWord.Selection.

Because the Selection object doesn’t exist, the Find property doesn’t exist. Because the Find property doesn’t exist, you cannot run its Execute() method.

Perhaps something in your script closed Word or caused it to crash.