Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Monday, October 20, 2008

How to Get Office Version Programmatically

one of my friend asked me this question: how to get Microsoft Word's version installed on client system?
a few searches and here is the answer so far :
1- use this VBS code [^]:
Set objWord = CreateObject("Word.Application")
Wscript.Echo "Version: " & objWord.Version
Wscript.Echo "Build: " & objWord.Build
objWord.Quit

2- find the Word application path and check the file version (C#) [^] :
read the registry key in "Software\Microsoft\Windows\CurrentVersion\App Paths" to find winword.exe key, and then in the key find Path's value that refer to exe file of Word app.
//looks inside CURRENT_USER:
RegistryKey _mainKey = Registry.CurrentUser;
_mainKey = _mainKey.OpenSubKey(
"Software\Microsoft\Windows\CurrentVersion\App Paths\\"
+ "winword.exe", false);
if (_mainKey == null)
{
//looks inside LOCAL_MACHINE:
_mainKey = Registry.LocalMachine;
_mainKey = _mainKey.OpenSubKey(
"Software\Microsoft\Windows\CurrentVersion\App Paths\\"
+ "winword.exe", false);
}
if (_mainKey == null)
throw new Exception("it does not exist!");


MessageBox.Show(
System.Diagnostics.FileVersionInfo.GetVersionInfo(
_mainKey.GetValue(string.Empty).ToString()).FileVersion);



See the reference for complete code

3- use Microsoft.Office name space in .Net [^]

just this code :

//be aware of exceptions
MessageBox.Show(
new Microsoft.Office.Interop.Word.Application().Version);

Tuesday, September 09, 2008

Some Usefull VBScript Codes

Using shell scripts like vbscript can make your work much easier specially when you are a sys admin, you can run them in login process or any other OS specified time or even run them in a custom time/way, there is several ways to run them but I prefer to save the text in a text file with the extension of .vb like loginScript.vb it would be run like a executable remember that you should have installed Windows Host Scripting on machine, here is some small vbscript snippets :

Basic output


  • Wscript.Echo "Hello World,this is one way to display output..."

  • MsgBox "Hello World," & "this is another way..."


  • Basic input


  • Dim V
    V = InputBox("What the heck?")

  • Getting if user belonged to specified group


  • 'returns boolean

    Set oNetwork = CreateObject("WScript.Network")
    sDomain = oNetwork.UserDomain
    sUser = oNetwork.UserName
    bIsMember = False
    Set oUser = GetObject("WinNT://" & sDomain & "/" & _
    sUser & ",user")

    For Each oGroup In userObj.Groups
    If oGroup.Name = "Domain Users" Then
    bIsMember = True
    Exit For
    End If
    Next

  • Mapping


  • 'mapping drive
    Set oNet = WScript.CreateObject("WScript.Network")
    oNet.MapNetworkDrive "X:", "\\theServer\XFiles)

  • 'Mapping Printer
    Set oNet = WScript.CreateObject("WScript.Network")
    oNet.AddWindowsPrinterConnection "\\PrintServer\SecretPrinter"
    oNet.SetDefaultPrinter "\\PrintServer\SecretPrinter"

  • Getting IP address


  • 'localIp is the result
    Set myObj = GetObject("winmgmts:{impersonationLevel=impersonate}" & _
    "!//localhost".ExecQuery_
    ("select IPAddress from " & _
    "Win32_NetworkingAdapterConfiguration" & _" where IPEnabled=TRUE")
    'Go through the addresses
    For Each IPAddress in myObj
    If IPAddress.IPAddress(0) <> "0.0.0.0" Then
    LocalIP = IPAddress.IPAddress(0)
    Exit For
    End If
    Next
  •