Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Microsoft Word files with the Microsoft Word Viewer by default

Status
Not open for further replies.

Dublin73

IS-IT--Management
Apr 26, 2005
236
US
Howdy,

What I’m tring to accomplish is this… A login VBScript runs, when a user logs in. The script changes the file association type of .doc files so that they open with the free Microsoft Word viewer, “Microsoft Office Word Viewer 2003”.

I want this to apply to end users based on Windows global groups. If the user is a member of a Windows domain global group called “MicrosoftWordViewer”, any Word documents (.doc files) that the user opens, will automatically launch with the “Microsoft Office Word Viewer 2003”.

I want to apply the same idea to Excel files with a “MicrosoftExcelViewer” Windows global group.

I’ve put together a script, which will make the Word and Excel viewers the default viewers for .doc and .xls files and it works nicely. This is the VBScript…


Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

''''' Change the default viewer for Word files to Microsoft Office Word Viewer 2003

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
strValueName = "Application"
strValue = "WORDVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue


''''' Change the default viewer for Excel files to Microsoft Office Excel Viewer 2003


strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.xls"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.xls"
strValueName = "Application"
strValue = "XLVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

That’s the easy bit. What I don’t know what to do is… make the script configure the Word and Excel viewers for a user only IF the user belongs to a specific global group. For example…

If user is a member of MicrosoftExcelViewer, all of his .xls documents will open with the “Microsoft Office Excel Viewer 2003”

If user is a member of MicrosoftWordViewer, all of his .doc documents will open with the “Microsoft Office Word Viewer 2003”

Any suggestions here would be much appreciated!



 
Set objUser = GetObject("WinNT://domainname/username,user")
For Each aGroup In objUser.Groups
Wscript.Echo aGroup.Name
Next
Set objUser = Nothing

'if you are going to enum .Groups more than once then write it to a dic object for use with the rest of the enums
 
Thanks for responding mrmovie.

As I only attempt scripting occassionally, I'm a novice at best, but I've somehow got the following script to work for the "MicrosoftWordViewer" global group.... and it seems to work consistently. I'll post back when I've included Excel.

On Error Resume Next

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("Wscript.Network")

strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)

For Each strGroup in objUser.MemberOf
strGroupPath = "LDAP://" & strGroup
Set objGroup = GetObject(strGroupPath)
strGroupName = objGroup.CN

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Select Case strGroupName
Case "MicrosoftWordViewer"

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
strValueName = "Application"
strValue = "WORDVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

End Select
Next
 
just a suggestion but i think you code might do the following???

if the user is a member of 30 groups it will iterate through every group and bind to every group....this will take time...perhaps lots over a slow connection with high latency...

seeing as you currently only concerned with 2 groups perhapd it will be more efficient to bind to those 2 groups and do a check it the user if a memeber???

(i know you could improve things currently with exit for statements but we dont like those)

Set objGroup = GetObject(WinNT://domain/groupname,group")
For Each aMember In objGroup.Members
'match here?
Next
Set objGroup = Nothing

or
Set objGroup = GetObject(WinNT://domain/groupname,group")
Set objUser = GetObject(WinNT://domain/username,user")
Msgbox objGroup.IsMember(User.ADsPath)
'infact that looks cak above, is if it is a string comparision then surely .IsMember(WinNT://domain/username,user") will do?
 
Hi mrmovie, thanks again for responding. The ( script... and as a result the ) registry change takes less than a second to carry out?

I don't know enough about VBScript to determine if this script is running through every group looking for a user. It wouldn't appear to be the case?

Anyway, based on what you're saying, is this how you'd recommend scripting it?....

On Error Resume Next

Set objGroup = GetObject(WinNT://domain/MicrosoftWordViewer,group")
For Each aMember In objGroup.Members

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
strValueName = "Application"
strValue = "WORDVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue

Next

Set objGroup = Nothing

Do I understand you correctly? thanks again!
 
'please note the If statement only acting on the current user
'On Error Resume Next

Set objGroup = GetObject(WinNT://domain/MicrosoftWordViewer,group")
For Each aMember In objGroup.Members
If LCase(aMember.Name) = LCase(WshShell.ExpandEnvironmentStrings("%username%") Then
Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
strValueName = "Application"
strValue = "WORDVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
Exit For
End If
Next

Set objGroup = Nothing
 
Guys, thanks for all your help! Based on Mark's FAQ...

FAQ329-5798

I've constructed the following and it works nicely.


ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
'Find the Windows Directory
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName

'Now check for group memberships and map appropriate drives
'Note that this checks Global Groups and not domain local groups.
For Each GroupObj In UserObj.Groups
'Force upper case comparison of the group names, otherwise this is case sensitive.
Select Case UCase(GroupObj.Name)
'Check for group memberships and take needed action
'In the script below, MICROSOFTWORDVIEWER and MICROSOFTEXCELVIEWER are groups.
'Note the use of all upper case letters as mentioned above.
'Note also that the groups must be Global Groups.
Case "MICROSOFTWORDVIEWER"
Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.doc"
strValueName = "Application"
strValue = "WORDVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
Case "MICROSOFTEXCELVIEWER"

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.xls"
oReg.CreateKey HKEY_CURRENT_USER,strKeyPath

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.xls"
strValueName = "Application"
strValue = "XLVIEW.EXE"
oReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
End Select
Next

'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing


'Quit the Script
wscript.quit

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top