Author Posts

March 14, 2016 at 2:43 am

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? 🙂

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

March 14, 2016 at 1:15 pm

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:

$word.Selection.InlineShapes.AddPicture('C:\pictures\companylogo.jpg')

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

March 14, 2016 at 11:50 pm

Hi,

You can find my script on this URL : http://textup.fr/157759BE
I test lot of code, so, all test for the logo is realy bad.

Thanks.

March 15, 2016 at 11:35 am

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") {
    $MSword.Selection.InlineShapes.AddPicture("$SFR")
}

Else {
    $MSword.Selection.InlineShapes.AddPicture("$SFRBUSINESS")
}

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.

March 16, 2016 at 12:37 am

Hi,

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 🙂
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 🙁
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

March 16, 2016 at 11:32 am

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") {
    $MSword.Selection.GoTo(3,1,3)
    $MSword.Selection.InlineShapes.AddPicture("$SFR")
}

Else {
    $MSword.Selection.InlineShapes.AddPicture("$SFRBUSINESS")
}

March 17, 2016 at 7:30 am

Hello,

I can confirm you ... If i write other word that "SFR" in the "Company" in AD, the logo dont change :/
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 🙁

March 17, 2016 at 1:52 pm

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.

March 17, 2016 at 11:47 pm

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 ...

March 18, 2016 at 12:02 am

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"
}
Else
{
$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 🙁 i put "test" in Company in AD but he dont use the 2end template :/

March 18, 2016 at 5:17 am

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

Change your test to

If ($ADcompany -eq "SFR")

March 18, 2016 at 5:53 am

Hello,

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 : http://textup.fr/158095gB

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

March 18, 2016 at 11:02 am

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.