Jump to content

For loop fails with word object


Mrtn
 Share

Recommended Posts

I need some help here,

I'm having trouble with my Word object:

Arrays are filled, word document opens fine, $oWordApp is object , first loop "For $j = 0" works (first array element is processed in Word) but then I get the following error :

The requested action with this object has failed.:

$oWordApp.Selection.TypeText($aStreng[$j])

$oWordApp.Selection.TypeText($aStreng[$j])^ ERROR

i'm a little bit lost here.. please help :D

oh, and the code :

If FileExists($sFilePath1) Then ;---> check of bestand bestaat
  If WinExists($Project & " Strenggegevens.xls") Then
   MsgBox(262144, "", "Excel bestand is open")
   WinClose($Project & " Strenggegevens.xls")
   WinWaitClose($Project & " Strenggegevens.xls")
   $oExcel = _ExcelBookOpen($sFilePath1)
  Else ;---> open excel bestand
   $oExcel = _ExcelBookOpen($sFilePath1)
  EndIf
Else
MsgBox(262144, "error", "Strenggegevens bestaan niet, aan het werk luie hond! ;)", 2)
EndIf

If IsObj($oExcel) Then
  $nStrengen = _ExcelReadCell($oExcel, 1, 1)
  $oWordApp = _WordCreate($templateWord)
  $oWordApp.activedocument.SaveAs($sDirProjecten & $Project & "\" & $Project & " StrengRapport.doc")

  For $i = 4 To $nStrengen
   $aStreng = _ExcelReadArray($oExcel, $i, 1, 33)
;~  _ArrayDisplay($aStreng)
   For $j = 0 To UBound($aStreng) - 1

    If IsObj($oWordApp) Then  ;<--- control line!!

     $oWordApp.Selection.TypeText($aStreng[$j])
     $oWordApp.Selection.TypeParagraph
     Sleep(100)
     ConsoleWrite($aStreng[$j] & @CRLF)
    EndIf
   Next
   $oWordApp.Selection.TypeParagraph
  Next
  $oWordApp.Selection.TypeParagraph
  $oWordApp.Selection.TypeText("----------------")
  $oWordApp.Selection.TypeParagraph
  $oWordApp.Selection.TypeText("totaal aantal strengen = " & $nStrengen)
Endif
Link to comment
Share on other sites

I played with this for a while, but could not replicate an issue. My best guess without seeing the input file is that one of the cells that you are reading from your Excel spreadsheets is either blank, or does not exist in the word document to select.

Link to comment
Share on other sites

there are blank cells in my excel dcument indeed, those array elements are empty.

I did check for empty array elements by

For $j = 0 To UBound($aStreng) - 1
If IsObj($oWordApp) Then
If $aStreng[$j] = "" Then  ;<----- check if array element is empty
MsgBox(0, "", "leeg")
$oWordApp.Selection.TypeText("empty")
$oWordApp.Selection.TypeParagraph
Else
$oWordApp.Selection.TypeText($aStreng[$j])
$oWordApp.Selection.TypeParagraph
EndIf
ConsoleWrite($aStreng[$j] & @CRLF)
EndIf
Next

if i run this, the loop stops after the first empty array element?

("empty" is typed in word document)

I'm out of clues... :D

Edited by Mrtn
Link to comment
Share on other sites

As

" " <> ""
I would strip off all spaces
If StringStripWS($aStreng[$j],8)  = "" Then

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

Thanks for the tip water, i will implement this in my code,

however the cells in my excel document are filled with autoit, so I know that they are empty.

Maybe it is better to construct a string with my array and then write the consctructed string to word?

Link to comment
Share on other sites

I'm sure it's worth a try and might be must faster too.

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