Jump to content

Word UDF Range problem (how to get the first line in a cell in a table)


Recommended Posts

Hi

 

I have a document which consists of a table with 1000 rows (articles) and 12 columns (data for each article). I need to extract (for every article, but i can do this in a loop) the first line of the 5th column (the text goes over multiple lines in the column), and i cant get it to work :(

Here is what i have so far (the $oWordDoc object already exists):

    $oCell=$oWordDoc.Tables(1).Cell(5,3).Select
    $oRange=_Word_DocRangeSet($oCell,-1,$wdLine,1,$wdLine,1)
    Local $oSelection=$oRange.Select
    $sData=$oSelection.Value
    MsgBox(0,"",$sData)

But i am always getting this error:

"R:\AutoIT\word-integration.au3" (390) : ==> Variable must be of type "Object".:
Local $oSelection=$oRange.Select
Local $oSelection=$oRange^ ERROR

What am i doing wrong? :(

Best,

Thomas

Edited by Thomymaster
Link to comment
Share on other sites

  • Moderators

Look at the return values for _Word_DocRangeSet

It doesn't return an object, but you're treating it as if it does.

Edit:

Weird, I see the return value for success says "1", but in the example it's used as an object.

My apologies for my ignorance on this subject.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Seems to be a documentation bug. I will test tomorrow and post the result.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

Try using $oSelection=$oRange.Select()

instead of $oSelection=$oRange.Select

AutoIt likes object methods to have ()

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

  • Moderators

It's incredibly difficult to debug your issue without working code (working as something we can run on our side).

One thing is for sure, you need to debug your code.

If Not IsObj($oRange) Then

As an example.

It's obvious where it fails at the moment, but try and run this and see if $oCell fails:

$oCell=$oWordDoc.Tables(1).Cell(5,3).Select
If NOt IsObj($oCell) Then
    MsgBox(16 + 262144, "Error", "$oCell is not an object!")
    Exit
EndIf

$oRange=_Word_DocRangeSet($oCell,-1,$wdLine,1,$wdLine,1)
If Not IsObj($oRange) Then
    ; we know it fails here
    MsgBox(16 + 262144, "Error", "$oRange is not an object!")
    Exit
EndIf

Local $oSelection=$oRange.Select

$sData = $oSelection.Value
MsgBox(0,"",$sData)

Otherwise, you'll need to provide step by step what you want to do (because we don't know what $wdLine is as an example), and provide a document we can test with that fails for you.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

What is the value of @error and @extended after

$oRange=_Word_DocRangeSet($oCell,-1,$wdLine,1,$wdLine,1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

This works fine here. Test.docx has a single table with 3 rows and 3 columns:

#include <Word.au3>
Global $sDocument = @ScriptDir & "\Test.docx"
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, $sDocument)
Global $oRow = $oDoc.Tables(1).Cell(1, 1).Row ; Get the whole row
Global $sText = $oRow.ConvertToText(0).Text ; Convert the table row to text. Separator: 0 = Paragraph markers, 1 = tab, 2 = comma, 3 = default list separator,
$oDoc.Undo(1) ; Undo the ConvertToText function so the table remains unchanged in the document
MsgBox(0, "", $sText)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

Seems to be a documentation bug. I will test tomorrow and post the result.

Fixed the docu

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

Hi

OK here is what i wanted to do:

#include <Word.au3>

$g_sDocument="O:\Test.docx"

    Local $oTable , $oRange, $oCell
    Local $sData=""
    Local $oWord=_Word_Create()
    Local $oWordDoc=_Word_DocOpen($oWord,$g_sDocument,False,$WdOpenFormatAuto,True) ; open read-only

    ; VBA statement is "table.Cell(count, Column_from).Select"

    $oCell=$oWordDoc.Tables(1).Cell(3,3).Select()
    $oCell=$oTable.Cell(5,3).Select
    $oRange=_Word_DocRangeSet($oCell,-1,$wdLine,1,$wdLine,1)
    ;$oRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 1, Default, 2)
    Local $oSelection=$oRange.Select
    $sData=$oSelection.Value
    MsgBox(0,"",$sData)

    _Word_Quit($oWord)
    Exit

Like i said i want to (later in a loop) get the first line of text of each row in the 3rd column, so for the first row it would be:

"Große Guanyin in Blanc-de-Chine Porzellan, China "

The error message is:

"R:\AutoIT\test.au3" (12) : ==> Variable must be of type "Object".:
$oCell=$oWordDoc.Tables(1).Cell(3,3).Select()
$oCell=$oWordDoc^ ERROR
->23:23:33 AutoIt3.exe ended.rc:1

Test.docx

Link to comment
Share on other sites

  • Moderators

Not sure how you'd assume when the line ends, I'll leave that up to you to figure, but this worked.

#include <Word.au3>

Global $gsz_File = "C:\Temp\Test.docx"

Global $go_Word = _Word_Create()
If @error Then Exit 110

Global $go_WDoc = _Word_DocAttach($go_Word, $gsz_File)
If @error Then
    $go_WDoc = _Word_DocOpen($go_Word, $gsz_File, Default, Default, True)
    If @error Then Exit @error
EndIf

$gsz_Text = $go_WDoc.Tables(1).Rows(1).Cells(3).Range.Text
MsgBox(64, "Info Text", _
    StringRegExpReplace($gsz_Text, "^(?si)(.+?)(\s*\d+\.\s*Jh\.\s*.+?)\z", "$1"))

_Word_DocClose($go_WDoc)
_Word_Quit($go_Word)

_Word_DocTableRead(); kept failing for me with no extended data.

;line 486 of word.au3
Local $oRange = $vTable.ConvertToText($sSeparator, False)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

_Word_DocTableRead only works on tables, not on ranges.

I might change this in the future if needed.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

Hi

@water this would be great :)

I'll try SmOke_Ns solution right now

The original VBA i wanted to get ported was (here "count" stands for every row and "Column_from" is "3")

            table.Cell(count, Column_from).Select
            Set rng = ActiveDocument.Range(Selection.Start, Selection.Bookmarks("\line").End)
            rng.Select
            Selection.Copy
            t = Selection.Text
Link to comment
Share on other sites

Have you tried to read the whole table using _Word_DocTableRead into an array and then process the cells you need?

Might be easier and even faster.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

  • Moderators

_Word_DocTableRead only works on tables, not on ranges.

I might change this in the future if needed.

I'm pretty sure I knew the difference ;).

I only used the range.text when doctableread failed at where I said.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

_Word_DocTableRead(); kept failing for me with no extended data.

I checked the code and noticed: It's a bug! Will soon be fixed.

Edit: Fixed in SVN

When you add

#include <Debug.au3>
_DebugSetup()
_DebugCOMError()

to your script you will get more detailed (and correct) error information.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

Link to comment
Share on other sites

@water

The prolem is that i wanted to get the first line in the cell, not until the first @CR (for this, i could use _Word_DocTableRead() and then process each column of the array). And this is not possible with _WordDocTabelRead()

So if you would change the function to work on ranges as well, then it'll maybe work

However then i still doesn't know how to write this statement in AutoIT:

Set rng = ActiveDocument.Range(Selection.Start, Selection.Bookmarks("\line").End)

Where a range object is returned which i can use for the updated _WordDocTableRead()

Edited by Thomymaster
Link to comment
Share on other sites

_Word_DocTableRead preserves line feeds in cells.

The following code assumes that cell 1 in row 2 consists of multiple lines. They are split at @CR and the resulting array is being displayed.

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

Global $sDocument = @ScriptDir & "\Test.docx"
; Create application object
Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open the test document
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word", "Error opening '" & $sDocument & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Local $asResult = _Word_DocTableRead($oDoc, 1, 0)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word", "Error reading the table to an array." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $aLine = StringSplit($asResult[0][0], @CR,  $STR_CHRSPLIT )
_ArrayDisplay($aLine)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

 

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

  • Recently Browsing   0 members

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