Sign in to follow this  
Followers 0
Mrtn

For loop fails with word object

6 posts in this topic

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

Share this post


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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


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

Share this post


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

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