Open DialogBox by using Com Object


posted by Sylvain LESIRE
12-22-2008

Downloads: 420
File size: 425 B
Views: 2,923

Embed
Open DialogBox by using Com Object
  1. $ObjUsrCom = new-object -ComObject UserAccounts.CommonDialog 
  2. $ObjUsrCom.Filter = "VBScripts|*.vbs|Text Documents|*.txt|All Files|*.*" 
  3. $ObjUsrCom.FilterIndex =
  4. $ObjUsrCom.InitialDir = "c:\" 
  5. $InitObjUsrCom = $ObjUsrCom.ShowOpen() 
  6.  
  7. If ( $InitObjUsrCom = $False) { 
  8.     write-host "Script Error: Please select a file!" 
  9.     
  10.     Else
  11.     $filename=$ObjUsrCom.FileName 
  12.     write-host "You selected the file: $filename " 

This script permits to show you how to open a dialog box by using COM Object : UserAccounts.CommonDialog

Comments

Thomas Lee wrote re: Open DialogBox by using Com Object
on 12-22-2008 11:03 AM

Some points:

1. This dialogbox isused in the User Accounts control panel (Nusrmgr.cpl) found in Windows XP.

2. It appears that this os only vaid in Windows XP. It does not work in Server 2008.

3. It's still cool1

Here's a slightly better 1st three lines:

$ObjUsrCom = new-object -ComObject UserAccounts.CommonDialog

$ObjUsrCom.Filter = "PowerShell Scripts|*.ps1|Legacy old scripting rubbish|*.vbs|All Files|*.*"

$ObjUsrCom.FilterIndex = 1

:-)

Sylvain LESIRE wrote re: Open DialogBox by using Com Object
on 12-22-2008 3:38 PM

It is true !!!

It runs only in WinXP and Win2003....

For your code , it is true too, VBS is an old language script, but it was an example ;-)

I will give you a sample with .NET method, but I have a little problem to apply filter option...

See U

Tobias Weltner wrote re: Open DialogBox by using Com Object
on 12-23-2008 3:35 AM

Here is a sample that works on all windows versions and opens a dialog to select a folder:

$object = New-Object -comObject Shell.Application

$folder = $object.BrowseForFolder(0, "Select Folder", 0)

if ($folder -ne $null) {

$folder.self.Path

}

Tobias Weltner wrote re: Open DialogBox by using Com Object
on 12-23-2008 3:41 AM
Sylvain LESIRE wrote re: Open DialogBox by using Com Object
on 12-23-2008 7:53 AM

Cool script too...

but it is not the same things like my code.

this dialogbox permits to select folder, but not a file.. ;-)

Sylvain LESIRE wrote re: Open DialogBox by using Com Object
on 12-23-2008 9:05 AM

@ Thomas

You can download comdlg at

activex.microsoft.com/.../comdlg32.cab

Unzip and register ocx  on W2k8

I think you can run on W2k8 in this case.... ;-)

Anybody can try it ?please

I Don t have w2k8 server...

Ps : if it runs on w2k8 , I will show you other comdlg dialog box

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.