![]() |
KnowBrainer Speech Recognition | ![]() |
Topic Title: Error message the first time running a custom command Topic Summary: Created On: 04/23/2022 03:44 PM Status: Post and Reply |
|
![]() |
![]() |
- Alan Cantor | - 04/23/2022 03:44 PM |
![]() |
![]() |
- Robert Snow | - 07/31/2022 04:28 PM |
![]() |
![]() |
- Mav | - 08/01/2022 01:44 AM |
![]() |
![]() |
- Alan Cantor | - 08/01/2022 10:20 AM |
![]() |
![]() |
- Edgar | - 08/03/2022 09:39 AM |
![]() |
![]() |
- Alan Cantor | - 08/03/2022 03:28 PM |
![]() |
![]() |
- Mav | - 08/04/2022 02:04 AM |
![]() |
![]() |
- Alan Cantor | - 08/04/2022 10:32 AM |
![]() |
![]() |
- R. Wilke | - 08/04/2022 11:22 AM |
![]() |
![]() |
- Mphillipson | - 08/04/2022 01:17 PM |
![]() |
![]() |
- kkkwj | - 08/04/2022 03:06 PM |
![]() |
![]() |
- Mphillipson | - 08/06/2022 03:33 PM |
![]() |
|||
I have a custom Dragon command for Excel called "good bye <values> <values>. Not sure this is relevant, but as part of this command, I set the Reference to "Microsoft Excel XX.0 Object Library (X.X)". [The values of X depend on the Excel version.] |
|||
|
|||
![]() |
|||
I'm sure you thought of this but could it have anything to do with system restart. ------------------------- Robert Snow |
|||
|
|||
![]() |
|||
What is the command in line 5? Without knowing this, it's next to impossible to tell for sure what causes the problem. Could it be that you're starting Excel and the target document simply hasn't been initialized yet?
hth mav |
|||
|
|||
![]() |
|||
Hi Mav, |
|||
|
|||
![]() |
|||
1 Sub Main 2 3 Dim objExcel As Excel.Application 4 Set objExcel = GetObject(, "Excel.Application") 5 6 Dim StartRow, EndRow, RangeRow As String 7 8 Let StartRow = ListVar1 9 Let EndRow = ListVar2 10 11 Let RangeRow = StartRow & ":" & EndRow 12 13 objExcel.Rows(RangeRow).Delete 14 15 End Sub When I remove the blank lines from the script, the instruction that triggers the error message is "Dim StartRow, EndRow, RangeRow As String"
Three thoughts… 1) it's highly probable that line 4 "Set objExcel = GetObject(, "Excel.Application")" is the culprit. Try moving it down below line 11 (Let RangeRow…); The error message should now show the line number just above the "Set obj…". This will indicate that the "Set obj…" line is the actual culprit. 2) can you provide a link to the documentation for "GetObject"? 3) a possible workaround, create a custom startup script for Excel which you would always use in place of "start Excel": (In pseudocode) Sub Main ShellExecute "[Fully qualified to excel]" [Wait 1] however long it takes for Excel to fully open Dim objExcel As Excel.Application Set objExcel = GetObject(, "Excel.Application") End Sub You could get fancy and also pass in the filename if you wanted but that might actually make matters worse. Then, your row deletion script Would remain exactly as is (with another call to "Dim obj…" and "Set obj…". This might "prime the pump" <shrug>.
------------------------- -Edgar |
|||
|
|||
![]() |
|||
Hi Edgar,
I think you're onto something. Maybe the pump isn't getting primed! I like your ideas, especially your pointing out that there's nothing sacrosanct about the position of this line... Set objExcel = GetObject(, "Excel.Application") ...as long as it appears BEFORE the line that actually deletes cells. So I tried moving it around the script. The error message signals a problem with the line AFTER the "Set objExcel" instruction, whatever that line happens to be. Here's another clue: I'm finding that I must exit and restart Excel every time I make a change to the script. If I don't, the command doesn't do anything. |
|||
|
|||
![]() |
|||
The error comes from the call to GetObject() for sure. Seems as if Dragon counts lines differently than the rest of the world. If it were 1 lower than the real number then I'd suspect the lines to be 0-indexed (the first line would be #0 and not #1), but with the number being one HIGHER than the real error line I can only assume a bug in the scripting engine. Anyway, the GetObject() function is part of the Basic Scripting Runtime. The description on the Nuance page is quite short, unfortunately. I've attached the Microsoft Windows Scripting help file, where the function is documented in more detail. The error message states that the object is currently not active. So Excel is either not running or not ready yet. If you don't want to access an object that's currently active (this is what GetObject does), you can use CreateObject() to start the application. Depending on your use case, this might be more suitable for you.
hth mav
|
|||
|
|||
![]() |
|||
The problem just gets stranger and stranger.
I reviewed my other custom commands for Excel that use almost the same code. None of the other commands exhibit the strange behaviour of failing once, and then working. For example, this script works, and it's very similar to the one that is causing mischief: Sub Main ' Set the Reference to "Microsoft Excel XX.0 Object Library (X.X)" ' The values of X depend on the Excel version SendDragonKeys "{Esc}" Dim objExcel As Excel.Application Set objExcel = GetObject(, "Excel.Application") Dim Target, ColumnA, ColumnB, Row As String Let ColumnA = Left(ListVar1, 1) Let ColumnB = Left(ListVar2, 1) Let Row = ListVar3 Let Row = Replace (Row, ",", "") ' Strip commas from Row number, e.g., 1,000 -> 1000 Let Target = ColumnA & ColumnB & Row objExcel.Range(Target).Select End Sub Since the above script works as expected while a similar one fails and then works, could there be an issue with the instruction that's doing the heavy lifting? This one: objExcel.Rows(RangeRow).Delete I don't remember how I came up with this line. I probably copied the code from a website, or I came up with it myself through trial and error. |
|||
|
|||
![]() |
|||
Many many years ago, I did some scripting in VBA for Excel, and although I have lost all my notes, I can well remember that it wasn't the easiest thing to do. I also remember that I spent quite a few days digesting the documentation and the tutorials, of which there are zillions around, before I could even begin with it.
Bottom line being that, in order to create and instantiate an object properly, you have to follow the object model scheme, which goes something like "Application - Workbook - Sheet - Range - ..." or thereabouts. If I recall correctly, one of the crucial points was also being able to determine whether Excel is already active and you want to talk to that active instance, or create a new one, which makes all the difference. As regards handling of objects in general, also bear in mind, that treating them properly also requires deleting them properly when you're done with them, such as by setting them to nothing for instance, because if you don't, you will cause a leak which might leave the scripting engine hanging, and Dragon along with it, if the engine is called from within Dragon, at worst. Welcome to the world of 00P, although just in miniature! ------------------------- |
|||
|
|||
![]() |
|||
Is there any difference if Excel is already running and when it is not?
I think I remember that Get object means that Microsoft Excel has to exist otherwise you have to use the create object instead. Where the create object will create a new instance of Microsoft Excel. And the script will not run until it is loaded. ------------------------- Thanks Mark
Dragon Professional Advanced Scripting/KnowBrainer Scripts |
|||
|
|||
![]() |
|||
My two bits:
RW is correct - none of your scripts clean up after themselves. You need 'set objExcel = nothing' after you are done with the object. Otherwise COM garbage is not cleaned up and "code" may try to use old objects that are invalid. Edgar and Mark are also correct - GetObject expects excel to be running (I think), and may return a null object. Oh wait, didn't the error message say "ActiveX Automation: no object currently active."? :-) To write your scripts properly they should check the value of objExcel something like this: ' get the running excel instance set objExcel = GetObject(...) ' abort if Excel is not running if objExcel = nothing then MsgBox No Excel object found! Exit Function, Exit Sub, or whatever endif ' now use the valid Excel object Your code here ------------------------- Win10/11/x64, AMD Ryzen 7 3700X/3950X, 64/128GB RAM, Dragon 15.3, SP 7 Standard, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Amazon YUWAKAYI headset, Klim and JUKSTG earbuds with microphones, excellent Sareville Wireless Mono Headset, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and Logitech G502 awesome gaming mouse. |
|||
|
|||
![]() |
|||
I believe get object will raise an error if Microsoft Excel not running
The following CODE will create a new instance of Microsoft Excel, but would not raise an error: ------------------------- Thanks Mark
Dragon Professional Advanced Scripting/KnowBrainer Scripts |
|||
|
FuseTalk Standard Edition v4.0 - © 1999-2023 FuseTalk™ Inc. All rights reserved.