1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by FinTechie, Jan 17, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. FinTechie

    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)
     
  2. draceplace

    draceplace

    Joined:
    Jun 8, 2001
    Messages:
    2,583
    Hey Scripting guys addressed this issue a while back. Its the last question in this article..
     
  3. Rollin_Again

    Rollin_Again

    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.

    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.

    Rollin
     
  4. FinTechie

    FinTechie Thread Starter

    Joined:
    Feb 23, 2012
    Messages:
    12
    Thanks Rollin I'll give that a try.
     
  5. FinTechie

    FinTechie Thread Starter

    Joined:
    Feb 23, 2012
    Messages:
    12
    Thanks for the link I'll give that a try as well.
     
  6. FinTechie

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

    FinTechie Thread Starter

    Joined:
    Feb 23, 2012
    Messages:
    12
    Thanks for the help rollin_again.
     
  8. Rollin_Again

    Rollin_Again

    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.

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

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1085713

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice