KnowBrainer Speech Recognition
Decrease font size
Increase font size
Topic Title: Can KB commands call Word VBA subroutines directly?
Topic Summary: Can KB Basic use the VBA/Word object models instead of just sending keystrokes?
Created On: 02/09/2018 01:34 PM
Status: Post and Reply
Linear : Threading : Single : Branch
 Can KB commands call Word VBA subroutines directly?   - kkkwj - 02/09/2018 01:34 PM  
 Can KB commands call Word VBA subroutines directly?   - speechpro - 02/09/2018 02:01 PM  
 Can KB commands call Word VBA subroutines directly?   - kkkwj - 02/09/2018 02:16 PM  
 Can KB commands call Word VBA subroutines directly?   - speechpro - 02/09/2018 02:36 PM  
 Can KB commands call Word VBA subroutines directly?   - kkkwj - 02/09/2018 04:16 PM  
 Can KB commands call Word VBA subroutines directly?   - kkkwj - 02/09/2018 05:53 PM  
 Can KB commands call Word VBA subroutines directly?   - kkkwj - 05/29/2020 09:46 PM  
 Can KB commands call Word VBA subroutines directly?   - monkey8 - 06/01/2020 01:29 PM  
 Can KB commands call Word VBA subroutines directly?   - monkey8 - 06/01/2020 02:05 PM  
 Can KB commands call Word VBA subroutines directly?   - kkkwj - 06/01/2020 03:07 PM  
Keyword
 02/09/2018 01:34 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 434
Joined: 11/05/2015

My Dragon commands currently just send keystrokes to MS Word to laboriously go through the manual keystrokes that would normally be required to run a macro in word. Needless to say, keystroke answer P rollover the ribbon during this sequence, and it takes a while to execute.

 

Is it possible for either the Dragon commands using advanced scripting or the KB commands (using winwrap basic) to get a reference to the Microsoft Word application and then directly manipulate the objects in the Word object model? Thanks



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

Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, KB 2017, SpeechStart, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, Office 365

 02/09/2018 02:01 PM
User is offline View Users Profile Print this message


speechpro
Power Member

Posts: 70
Joined: 11/09/2006

Create an advanced script - name it whatever makes sense for your purpose - include the following:

Sub Main

'call Word macro
'...create reference to Word object
Dim objWord As Object
Set objWord = GetObject(, "Word.Application")
objWord.Run "yourmacrohere"

'...destroy reference to Word object
set objWord = Nothing

End Sub
 02/09/2018 02:16 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 434
Joined: 11/05/2015

Thank you. You beat me to it, more or less. After scouring the net for a while, I found an explanation of how to add Dragon references to the MS word object Library. The secret technique is to right-click within the Advanced Scripting window and there is a references option on the bottom of the pop-up menu. Once you have the reference, then you can do anything you want with the objects. I have attached an image. 

 

Sub Main

Application.Run "FHeading2"

End Sub

 

Maybe your method would be more robust when moving among machines, because it does not depend on a reference to a local MSWord library. I sure don't want to have to relink a new reference for every macro in my command set when I copy the files to my laptop!



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

Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, KB 2017, SpeechStart, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, Office 365



 02/09/2018 02:36 PM
User is offline View Users Profile Print this message


speechpro
Power Member

Posts: 70
Joined: 11/09/2006

One small clarification: When you reference Word via the method you used, the object is specific to the Word version you check. If you deploy the command on a machine with a different Word version, you will need to recreate the reference.

 02/09/2018 04:16 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 434
Joined: 11/05/2015

Haha, I see that you saw the same thing. As usual, you're a step ahead of me. (I edited my post above without reading yours below my edits, sorry...)


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

Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, KB 2017, SpeechStart, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, Office 365

 02/09/2018 05:53 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 434
Joined: 11/05/2015

I tried out this code in a dozen macros, and moved it to my laptop. It worked perfectly fine. This would be a great little piece of code to add to the KB manual, IMHO.

Sub Main

'call Word macro
'...create reference to the Word object
Dim objWord As Object
Set objWord = GetObject(, "Word.Application")
objWord.Run "yourmacrohere"

'...destroy reference to Word object
set objWord = Nothing

End Sub

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

Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, KB 2017, SpeechStart, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, Office 365

 05/29/2020 09:46 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 434
Joined: 11/05/2015

I use the VBA method above (GetObject) routinely in Dragon scripting (not KB) to run macros in Word, PowerPoint, and Excel. They all use the obj.Run "MacroName" syntax (with .run).

However, I've been trying to use that method to run a VBA macro in Outlook without success. The "objWord" object for Outlook does not have a .Run method. There is no Outlook COM object in the list of references when I right-click in the script editor window. (And I didn't have to use a reference for the GetObject method for Word or PowerPoint or the others).

Does anyone know how to call a VBA macro in Outlook using the code method above? There must be a way since Outlook has VBA. But maybe the security involved with Outlook prevents Dragon from calling macros.

 

UPDATE

 

After a pile of reading and looking around the net for examples, I found out that there is no way for an external program to run internal Outlook VBA macros created in the Outlook VBA editor. As best I could tell, the design of the Outlook object model has been designed to accomodate (and enforce) email security rules.



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

Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, KB 2017, SpeechStart, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, Office 365



 06/01/2020 01:29 PM
User is offline View Users Profile Print this message

Author Icon
monkey8
Top-Tier Member

Posts: 3540
Joined: 01/14/2008

If you want to access Microsoft Word programmatically try using the .NET libraries that are available through KnowBrainer by setting the script to .NET instead of COM. It's a very underutilised part of KnowBrainer that I added and you will find plenty examples on Google of .net commands integrating with office. .NET Scripting is a lot easier to use and quicker to put scripts together especially for beginners.

 

I didn't add any Microsoft Word library commands to KnowBrainer(.NET or COM) because they are very easy to implement and the options are endless but like I say try .NET where you can quickly convert examples of C# and VB .net commands into KnowBrainer WinWrap basic scripting. In the examples below just remember that the references are set by right clicking on the editor and not through visual studio. I think from memory Kevin you program in C#.

 

https://support.microsoft.com/en-gb/help/316383/how-to-automate-word-from-visual-basic-net-to-create-a-new-document

 

https://www.c-sharpcorner.com/article/word-automation-using-C-Sharp/100X

 

After a pile of reading and looking around the net for examples, I found out that there is no way for an external program to run internal Outlook VBA macros created in the Outlook VBA editor. As best I could tell, the design of the Outlook object model has been designed to accomodate (and enforce) email security rules.

 

 

EDIT: yes there is I am pleased to say, if you want to run VBA macros created in office through KnowBrainer or advanced scripting then the easiest way is to assign hotkeys to the macros through word or Outlook or Excel and then use application-specific scripts to activate the hotkeys.

 

So if you want to take a stage further as a C# programmer then you can pretty much run a lot of C# directly under KnowBrainer (converted to VB .Net) giving you access to libraries of commands and subroutines. If you are VB .NET programmer then even easier.



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



 06/01/2020 02:05 PM
User is offline View Users Profile Print this message

Author Icon
monkey8
Top-Tier Member

Posts: 3540
Joined: 01/14/2008

If you want examples of inbuilt KB .net commands I have added one called "running processes" and I asked Rüdiger to add one way back when, for which he kindly obliged, based on some code from his dragonbench application and it is called "System Information".

 



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



 06/01/2020 03:07 PM
User is offline View Users Profile Print this message

Author Icon
kkkwj
Top-Tier Member

Posts: 434
Joined: 11/05/2015

Hi Lindsay, thank you for your info.

The example links you provided also use the GetObject method that I learned from PG to get a reference to a running Word application. One of the examples creates a new word application, too. Yes, I prefer to write in C#, although like everyone else I can write and script in many languages.

You made me smile when you said:

"EDIT: yes there is I am pleased to say, if you want to run VBA macros created in office through KnowBrainer or advanced scripting then the easiest way is to assign hotkeys to the macros through word or Outlook or Excel and then use application-specific scripts to activate the hotkeys."

Of course, you are right; but I rarely think of using keystrokes (a limitation I'm trying to overcome :-)). The use of keystrokes to run Outlook VBA macros is allowed by Outlook because the keys come through the Outlook GUI interface and not through an API ("Outlook.Run TheMacro") interface. It's kind of odd that they'll let you run the same macro through keys but not through an API. Oh well. Presumably they had a reason.

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

Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, KB 2017, SpeechStart, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, Office 365

Statistics
31670 users are registered to the KnowBrainer Speech Recognition forum.
There are currently 0 users logged in.
The most users ever online was 4473 on 07/17/2020 at 10:00 AM.
There are currently 433 guests browsing this forum, which makes a total of 433 users using this forum.

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