Sign in to follow this  
Followers 0
mstr

Table with variables

7 posts in this topic

I've got a question:

I want to make a AutoIt application, who asks me some information.

If I've entered this information then he put it into a kind of 'Word Table'

Example:

Posted Image

So the variables he most ask me, I enter the variables and he put it into a table.

If all the variables are asked and entered, then he most print the table twice.

Is this possible with AutoIt, or is't better to chose another scripting language?

If it's possible, how can I make a table into AutoIt?

Share this post


Link to post
Share on other sites



I'll let you ponder over this until I have time to explain it better.

#include <Word.au3>

Dim $aQuestions[3] = ["Name", "Address", "Telephone Number"]

$oWordApp = _WordCreate(@ScriptDir & "\Test.doc", 1, 0)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oRange = $oDoc.Range
$oTable = _WordDocTableAdd($oDoc, $oRange, 3, 2, True)
$oCells = $oTable.Range.Cells
$x = 0
For $i = 1 To $oCells.Count
    If Mod($i, 2) = 0 Then
        $sAnswer = InputBox("Question", "What is your " & $aQuestions[$x] & "?")
        $oCells ($i).Range.InsertAfter ($sAnswer)
        $x += 1
    Else
        $oCells ($i).Range.InsertAfter ($aQuestions[$x])
    EndIf
Next

_WordPropertySet($oWordApp, "visible", True)

For $i = 0 To 42
    $oTable.AutoFormat ($i, True, True, True)
    Sleep(500)
Next

;===============================================================================
;
; Function Name:    _WordDocTableAdd()
; Description:      Returns an object variable representing a new table object
; Parameter(s):     $o_object               - Object variable of a Word.Application, document object
;                   $o_range                - The range where you want the table to appear. The table replaces the range, if the range isn't collapsed
;                   $i_NumRows              - The number of rows you want to include in the table
;                   $i_NumColumns           - The number of columns you want to include in the table
;                   $i_DefaultTableBehavior - Optional: Sets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells contents (AutoFit)
;                                               0 = (Default) AutoFit disabled
;                                               1 = AutoFit enabled
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success  - Returns an object variable pointing to a Word.Application, table object
;                   On Failure  - Returns 0 and sets @ERROR
;                   @ERROR      - 0 ($_WordStatus_Success) = No Error
;                               - 1 ($_WordStatus_GeneralError) = General Error
;                               - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
;                               - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
;                   @Extended   - Contains invalid parameter number
; Author(s):        Bob Anthony (Code based off IE.au3)
;
;===============================================================================
;
Func _WordDocTableAdd(ByRef $o_object, $o_range, $i_NumRows, $i_NumColumns, $i_DefaultTableBehavior = 0)
    If Not IsObj($o_object) Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "$_WordStatus_InvalidDataType")
        SetError($_WordStatus_InvalidDataType, 1)
        Return 0
    EndIf
    ;
    If Not __WordIsObjType ($o_object, "document") Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "$_WordStatus_InvalidObjectType")
        SetError($_WordStatus_InvalidObjectType, 1)
        Return 0
    EndIf
    ;
    Local $o_table

    $o_table = $o_object.Tables.Add ($o_range, $i_NumRows, $i_NumColumns, $i_DefaultTableBehavior)
    If Not IsObj($o_table) Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "", "Tables Object Creation Failed")
        SetError($_WordStatus_GeneralError)
        Return 0
    EndIf

    SetError($_WordStatus_Success)
    Return $o_table
EndFunc   ;==>_WordDocTableAdd

Share this post


Link to post
Share on other sites

That was were I was looking for :)

I have still a question, how can I change the properties of the table?

Like font size etc.

Share this post


Link to post
Share on other sites

A small kick,

Maybe somebody can help me :)

Share this post


Link to post
Share on other sites

Maybe it's easier to do it in Excel?

Share this post


Link to post
Share on other sites

Google for "Word Object Model" and try the MSDN sites that come up. You'll find the properties of table objects in there somewhere.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

This should help, if you have any questions please ask.

#include <Word.au3>

Dim $aQuestions[3] = ["Name", "Address", "Telephone Number"]

_WordErrorHandlerRegister()
$oWordApp = _WordCreate(@ScriptDir & "\Test.doc", 1, 0)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oRange = $oDoc.Range
$oTable = _WordDocTableAdd($oDoc, $oRange, 3, 2, True)
$oCells = $oTable.Range.Cells
$x = 0
For $i = 1 To $oCells.Count
    If Mod($i, 2) = 0 Then
        $sAnswer = InputBox("Question", "What is your " & $aQuestions[$x] & "?")
        $oCells ($i).Range.InsertAfter ($sAnswer)
        $x += 1
    Else
        $oCells ($i).Range.InsertAfter ($aQuestions[$x])
    EndIf
Next

_WordPropertySet($oWordApp, "visible", True)

With $oTable
    .Style = "Medium Grid 2 - Accent 1"
    .ApplyStyleFirstColumn = False
    .ApplyStyleHeadingRows = False
    .ApplyStyleLastColumn = False
    .ApplyStyleLastRow = False
EndWith

With $oTable.Range.Font
    .Name = "Courier New"
    .Size = 24
    .Bold = True
    .Italic = True
    .Underline = True
EndWith

;===============================================================================
;
; Function Name:    _WordDocTableAdd()
; Description:      Returns an object variable representing a new table object
; Parameter(s):     $o_object            - Object variable of a Word.Application, document object
;               $o_range            - The range where you want the table to appear. The table replaces the range, if the range isn't collapsed
;               $i_NumRows    - The number of rows you want to include in the table
;               $i_NumColumns      - The number of columns you want to include in the table
;               $i_DefaultTableBehavior    - Optional: Sets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells contents (AutoFit)
;                                    0 = (Default) AutoFit disabled
;                                    1 = AutoFit enabled
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success  - Returns an object variable pointing to a Word.Application, table object
;                   On Failure  - Returns 0 and sets @ERROR
;               @ERROR  - 0 ($_WordStatus_Success) = No Error
;                        - 1 ($_WordStatus_GeneralError) = General Error
;                        - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
;                        - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
;               @Extended  - Contains invalid parameter number
; Author(s):        Bob Anthony (Code based off IE.au3)
;
;===============================================================================
;
Func _WordDocTableAdd(ByRef $o_object, $o_range, $i_NumRows, $i_NumColumns, $i_DefaultTableBehavior = 0)
    If Not IsObj($o_object) Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "$_WordStatus_InvalidDataType")
        SetError($_WordStatus_InvalidDataType, 1)
        Return 0
    EndIf
    ;
    If Not __WordIsObjType ($o_object, "document") Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "$_WordStatus_InvalidObjectType")
        SetError($_WordStatus_InvalidObjectType, 1)
        Return 0
    EndIf
    ;
    Local $o_table

    $o_table = $o_object.Tables.Add ($o_range, $i_NumRows, $i_NumColumns, $i_DefaultTableBehavior)
    If Not IsObj($o_table) Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "", "Tables Object Creation Failed")
        SetError($_WordStatus_GeneralError)
        Return 0
    EndIf

    SetError($_WordStatus_Success)
    Return $o_table
EndFunc   ;==>_WordDocTableAdd

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
Sign in to follow this  
Followers 0