Sign in to follow this  
Followers 0
Thomymaster

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

19 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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.

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites

It is still returning the same error :(

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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.

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

_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.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

_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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

@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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks a lot :)

I managed to solve this in the same way, i first read everything via _Word_DocTableRead() and then later update the value in the third cell with the text until the first @CR in this cell for all rows

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