Jump to content

MS Word vs AutoIt Word UDF > Selection.TypeParagraph after InlineShapes.AddPicture()


Go to solution Solved by Blaxxun,

Recommended Posts

Hello Forum!

I have a Word VBA Macro that inserts an image.
I rebuilt the same behavior in AutoIt.
Everything works the same except of one tiny difference.
In VBA the image does NOT move down when i manually press <ENTER> after the Macro was running.
In Autoit the image DOES move down when i press <ENTER> after the Script was running.
Adding the image is just the beginning of the script.
More Text will be added after.

When i press <END> and then <ENTER> the behavior is the same again as in VBA.
I tryd already several things but i do not want to hack in a Send("{END}")


VBA:

    ' LOGO ----------------------------------------------------------------------------------------------------------------------

    Dim Img As String
    Dim inlineShape As inlineShape
    Dim shape As shape
    
    Img = "C:\Logo.png"
    
    Set inlineShape = Selection.InlineShapes.AddPicture(FileName:=Img, LinkToFile:=False, SaveWithDocument:=True)
  
    inlineShape.LockAspectRatio = msoTrue
    inlineShape.width = CentimetersToPoints(9.58)
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    
    Set shape = inlineShape.ConvertToShape
    shape.WrapFormat.Type = wdWrapMergeFront ' Set wrapping type to "Top and Bottom" < This is not wrong!
   
    ' Texts ------------------------------------------------------------------------------------------------------------------------

    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Selection.ParagraphFormat.SpaceAfter = 10
    'Selection.TypeParagraph


AutoIt:

#include <Word.au3>
#include "WordConstants.au3"

Global $oRange
Global Const $wdAlignParagraphCenter = 1 ;  Align Center
Global Const $wdAlignParagraphLeft = 0 ;    Align Left
Global Const $wdWrapMergeFront = 4 ;        In front of Text 

Global $Bild = @ScriptDir & "\Logo.png"
Global $oWordApp = _Word_Create(); Start Word
Global $oDoc = _Word_DocAdd($oWordApp); Create Document
Global $oInlineShape = $oDoc.InlineShapes.AddPicture($Bild, False, True)
$oInlineShape.LockAspectRatio = True
$oInlineShape.width = CentimetersToPoints(9.58)
$oRange = $oInlineShape.Range
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphCenter ; Center
Global $oShape = $oInlineShape.ConvertToShape ; Convert InlineShape to Shape
$oShape.WrapFormat.Type = $wdWrapMergeFront ; Set wrapping type to "Top and Bottom" < "MergeFront" selects "Top and Bottom" in Word
$oRange = _Word_DocRangeSet($oDoc, 0)
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphLeft ; Align Left
$oRange.ParagraphFormat.SpaceAfter = 10

;$oWordApp.Selection.TypeParagraph
;$oRange.InsertAfter("Inserted text after range.")
;$oRange.EndKey($wdLine,0)
;$oRange.Text = @CRLF
;$oRange.Collapse($wdCollapseEnd)

 

Edited by Blaxxun
Typo
Link to comment
Share on other sites

  • Solution

Okay, looks like i was able to solve it myself 😆
 

#include <Word.au3>
#include "WordConstants.au3"

Global Const $wdAlignParagraphCenter = 1 ;                                  Align Center
Global Const $wdAlignParagraphLeft = 0 ;                                    Align Left
Global Const $wdWrapMergeFront = 4 ;                                        In front of Text
Global Const $wdMove = 0 ;                                                  Move to

Global $sPic = @ScriptDir & "\Logo.png"
Global $oWord = _Word_Create() ;                                            Start Word
Global $oDoc = _Word_DocAdd($oWord) ;                                       Create Document
Global $oRange
Global $oSel = $oWord.Selection
Global $oInlineShape = $oDoc.InlineShapes.AddPicture($sPic, False, True)
$oInlineShape.LockAspectRatio = True
$oInlineShape.width = CentimetersToPoints(9.58)
$oSel.ParagraphFormat.Alignment = $wdAlignParagraphCenter ;                 Align Center
Global $oShape = $oInlineShape.ConvertToShape ;                             Convert InlineShape to Shape
$oShape.WrapFormat.Type = $wdWrapMergeFront ;                               Set wrapping type to "Top and Bottom"
$oSel.ParagraphFormat.Alignment = $wdAlignParagraphLeft ;                   Align Left
$oSel.ParagraphFormat.SpaceAfter = 10
$oSel.EndKey($wdLine, $wdMove)
$oSel.TypeParagraph


.Selection.EndKey was not working because i used it wrong.
$oWord.Selection.EndKey($wdLine,$wdMove) is correct.

Edited by Blaxxun
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...