Jump to content
Efo74

Word test alignment

Recommended Posts

Efo74

Hello,

I would like to know if there is a way to justify text in Word via Autoit.

Tahnk you


:rolleyes:

Share this post


Link to post
Share on other sites
water

Sure. Autoit comes with a Word UDF, Use function _Word_DocRangeSet to select the paragraph to justify.
Then use

$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify

The WdParagraphAlignment enumeration can be found here: https://msdn.microsoft.com/en-us/library/bb237926(v=office.12).aspx

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Efo74

Thank you,

 

now I will try

:-)

 


:rolleyes:

Share this post


Link to post
Share on other sites
Efo74

Hello,

I have made some test, but if I have tree different line of text, I can't make 3 different type of justifications. I don't undestand exactly how _Word_DocRangeSet function works. Can someone make me a shot example ?

3 lines like this ...

this is the first line

this is the second line

this is the third line

Thank you all for your support


:rolleyes:

Share this post


Link to post
Share on other sites
JLogan3o13

You state you have made some test. Please post what you have, even if it is not working as you would like it to. This will help us help you ;)


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
Efo74

This is an example that does not work.

 

#include <Word.au3>

Local $oWord = _Word_Create()
$oDoc = _Word_DocAdd($oWord)

$oRange = _Word_DocRangeSet($oDoc, -1, Default, 2, Default, 1) ;???!!!!????

$oRange.ParagraphFormat.Alignment = 0 ;left
$oWord.Selection.TypeText("Test Line1 left justify")
$oRange.ParagraphFormat.Alignment = 1 ;center
$oWord.Selection.TypeParagraph()
$oWord.Selection.TypeText("Test Line2 center justify")
$oRange.ParagraphFormat.Alignment = 2 ;right
$oWord.Selection.TypeParagraph()
$oWord.Selection.TypeText("Test Line3 right justify")


:rolleyes:

Share this post


Link to post
Share on other sites
goss34

Does this help? (test.doc has just 3 lines of text)

#include <Word.au3>

; Create application object
Local $oRange, $oWord = _Word_Create()

; Open the test document
Local $oDoc = _Word_DocOpen($oWord, @DesktopDir & "\Test.doc", Default, Default, True)

; Set default alignment 0= Left, 1 = Centre, 2 = Right
Local $wdAlignParagraphJustify = 0

; Move line 1
$oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 0, Default, 2)
Local $wdAlignParagraphJustify = 2
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify


; Move line 2
$oRange = _Word_DocRangeSet($oDoc, 0, $wdParagraph, 1, Default, 2)
Local $wdAlignParagraphJustify = 1
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify

 

Share this post


Link to post
Share on other sites
Efo74

Sorry, but it does not work,

Word 2013 opens with no blank page ... nothing appear


:rolleyes:

Share this post


Link to post
Share on other sites
goss34

Have you created a document called test.doc on your desktop and saved 3 lines of test in it before running the code?

I am running version 3.3.12.0 and Word 2010 - I do not have access to 2013 currently but it works ok with my setup and the above code.

Share this post


Link to post
Share on other sites
goss34

Tested on 2013 and opened as read only - try this:

#include <Word.au3>

; Create application object
Local $oRange, $oWord = _Word_Create()

; Open the test document
Local $oDoc = _Word_DocOpen($oWord, @DesktopDir & "\Test.docx", Default, Default, False)

; Set default alignment 0= Left, 1 = Centre, 2 = Right
Local $wdAlignParagraphJustify = 0

; Move line 1
$oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 0, Default, 2)
Local $wdAlignParagraphJustify = 2
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify


; Move line 2
$oRange = _Word_DocRangeSet($oDoc, 0, $wdParagraph, 1, Default, 2)
Local $wdAlignParagraphJustify = 1
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify

Text in my docx: (no character returns after each line)

This is line 1 of the test document.

This is line 2 of the test document.

This is line 3 of the test document.

Edited by goss34

Share this post


Link to post
Share on other sites
water

Will test as soon as I return to my office.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Efo74

Thank you very much for the support goss34,

ok, it works, but I thought it was possible to write a line of text, apply formatting and then save the document, and therefore not from a document already written (saved)


:rolleyes:

Share this post


Link to post
Share on other sites
water

Sure this is possible.
Open a new document, use _Word_DocRangeSet to set the range then use $oRange.InsertAfter to insert some text at the end of the document. $oRange is expanded to include the new text. So all formatting applied to $oRange includes the next text.
 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Efo74

Thant you,

today I am out of office, but Tomorrow i will try using $oRange.InsertAfter

I keep you updated


:rolleyes:

Share this post


Link to post
Share on other sites
goss34

So here's an example for you to follow, this time you don't need test.doc, it will populate then format it as you wanted.

Hope it helps.

#include <Word.au3>

; Create application object
Local $oRange, $oWord = _Word_Create()

; Open the test document
Local $oDoc = _Word_DocAdd($oWord)

; Set default alignment 0= Left, 1 = Centre, 2 = Right
Local $wdAlignParagraphJustify = 0

; Select start of doc as range
$oRange = _Word_DocRangeSet($oDoc, -1)

; Insert text before selected range ^^
$vRange = $oDoc.Range
$vRange.InsertBefore("I am line 1" & @CR & "I am line 2" & @CR & "I am line 3")

; Move line 1
$oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 0, Default, 2)
Local $wdAlignParagraphJustify = 2
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify

; Move line 2
$oRange = _Word_DocRangeSet($oDoc, 0, $wdParagraph, 1, Default, 2)
Local $wdAlignParagraphJustify = 1
$oRange.ParagraphFormat.Alignment = $wdAlignParagraphJustify

 

  • Like 1

Share this post


Link to post
Share on other sites
Efo74

goss34,

this is just what I was trying to do,

thank you for everything, you were very kind.


:rolleyes:

Share this post


Link to post
Share on other sites
goss34

No problem Efo, its nice to be able to help someone else for a change rather than someone always helping me ;)

Share this post


Link to post
Share on other sites
blisszero

this topic helped me a lot to clarify some problems that had been, leaving just another reference for future searches

 

#include <Word.au3>
;~ criar Word oculto(false) / create hidden Word document (false)
$oWord = _Word_Create(False)
;~ Adicionar documento criado para edição / Add document created for editing
$oDoc = _Word_DocAdd($oWord)

; Usar a seleção atual / Use current selection
$oRange = _Word_DocRangeSet($oDoc, 0)

; Inserir Texto / Insert Text
$oRange.Insertafter(ClipGet())

;~ Formatação do texto / Formatting text
$oRange.ParagraphFormat.Alignment = 9 ; Left = 0, Center = 1, Right = 2, Justify = 3, ThaiJustify = 9
$oRange.Font.Name = "Century Gothic"
$oRange.Font.Size = 12
$oRange.Font.ColorIndex = 1 ; https://i-msdn.sec.s-msft.com/pt-br/vba/excel-vba/articles/images/colorin_za06050819.gif

;substituir duplo ou triplo pulo de linha / replace double or triple line jump
_Word_DocFindReplace($oDoc, "^13^13^13", "^p", Default, Default, Default, Default, True)
_Word_DocFindReplace($oDoc, "^13^13", "^p", Default, Default, Default, Default, True)

; Salvar / Save
_Word_DocSaveAs($oDoc, @ScriptDir & "\coco.doc")

; fechar COM Word / close and quit word doc
_Word_DocClose($oDoc)
_Word_Quit($oWord)

tks any \:D/

Share this post


Link to post
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

×