VBA Code help

skmcrodgers

New Member
Joined
Mar 15, 2017
Messages
4
we have a keypad that you program each button so when pressed it will bring to front the excel file for that button. i had this working but at work we got updated to office 365 (excel 2016) and i think the title bar naming is the difference. i wrote vbs code to rename the title bar but it puts - Excel at the end and if save it changes to - Saved and code no longer works. before iw ould still have to open all 10 excel files manually then program the buttons to bring to front..i would rather have the code check to see if the excel file is open if not it will open it if it is already open bring it to the front without saying file already open.. the below is an example of the code that did work on excel 2010. the keypad is programmed using vba. i have all 10 excel sheets in a folder.. this worked before upgrade.. not all code just to show how it was coded: also wasnt sure if you could open the excel by file name instead of title name? any suggestions on how to program to open if not already and not prompt already open or if i open each file manually how to have it bring the window to front for the excel button i press?? thanks for any help!!

'=↓=↓=↓= MW3 Header Start =↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG1]
' Script Type: MacroWorks 3 Device Script
' Device Name: X-keys XK-24
' Device Description: 24 Button HID Controller
' Script Name: XK-24
' Script Description: Macros for XK-24 [MWTAG19]
' PID: 1029
' Firmware: π3 V 28
' MW3 Version: 15
' Script Language: Visual Basic
' Script Template: Beta 1.0
' Format: Unicode 8
' P.I. Engineering, Inc.
' "The No Slogan Company"
' Williamston, Michigan, USA
' www.xkeys.com
' Start Date: 3/13/2017 11:12:31 AM [MWTAG20]
' Last Update: 3/28/2017 10:36:14 AM [MWTAG21]
' Default Mode: Hardware
' Image Rotation: 0
' Config Path: XK-24\ReportConfig_XK24.xml [MWTAG18]
'=↑=↑=↑= MW3 Header End =↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=[/MWTAG1]
'=↓=↓=↓= Notes Start =↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG2]
' User comment notes can go here.
' CAUTION:
' DO NOT MODIFY COMMENT LINES OR TAGS
'
' This file can contain both true VB script code, and special hardware codes to program
' the X-keys device's internal memory. These hardware codes are auto-generated by the
' MW3.1 GUI and are indicated by commented lines ending or bracketed with [HWTAG1].
' DO NOT MODIFY THESE LINES!
' This can lead to corrupt data written to the device.
' Additionally other commented lines with [MWTAGx] or other formatting information
' for the GUI can be found through out this file. Modifying these comments can cause
' problems in the GUI. Always make back-ups before making any modifications to this file.
'
' ADDTIONAL NOTES:
' User comment notes can go here. This area is safe.
'=↑=↑=↑= Notes End =↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=[MWTAG2]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Public Class Script
Implements Interfaces.IScript '[MWTAG12]

'=↓=↓=↓= Initialize Variables Start =↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG3]
Dim MW3 As Interfaces.MW3
Dim MyDevice As Integer() = {1029, -1, 1} 'Device Descriptor [MWTAG8]
Dim AppName As String = "" 'Active application name
Dim AppTitle As String = "" 'Active application title
Dim Layer_Red As Integer = 0 'Red,3
'=↑=↑=↑= Initialize Varibles End =↑=↑=↑=↑=↑=↑=↑=↑=↑=[/MWTAG3]
Public Sub Initialize(MW3 As Interfaces.MW3) Implements Interfaces.IScript.Initialize
Me.MW3 = MW3
End Sub

Public Sub ScriptLoad() Implements Interfaces.IScript.ScriptLoad
LEDset()
MW3.SetAllBacklightLED(MyDevice,1,0) ' Turn red off
MW3.SetAllBacklightLED(MyDevice,0,1) ' Turn blue on
MW3.SetBacklightIntensity(MyDevice,255,255) 'blue and red max intensity
End Sub

Public Function ScriptUnload() As Boolean Implements Interfaces.IScript.ScriptUnload
End Function

Public Sub SysPowerChange(PowerState As Byte) Implements Interfaces.IScript.SysPowerChange
End Sub
Public Sub ActiveAppChange(ActiveAppTitle As String, ActiveAppName As String) Implements Interfaces.IScript.ActiveAppChange
AppName= ActiveAppName
AppTitle= ActiveAppTitle
End Sub

Public Sub DeviceChange(DevInfo As Integer(), plugged As Boolean) Implements Interfaces.IScript.DeviceChange

End Sub
Public Sub Initializing() Implements Interfaces.IScript.Initializing
End Sub

'=↓=↓=↓= Digital State Change Event Start =↓=↓=↓=↓=↓=↓=↓=[MWTAG4]
Public Sub XKEvent(CID As Integer, State As Boolean, Repeat As Integer, TimeStamp as Long, MessageData As Integer) Implements Interfaces.IScript.XKEvent
Select Case CID 'Button Check Start [MWTAG9]
Case 1001 'Button 001
If State 'Pressed [MWTAG10]
'
<windowtofront sample="">
Dim windowtitle As String = "Foundry Lab Report - Compactability.xlsm"
MW3.WindowToFront(windowtitle)
'/
<windowtofront sample="">
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
Case 1002 'Button 002
If State 'Pressed [MWTAG10]
'
<windowtofront sample="">
Dim windowtitle As String = "Foundry Lab Report - Permiability.xlsm"
MW3.WindowToFront(windowtitle)
'/
<windowtofront sample="">
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
Case 1003 'Button 003
If State 'Pressed [MWTAG10]
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]



this is code i tried to use to open the excel file if not already open but it wouldnt work at all either..


'=↓=↓=↓= MW3 Header Start =↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG1]
' Script Type: MacroWorks 3 Device Script
' Device Name: X-keys XK-24
' Device Description: 24 Button HID Controller
' Script Name: XK-24
' Script Description: Macros for XK-24 [MWTAG19]
' PID: 1029
' Firmware: π3 V 28
' MW3 Version: 15
' Script Language: Visual Basic
' Script Template: Beta 1.0
' Format: Unicode 8
' P.I. Engineering, Inc.
' "The No Slogan Company"
' Williamston, Michigan, USA
' www.xkeys.com
' Start Date: 3/13/2017 11:12:31 AM [MWTAG20]
' Last Update: 3/28/2017 10:36:14 AM [MWTAG21]
' Default Mode: Hardware
' Image Rotation: 0
' Config Path: XK-24\ReportConfig_XK24.xml [MWTAG18]
'=↑=↑=↑= MW3 Header End =↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=[/MWTAG1]
'=↓=↓=↓= Notes Start =↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG2]
' User comment notes can go here.
' CAUTION:
' DO NOT MODIFY COMMENT LINES OR TAGS
'
' This file can contain both true VB script code, and special hardware codes to program
' the X-keys device's internal memory. These hardware codes are auto-generated by the
' MW3.1 GUI and are indicated by commented lines ending or bracketed with [HWTAG1].
' DO NOT MODIFY THESE LINES!
' This can lead to corrupt data written to the device.
' Additionally other commented lines with [MWTAGx] or other formatting information
' for the GUI can be found through out this file. Modifying these comments can cause
' problems in the GUI. Always make back-ups before making any modifications to this file.
'
' ADDTIONAL NOTES:
' User comment notes can go here. This area is safe.
'=↑=↑=↑= Notes End =↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=↑=[MWTAG2]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Public Class Script
Implements Interfaces.IScript '[MWTAG12]

'=↓=↓=↓= Initialize Variables Start =↓=↓=↓=↓=↓=↓=↓=↓=[MWTAG3]
Dim MW3 As Interfaces.MW3
Dim MyDevice As Integer() = {1029, -1, 1} 'Device Descriptor [MWTAG8]
Dim AppName As String = "" 'Active application name
Dim AppTitle As String = "" 'Active application title
Dim Layer_Red As Integer = 0 'Red,3
'=↑=↑=↑= Initialize Varibles End =↑=↑=↑=↑=↑=↑=↑=↑=↑=[/MWTAG3]
Public Sub Initialize(MW3 As Interfaces.MW3) Implements Interfaces.IScript.Initialize
Me.MW3 = MW3
End Sub

Public Sub ScriptLoad() Implements Interfaces.IScript.ScriptLoad
LEDset()
MW3.SetAllBacklightLED(MyDevice,1,0) ' Turn red off
MW3.SetAllBacklightLED(MyDevice,0,1) ' Turn blue on
MW3.SetBacklightIntensity(MyDevice,255,255) 'blue and red max intensity
End Sub

Public Function ScriptUnload() As Boolean Implements Interfaces.IScript.ScriptUnload
End Function

Public Sub SysPowerChange(PowerState As Byte) Implements Interfaces.IScript.SysPowerChange
End Sub
Public Sub ActiveAppChange(ActiveAppTitle As String, ActiveAppName As String) Implements Interfaces.IScript.ActiveAppChange
AppName= ActiveAppName
AppTitle= ActiveAppTitle
End Sub

Public Sub DeviceChange(DevInfo As Integer(), plugged As Boolean) Implements Interfaces.IScript.DeviceChange

End Sub
Public Sub Initializing() Implements Interfaces.IScript.Initializing
End Sub

'=↓=↓=↓= Digital State Change Event Start =↓=↓=↓=↓=↓=↓=↓=[MWTAG4]
Public Sub XKEvent(CID As Integer, State As Boolean, Repeat As Integer, TimeStamp as Long, MessageData As Integer) Implements Interfaces.IScript.XKEvent
Select Case CID 'Button Check Start [MWTAG9]
Case 1001 'Button 001
If State 'Pressed [MWTAG10]
'
<shortcut[p:\lab\charting sand\tv="" display\compactability.xlsx]="">
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Compactability.xlsx","")
'
</shortcut[p:\lab\charting>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
Case 1002 'Button 002
If State 'Pressed [MWTAG10]
'
<shortcut[p:\lab\charting sand\tv="" display\permability.xlsx]="">
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Permability.xlsx","")
'
</shortcut[p:\lab\charting>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
Case 1003 'Button 003
If State 'Pressed [MWTAG10]
'
<shortcut[p:\lab\charting sand\tv="" display\friability.xlsx]="">
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Friability.xlsx","")
'
</shortcut[p:\lab\charting>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
</windowtofront></windowtofront></windowtofront></windowtofront>
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
actually this is the code I used for checking if file open if not opening the file but it don't work either
Case 1001 'Button 001
If State 'Pressed [MWTAG10]
'<Shortcut[P:\lab\CHARTING SAND\TV Display\Compactability.xlsx]>
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Compactability.xlsx","")
'</Shortcut[P:\lab\CHARTING SAND\TV Display\Compactability.xlsx]>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
Case 1002 'Button 002
If State 'Pressed [MWTAG10]
'<Shortcut[P:\lab\CHARTING SAND\TV Display\Permability.xlsx]>
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Permability.xlsx","")
'</Shortcut[P:\lab\CHARTING SAND\TV Display\Permability.xlsx]>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
Case 1003 'Button 003
If State 'Pressed [MWTAG10]
'<Shortcut[P:\lab\CHARTING SAND\TV Display\Friability.xlsx]>
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Friability.xlsx","")
'</Shortcut[P:\lab\CHARTING SAND\TV Display\Friability.xlsx]>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
 
Upvote 0
for some reason it isn't showing the code I used for opening trying to display it again: sorry for duplication

Case 1001 'Button 001
If State 'Pressed [MWTAG10]
'<Shortcut[P:\lab\CHARTING SAND\TV Display\Compactability.xlsx]>
MW3.RunApp("P:\lab\CHARTING SAND\TV Display\Compactability.xlsx","")
'</Shortcut[P:\lab\CHARTING SAND\TV Display\Compactability.xlsx]>
Else 'Released [MWTAG11]
End If 'Press-Release [/MWTAG10/MWTAG11]
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,665
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top