Page 1 of 1 1
Topic Options
#212023 - 2016-10-17 12:05 PM Word automation - image text wrap
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
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


Edited by Mart (2016-10-17 12:07 PM)
Edit Reason: fixed typo in the code
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#212024 - 2016-10-17 01:58 PM Re: Word automation - image text wrap [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
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.....
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#212025 - 2016-10-17 05:07 PM Re: Word automation - image text wrap [Re: Mart]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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

Top
#212026 - 2016-10-17 05:09 PM Re: Word automation - image text wrap [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Nevermind. That just changes it to a floating object.
Top
#212027 - 2016-10-17 05:21 PM Re: Word automation - image text wrap [Re: ShaneEP]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
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

- Chuck Norris once sold ebay to ebay on ebay.

Top
#212028 - 2016-10-17 10:29 PM Re: Word automation - image text wrap [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
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
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 370 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.055 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org