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



## FinTechie (Feb 23, 2012)

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)


----------



## draceplace (Jun 8, 2001)

Hey Scripting guys addressed this issue a while back. Its the last question in this article..


----------



## Rollin_Again (Sep 4, 2003)

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
> 
> ...


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 (Feb 23, 2012)

Thanks Rollin I'll give that a try.


----------



## FinTechie (Feb 23, 2012)

draceplace said:


> Hey Scripting guys addressed this issue a while back. Its the last question in this article..


Thanks for the link I'll give that a try as well.


----------



## FinTechie (Feb 23, 2012)

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


----------



## FinTechie (Feb 23, 2012)

Thanks for the help rollin_again.


----------



## Rollin_Again (Sep 4, 2003)

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


----------

