Solved

How do run a vbscript from vba while referencing activeworkbook

Posted on 2011-09-16
8
561 Views
Last Modified: 2012-05-12
I have an expertly developed script from Rob that sendkeys to the name manager in excel. It currently picks a file, deletes the names and stops. I'd like to run it after a vba macro.

My goals
display all names in active workbook
delete all the names that can be deleted from vba
run the vbscript to delete the remaining names through sendkeys on the Name Manager

Please refer here: http:/Q_27308727.html#a36553093 for a sample file so you know what I'm dealing with.

Thanks,

Thomas

Sub Display_All_Names_in_Workbook_Delete()
Dim nmName As Name

On Error Resume Next
For Each nmName In ActiveWorkbook.Names
    nmName.Visible = True
    nmName.Delete
Next

End Sub

Open in new window

Set myExcelWorker = CreateObject("Excel.Application")

' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False

myExcelWorker.Workbooks.Open(GetFileName( "", "*.xlsx;*.xls;*.xlsxm"))

Set oWB =myExcelWorker.ActiveWorkbook
myExcelWorker.Visible=True

intNames = oWB.Names.Count

strTempScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "DeleteNamesTemp.vbs"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objScript = objFSO.CreateTextFile(strTempScript, True)
objScript.WriteLine "WScript.Sleep 5000"
objScript.WriteLine "Set WshShell = CreateObject(""WScript.Shell"")"
objScript.WriteLine "WshShell.SendKeys ""{TAB}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{TAB}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{TAB}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{DOWN}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "For i = 1 to " & intNames
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""%D"""
objScript.WriteLine "WScript.Sleep 250"
objScript.WriteLine "WshShell.SendKeys ""{DOWN}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{UP}"""
objScript.WriteLine "Next"
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{ESC}"""
objScript.Close


Set WshShell = CreateObject("WScript.Shell")

WScript.Sleep 100
WshShell.SendKeys "^{F3}"
WScript.Sleep 200

WshShell.Run "wscript.exe """ & strTempScript & """", 1, True

objFSO.DeleteFile strTempScript, True


Function GetFileName( myDir, myFilter )
' This function opens a File Open Dialog and returns the
' fully qualified path of the selected file as a string.
'
' Arguments:
' myDir is the initial directory; if no directory is
' specified "My Documents" is used;
' NOTE: this default requires the WScript.Shell
' object, and works only in WSH, not in HTAs!
' myFilter is the file type filter; format "File type description|*.ext"
' ALL arguments MUST get A value (use "" for defaults), OR otherwise you must
' use "On Error Resume Next" to prevent error messages.
'
' Dependencies:
' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and later.
' To use the default "My Documents" WScript.Shell is used, which isn't
' available in HTAs.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

    ' Standard housekeeping
    Dim objDialog

    ' Create a dialog object
    Set objDialog = CreateObject( "UserAccounts.CommonDialog" )

    ' Check arguments and use defaults when necessary
    If myDir = "" Then
        ' Default initial folder is "My Documents"
        objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
    Else
        ' Use the specified initial folder
        objDialog.InitialDir = myDir
    End If
    If myFilter = "" Then
        ' Default file filter is "All files"
        objDialog.Filter = "All files|*.*"
    Else
        ' Use the specified file filter
        objDialog.Filter = myFilter
    End If

    ' Open the dialog and return the selected file name
    If objDialog.ShowOpen Then
        GetFileName = objDialog.FileName
    Else
        GetFileName = ""
    End If
End Function

Open in new window

0
Comment
Question by:nutsch
  • 3
  • 2
8 Comments
 
LVL 39

Author Comment

by:nutsch
ID: 36554642
Mark, it was working in the preview and now it's no longer working.

T
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36556015
Hi, instead of running the macro at all, why don't you put that macro code inside the VBS, so that you have all of the code in the VBS?

After this:
intNames = oWB.Names.Count

Open in new window


put this:
On Error Resume Next
For Each nmName In oWB.Names
    nmName.Visible = True
    nmName.Delete
Next

Open in new window



and that should be it!

Rob.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36559402
Any chance of a sample file that is not corrupt but demonstrates the issue? The ones posted in that other question seem to work fine with normal delete methods.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

Author Comment

by:nutsch
ID: 36560973
This one should do it.

T
Sample.xls
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 499 total points
ID: 36563966
Slight correction, try this.

Regards,

Rob.
Set myExcelWorker = CreateObject("Excel.Application")

' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False

myExcelWorker.Workbooks.Open(GetFileName( "", "*.xlsx;*.xls;*.xlsxm"))

Set oWB =myExcelWorker.ActiveWorkbook
myExcelWorker.Visible=True

On Error Resume Next
For Each nmName In oWB.Names
    nmName.Visible = True
    nmName.Delete
Next 
Err.Clear
On Error GoTo 0

intNames = oWB.Names.Count

strTempScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "DeleteNamesTemp.vbs"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objScript = objFSO.CreateTextFile(strTempScript, True)
objScript.WriteLine "WScript.Sleep 5000"
objScript.WriteLine "Set WshShell = CreateObject(""WScript.Shell"")"
objScript.WriteLine "WshShell.SendKeys ""{TAB}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{TAB}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{TAB}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{DOWN}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "For i = 1 to " & intNames
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""%D"""
objScript.WriteLine "WScript.Sleep 250"
objScript.WriteLine "WshShell.SendKeys ""{DOWN}"""
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{UP}"""
objScript.WriteLine "Next"
objScript.WriteLine "WScript.Sleep 100"
objScript.WriteLine "WshShell.SendKeys ""{ESC}"""
objScript.Close


Set WshShell = CreateObject("WScript.Shell")

WScript.Sleep 100
WshShell.SendKeys "^{F3}"
WScript.Sleep 200

WshShell.Run "wscript.exe """ & strTempScript & """", 1, True

objFSO.DeleteFile strTempScript, True


Function GetFileName( myDir, myFilter )
' This function opens a File Open Dialog and returns the
' fully qualified path of the selected file as a string.
'
' Arguments:
' myDir is the initial directory; if no directory is
' specified "My Documents" is used;
' NOTE: this default requires the WScript.Shell
' object, and works only in WSH, not in HTAs!
' myFilter is the file type filter; format "File type description|*.ext"
' ALL arguments MUST get A value (use "" for defaults), OR otherwise you must
' use "On Error Resume Next" to prevent error messages.
'
' Dependencies:
' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and later.
' To use the default "My Documents" WScript.Shell is used, which isn't
' available in HTAs.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

    ' Standard housekeeping
    Dim objDialog

    ' Create a dialog object
    Set objDialog = CreateObject( "UserAccounts.CommonDialog" )

    ' Check arguments and use defaults when necessary
    If myDir = "" Then
        ' Default initial folder is "My Documents"
        objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
    Else
        ' Use the specified initial folder
        objDialog.InitialDir = myDir
    End If
    If myFilter = "" Then
        ' Default file filter is "All files"
        objDialog.Filter = "All files|*.*"
    Else
        ' Use the specified file filter
        objDialog.Filter = myFilter
    End If

    ' Open the dialog and return the selected file name
    If objDialog.ShowOpen Then
        GetFileName = objDialog.FileName
    Else
        GetFileName = ""
    End If
End Function

Open in new window

0
 
LVL 39

Author Closing Comment

by:nutsch
ID: 37257826
Thanks guys, sorry for the closing delay. Rob's solution is working fine.

T
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question