Jump to content

Recommended Posts

Posted (edited)

Hi All,

I got some examples on how to create a word document from autoit but I'm hung up on formatting.  Specifically setting up ranges.  The example at https://www.autoitscript.com/autoit3/docs/libfunctions/_Word_DocRangeSet.htm isn't helping.

Here is what I am trying to do.

I am creating a document one line at a time and need each insertion to have different formatting. This project I'm using autoit to create an outline. Here is an example of what I want.

1) Title A
            Subtitle XX
2) Title B
           Subtitle YY
           Subtitle ZZ

For starters I am just trying to italicize specific lines or words. Here is a sample of code that I'm working from:

#include <MsgBoxConstants.au3>
#include <Word.au3>

$sFile = @ScriptDir & "\Output\Agenda.doc"
Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_Create Example", _
        "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Global $oDoc = _Word_DocAdd($oWord)
Global $oRange = _Word_DocRangeSet($oDoc, 0) ; Use current selection
Global $myArray[5] = ["1) Title A", "Subtitle XX", "2) Title B", "Subtitle YY", "Subtitle ZZ" ]


For $i=0 to Ubound($myArray)-1
      $oRange.InsertAfter($myArray[$i] & @CRLF)
      If StringInStr($myArray[$i],"Subtitle") Then
              $oRange.Font.Italic = True
      EndIf
Next

The above code makes everything italic when I just want the lines with "Subtitle" in them italic.

 

Edited by nassausky
Clarifying my project
Posted

Hope this helps someone stuck on using another method to add formatting per line:

#include <MsgBoxConstants.au3>
#include <Word.au3>

$sFile = @ScriptDir & "\Output\Agenda.doc"
Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_Create Example", _
        "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Global $oDoc = _Word_DocAdd($oWord)
Global $aArray[5] = ["1) Title A", "Subtitle XX", "2) Title B", "Subtitle YY", "Subtitle ZZ" ]

Local $oRange[1], $oRangeA, $oRangeFound, $oSearchRange

$oRangeA = _Word_DocRangeSet($oDoc, -1) ; Use start of document
$oRangeA.InsertAfter("Agenda" & @CRLF)


For $i = 0 to Ubound($aArray)-1
       ReDim $oRange[UBound($oRange) + 1]
       $oRange[$i]= _Word_DocRangeSet($oDoc, -2)
       $oRange[$i].InsertAfter($aArray[$i] & @CRLF)
       If StringInStr($aArray[$i],"Subtitle") Then
              ConsoleWrite("Found Subtitle in string for "& $aArray[$i])
              $oRange[$i].Font.Bold = True
       EndIf
Next

I had to rethink that if I created a new range for each line it would do the trick but the question was how do I have it continue from the previous range. The trick seems to be using -2 as the last parameter in _Word_DocRangeSet

Posted

There is no need to store the different ranges. This works as well :)

#include <MsgBoxConstants.au3>
#include <Word.au3>

Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Test Script", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Global $oDoc = _Word_DocAdd($oWord)
If @error <> 0 Then Exit MsgBox($MB_SYSTEMMODAL, "Test Script", "Error creating a new Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Global $aArray[] = ["Agenda", "1) Title A", "Subtitle XX", "2) Title B", "Subtitle YY", "Subtitle ZZ"]

$oRange = _Word_DocRangeSet($oDoc, -1) ; Use start of document
For $i = 0 To UBound($aArray) - 1
    $oRange = _Word_DocRangeSet($oDoc, -2) ; Go to end of doucument
    $oRange.InsertAfter($aArray[$i] & @CRLF)
    If StringInStr($aArray[$i], "Subtitle") Then $oRange.Font.Bold = True ; Set subtitles to bold
Next

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...