KnowBrainer Speech Recognition
Decrease font size
Increase font size
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
Linear : Threading : Single : Branch
 Mysterious scripting error: Object var is 'Nothing'   - loris - 01/31/2016 09:01 AM  
 Mysterious scripting error: Object var is 'Nothing'   - speechpro - 01/31/2016 09:51 AM  
 Mysterious scripting error: Object var is 'Nothing'   - loris - 01/31/2016 10:26 AM  
 Mysterious scripting error: Object var is 'Nothing'   - wristofdoom - 01/25/2023 01:01 PM  
 Mysterious scripting error: Object var is 'Nothing'   - speechpro - 01/29/2023 05:15 PM  
 Mysterious scripting error: Object var is 'Nothing'   - wristofdoom - 01/30/2023 05:49 PM  
 Mysterious scripting error: Object var is 'Nothing'   - speechpro - 01/30/2023 08:52 PM  
 Mysterious scripting error: Object var is 'Nothing'   - kkkwj - 01/25/2023 05:07 PM  
 Mysterious scripting error: Object var is 'Nothing'   - wristofdoom - 01/29/2023 01:49 PM  
 Mysterious scripting error: Object var is 'Nothing'   - kkkwj - 01/30/2023 11:30 AM  
 Mysterious scripting error: Object var is 'Nothing'   - wristofdoom - 01/30/2023 05:43 PM  
 Mysterious scripting error: Object var is 'Nothing'   - R. Wilke - 01/30/2023 11:45 AM  
 Mysterious scripting error: Object var is 'Nothing'   - Matt_Chambers - 01/31/2023 07:57 AM  
 Mysterious scripting error: Object var is 'Nothing'   - kkkwj - 01/31/2023 09:03 PM  
Keyword
 01/31/2016 09:01 AM
User is offline View Users Profile Print this message


loris
Senior Member

Posts: 102
Joined: 10/04/2009

I have three simple lines of code, and they're not running. Here they are:

Sub Main
    Selection = Trim(Selection)
    Selection.InsertBefore "("
    Selection.InsertAfter ") "
End Sub

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



 01/31/2016 09:51 AM
User is offline View Users Profile Print this message


speechpro
Senior Member

Posts: 88
Joined: 11/09/2006

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
    Set objWord  = GetObject(, "Word.Application") 
    objWord.Selection = Trim(objWord.Selection)
    objWord.Selection.InsertBefore "("
    objword.Selection.InsertAfter ") "
    set objWord = Nothing

End Sub

also, since you already have access to the selection, you could combine the () steps into one line instead of 3:

Sub Main
    Set objWord  = GetObject(, "Word.Application") 
    objWord.Selection = "(" & Trim(objWord.Selection) & ")"
    set objWord = Nothing

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



 01/31/2016 10:26 AM
User is offline View Users Profile Print this message


loris
Senior Member

Posts: 102
Joined: 10/04/2009

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

 01/25/2023 01:01 PM
User is offline View Users Profile Print this message

Author Icon
wristofdoom
Top-Tier Member

Posts: 347
Joined: 09/03/2020

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.

 01/29/2023 05:15 PM
User is offline View Users Profile Print this message


speechpro
Senior Member

Posts: 88
Joined: 11/09/2006

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

 

 



 01/30/2023 05:49 PM
User is offline View Users Profile Print this message

Author Icon
wristofdoom
Top-Tier Member

Posts: 347
Joined: 09/03/2020

Originally posted by: speechpro 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

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.



 01/30/2023 08:52 PM
User is offline View Users Profile Print this message


speechpro
Senior Member

Posts: 88
Joined: 11/09/2006

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.

 01/25/2023 05:07 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 1123
Joined: 11/05/2015

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.

 01/29/2023 01:49 PM
User is offline View Users Profile Print this message

Author Icon
wristofdoom
Top-Tier Member

Posts: 347
Joined: 09/03/2020

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.

 01/30/2023 11:30 AM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 1123
Joined: 11/05/2015

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.

 01/30/2023 05:43 PM
User is offline View Users Profile Print this message

Author Icon
wristofdoom
Top-Tier Member

Posts: 347
Joined: 09/03/2020

Originally posted by: kkkwj Could you please post the entire script that is problematic?

 

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.



 01/30/2023 11:45 AM
User is offline View Users Profile Print this message

Author Icon
R. Wilke
Top-Tier Member

Posts: 8058
Joined: 03/04/2007

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.

-------------------------

 01/31/2023 07:57 AM
User is offline View Users Profile Print this message

Author Icon
Matt_Chambers
Top-Tier Member

Posts: 756
Joined: 08/09/2018

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.
 01/31/2023 09:03 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 1123
Joined: 11/05/2015

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.

Statistics
32528 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 2 users logged in.
The most users ever online was 12124 on 09/09/2020 at 04:59 AM.
There are currently 361 guests browsing this forum, which makes a total of 363 users using this forum.

FuseTalk Standard Edition v4.0 - © 1999-2023 FuseTalk™ Inc. All rights reserved.