Solved

How do run a vbscript from vba while referencing activeworkbook

Posted on 2011-09-16
8
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

751 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