Solved: Vbscript UserAccounts.CommonDialog doesn't work in Window 7

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

FinTechie

Thread Starter
Joined
Feb 23, 2012
Messages
12
I understand this was an undocumented feature in WinXP, but I used it a lot to present an open file dialogue box and allow the user toselect a file to open. Below is thescript that I previously used along with the error I get when i try to run it.
I have looked around the internet for a solution, but theyall seem long and complicated. Hasanyone found an easy way to write this script so that it works in Windows 7?

Error Description: If I attempt to use the script in Windows 7 I get the following error...Windows script host. ActiveX componentcan't create object. Code 800A01AD.
Set ObjFSO =CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "VBScripts|*.vbs|TextDocuments|*.txt|All Files|*.*"
ObjFSO.FilterIndex = 3
ObjFSO.InitialDir = "c:\"
InitFSO = ObjFSO.ShowOpen
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(ObjFSO.FileName)
 
Joined
Sep 4, 2003
Messages
4,912
I'm a bit confused as to why you are doing it this way. If you are already creating an instance of Excel why aren't you using the open file dialogue that is built into Excel? See my example below.

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True

vFileName = objExcel.GetOpenFilename ("Text Files (*.txt),*.txt,VBS Files (*.vbs),*.vbs,All Files (*.*),*.*")

Set objWorkbook = objExcel.Workbooks.Open(vFileName)
If you want the dialogue to open in a particular directory simply use the CHDIR command to set the working directory before executing the call.

ChDir ("C:\")

vFileName = objExcel.GetOpenFilename ("Text Files (*.txt),*.txt,VBS Files (*.vbs),*.vbs,All Files (*.*),*.*")
Rollin
 

FinTechie

Thread Starter
Joined
Feb 23, 2012
Messages
12
I was originally on Win XP and found that code snippet on the internet. It served my purpose and worked well, but apparently was an undocumented feature that was depricated in Win 7 (or Vista). Anyway yes i re-worked it with your suggestion and it appears to be working well now. Below is my new code.


Set objFiles = CreateObject("Excel.Application")

FileName = objFiles.Application.GetOpenFilename("ExcelFiles (*.xl*), *.xl*")



If FileName = False Then

Wscript.Echo"Script Error: Please select a file!"

Wscript.Quit

Else

End If



Set objExcel = CreateObject("Excel.Application")

Set objWorkbook = objExcel.Workbooks.Open(FileName)



‘’’’’’’’’’

‘’’Additional Code working with the file

‘’’’’’’’’





'Save and close Excel

objExcel.DisplayAlerts = False

objExcel.Save

objExcel.Quit
 
Joined
Sep 4, 2003
Messages
4,912
Glad I could help out. Just wanted to point out one thing with your code. You appear to be creating two instances of the Excel object.

Set objFiles = CreateObject("Excel.Application")
.
.
.
Set objExcel = CreateObject("Excel.Application")
This is really redundant and not needed since you can use the first Excel instance you created to both call the open file dialogue and then later open the workbook. Was there a particular reason why you created two instances instead of just referring to the first instance when opening the workbook further in your code?

Rollin
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top