Mart
(KiX Supporter)
2016-10-17 12:05 PM
Word automation - image text wrap

Hi Guys,

Trying to automate some things in Word and I'm not having any luck yet. What I would like to do is add an image and set the text wrapping to Square. I have seen some posts about InLine shapes not supporting text wrapping options but I did not find a definitive answer on that yet. Second thing I tried is recording a macro but text wrapping buttons are disabled when recording a macro.

Did anybody do anything like this? any help would be appreciated because I s*ck at COM scripting. The code below says everything worked fine but text wrapping is still set to InLine.

I'm runnign Word 2010, and kix 4.66.

 Code:
Break on

$objWord = CreateObject("Word.Application")
? @ERROR
? @SERROR

$objDoc = $objWord.Documents.Add()
? @ERROR
? @SERROR

$objSelection = $objWord.Selection
? @ERROR
? @SERROR

$objWord.Visible = True
? @ERROR
? @SERROR

$objShape = $objSelection.InlineShapes.AddPicture("c:\somefolder\image.png")
? @ERROR
? @SERROR

;wdWrapSquare = 0
;wdWrapTight = 1
;wdWrapThrough = 2
;wdWrapNone = 3
;wdWrapTopBottom = 4

$objShape.WrapFormat.Type = 0 ;"wdWrapSquare"
? @ERROR
? @ERROR

$objSelection.TypeText("111")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("222")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("333")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("444")

Sleep 10

;Quit word
$objWord.Quit


Mart
(KiX Supporter)
2016-10-17 01:58 PM
Re: Word automation - image text wrap

Playing around a bit and found:

 Quote:

....
An InlineShape object cannot have text wrapping. Word handles an InlineShape as a text character, so logically, it cannot have text wrapping. Only a Shape can have text wrapping.
....


in https://social.msdn.microsoft.com/Forums...hape?forum=vsto and in https://support.office.com/en-us/article...75-3dfae9547341

Looks like I need to add a shape, add the image as fill material for the shape and set text wrapping on the shape.

I'm going to play around a bit more.....


ShaneEP
(MM club member)
2016-10-17 05:07 PM
Re: Word automation - image text wrap

How about this?

 Code:
Break on

$objWord = CreateObject("Word.Application")
? @ERROR
? @SERROR

$objDoc = $objWord.Documents.Add()
? @ERROR
? @SERROR

$objSelection = $objWord.Selection
? @ERROR
? @SERROR

$objWord.Visible = True
? @ERROR
? @SERROR

$objShape = $objSelection.InlineShapes.AddPicture(@ScriptDir+"\AEP.png", 0, 1)
? @ERROR
? @SERROR

$nul = $objShape.ConvertToShape()
? @ERROR
? @ERROR

;wdWrapSquare = 0
;wdWrapTight = 1
;wdWrapThrough = 2
;wdWrapNone = 3
;wdWrapTopBottom = 4

$objShape.WrapFormat.Type = 0 ;"wdWrapSquare"
? @ERROR
? @ERROR

$objSelection.TypeText("111")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("222")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("333")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("444")

;Sleep 10

;Quit word
;$objWord.Quit


ShaneEP
(MM club member)
2016-10-17 05:09 PM
Re: Word automation - image text wrap

Nevermind. That just changes it to a floating object.

Mart
(KiX Supporter)
2016-10-17 05:21 PM
Re: Word automation - image text wrap

Well there seems to be a solution. I got something cooking that needs some more testing. I'll post it tonight or tomorowmorning as soon as it is ready.

Mart
(KiX Supporter)
2016-10-17 10:29 PM
Re: Word automation - image text wrap

After all it turned out to be much simpler than I thought at first.

As said above InLine images do not accept text wrapping as they are seen as text by Word and you cannot just wrap text around other text. Shapes do accept text wrapping so inserting a shape and setting an image as fill material should do the trick. It is possible to insert an image as a shape and apply text wrapping to it like shown below. The extra testing was needed to see if it could be used in our outlook signature creation part of our logon script. I already have something running in production for this (see: this post) but the company branding is being updated so changes are needed. Took some time to figure it out but it works.

Below is some code to demonstrate how it works. I hope it might be useful to others in the future.

As a side note, I used .gif images to prevent Word from resizing the image as it does with .png and .jpg images for example.

 Code:
Break on

$objWord = CreateObject("Word.Application")
? @ERROR
? @SERROR

$objDoc = $objWord.Documents.Add()
? @ERROR
? @SERROR

$objSelection = $objWord.Selection
? @ERROR
? @SERROR

$objWord.Visible = True
? @ERROR
? @SERROR

$objShape = $objDoc.Shapes.AddPicture("C:\folder\image.gif")

;wdWrapSquare = 0
;wdWrapTight = 1
;wdWrapThrough = 2
;wdWrapNone = 3
;wdWrapTopBottom = 4
$objShape.wrapformat.type = 0
? @ERROR
? @SERROR

$objSelection.TypeText("111")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("222")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("333")
$objSelection.TypeText(Chr(11))
$objSelection.TypeText("444")
$objSelection.TypeText(Chr(11))

Sleep 10

;Quit word
;$objWord.Quit