Jump to content
Efo74

Word test alignment

Recommended Posts

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


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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")

Share this post


Link to post
Share on other sites

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

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

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

Will test as soon as I return to my office.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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 2019-08-19 - Version 1.4.13.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-07-24 - Version 1.3.6.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (NEW 2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (NEW 2019-10-09 - Version 0.9.0.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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

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

×
×
  • Create New...