![]() |
KnowBrainer Speech Recognition | ![]() |
Topic Title: Mysterious scripting error: Object var is 'Nothing' Topic Summary: Can't see what I've done wrong in three lines of code Created On: 01/31/2016 09:01 AM Status: Post and Reply |
|
![]() |
|
I have three simple lines of code, and they're not running. Here they are: Sub Main This started as a Word macro. Works just dandy in Word. When I transfer it to Advanced Scripting—after having converted it using the Word 2016 reference—it throws the following error: (10094) ActiveX Automation: Object var is 'Nothing'. (at line 4 Position 0) Given that it does exactly what I want as a Word macro, I could of course make the Dragon macro simply call the word keystroke. But I'd rather do the right thing. What is turning my selection into "Nothing"? Anyone know? Thanks, LS ------------------------- loris Win 10, DPI 14, KnowBrainer 2017 |
|
|
|
![]() |
|
You need to create an object reference to the active Word instance. You also should clean up by getting rid of the reference after you're done with it / before the End sub. Sub Main End Sub also, since you already have access to the selection, you could combine the () steps into one line instead of 3: Sub Main End Sub
I just tested this from another app to change the Word selection, using the second version of the code above. I don't have Dragon on this machine, but this should work the same in Advanced Scripting. One more thing: using this method, you don't have to explicitly select the reference to the version of Word on your machine. This will work across various Word versions (unless MS changes the underlying objects, which hasn't happened in forever). |
|
|
|
![]() |
|
That's super, speechpro. That works straightaway. I'm struggling a little bit to figure out why I need the object reference to the active Word instance - I've transferred other Word macros across that didn't require this, and they seem similar in structure, at least in so far as they too used selections. But whatever. It gives me something to study, and it works! Thanks again.
------------------------- loris Win 10, DPI 14, KnowBrainer 2017 |
|
|
|
![]() |
|
I'm getting this same error message sometimes.
I have a macro in Microsoft Word and an identical command in Dragon. The macro/command is to remove highlight from selected text. The command is: Selection.Range.HighlightColorIndex = wdNoHighlight Usually, the command works great. I say "remove highlight" and highlighted text is removed. But sometimes... I get an error: ``` The Macro contains the error: MyCommand: "remove highlight" Line: 6 Position: O Description: (10094) ActiveX Automation: Object var is "Nothing", - Please use MyCommands Editor to make the appropriate corrections. ``` I tried adding on this code suggested by speechPro: ``` Set objWord = GetObject(, "Word.Application") objWord.Selection.Range.HighlightColorIndex = wdNoHighlight Set objWord = Nothing ``` But I get the same issue. My command has Microsoft Word object library checked. What's weird is that the issue comes and goes. Sometimes it seems to get fixed when I restart the computer. Any ideas what's going on here? Microsoft 365. ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
I think it would be better to use code that checked the value of objWord before trhing to use it, like so:
Set objWord = GetObject(,"Word.Application") if objWord is Nothing MessageBox "No reference!" Exit Sub endif ' CLAIM: object reference is valid here ... continue with your code Set objWord = nothing ------------------------- 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. |
|
|
|
![]() |
|
Thanks, I tried that, but didn't fix it.
The real question here is not so much "How do I make this script better?" but "why does this command work sometimes, but then break other times? Why does restarting fix it?" It's very strange. ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
just seeing this One issue = even though the object is referenced, the constant "wdNoHighlight" is not known through this method. You can decode the constant value through the Word VBA editor, or use an external reference to get the correct value, but saving you a click - it's 0 (zero) https://learn.microsoft.com/en-us/office/vba/api/word.wdcolorindex Change from objWord.Selection.Range.HighlightColorIndex = wdNoHighlight to objWord.Selection.Range.HighlightColorIndex = 0
|
|
|
|
![]() |
|
Could you please post the entire script that is problematic?
------------------------- 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. |
|
|
|
![]() |
|
Kevin,
If you set the thread to display a linear mode, you will find the answer from speech pro coming in previous to your response. I guess he says it all, and although I haven't tested it, this should explain it. When doing VBA code in Dragon, very often you have to handle the parameters passed in in particular ways, such as converting them to the correct data type or simply just passing them in literally. That's because the Dragon "IDE" has no way of talking to the objects the same way that the VBA editor does. ------------------------- |
|
|
|
![]() |
|
Here is an example of the code that doesn't always work: Sub Main Selection.Range.HighlightColorIndex = wdNoHighlight End Sub
Today, this code is working fine. I predict that sometime in the next week or so, it will stop working, and through the error mentioned, and will continue to not work until I restart the computer. I'm in the same pattern with other Dragon commands that make use of code that was generated by a Microsoft Word macro. For example, here's another one that intermittently gives me the same problem: Sub Main NewWindow End Sub ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
One issue = even though the object is referenced, the constant "wdNoHighlight" is not known through this method.
You can decode the constant value through the Word VBA editor, or use an external reference to get the correct value, but saving you a click - it's 0 (zero)
https://learn.microsoft.com/en-us/office/vba/api/word.wdcolorindex
Change from objWord.Selection.Range.HighlightColorIndex = wdNoHighlight
to objWord.Selection.Range.HighlightColorIndex = 0 Thanks. Your version of the code is working well. But my original code is also working--for now, at least. I predict that sometime in the next week, it will stop working until I restart my computer. The next time the issue is occurring, I will see if your code works better than my code. Your code:
Set objWord = GetObject(, "Word.Application") objWord.Selection.Range.HighlightColorIndex = 0 Set objWord = Nothing
My code:
Selection.Range.HighlightColorIndex = wdNoHighlight
------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
Just curious - try running the macro with the constant specified as a variable (not hard-coded to 0) 1. WITH Word running 2. WITH ZERO instances of Word running (check Task Manager before issuing command) Wondering if having Word active somehow passes through the constant value. |
|
|
|
![]() |
|
One workaround would be to make all of these scripts macros in Word, and then call the macro from Dragon. Should be as simple as a single line as follows:
Application.Run(macroname) You will need an object reference to Word, of course. |
|
|
|
![]() |
|
I'm wondering if declaring the variables in the Dragon script would help. I gave up trying to do Word/VBA operations in Dragon scripting long ago. It was much more reliable to write the macros in the Office apps and call them from Dragon like Matt suggests.
------------------------- 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. |
|
|
FuseTalk Standard Edition v4.0 - © 1999-2023 FuseTalk™ Inc. All rights reserved.