Monday, February 21, 2011

Read a Bookmark in a Word Document

Set objWord = CreateObject("Word.Application")
objword.Visible = False
Set objDoc = objWord.Documents.Open("e:\copy.doc")

Do until objDoc.AtEndofStream
                                stxt = objDoc.Readline
                                MsgBox stxt
Loop

objdoc.Close
objWord.Quit

Get Detail of Object Drive


Dim filesys
set filesys = CreateObject("Scripting.FileSystemObject")
Set drv = filesys.GetDrive("e")

select case drv.DriveType
                Case 0: drtype = "Unknown"
                Case 1: drtype = "Removable"
                Case 2: drtype = "Fixed"
                Case 3: drtype = "Network"
                Case 4: drtype = "CD-ROM"
                Case 5: drtype = "RAM Disk"
End Select

WScript.Echo "The specified drive is a " & drtype & " type disk."
WScript.Echo " "
WScript.Echo "Total size is " & drv.TotalSize & ". "
WScript.Echo "Available space is " & drv.AvailableSpace &  ". "
WScript.Echo "Type is " & drv.DriveType &  " "
WScript.Echo "Path is "  & drv.Path &  " "

Dim filesyst, drive
Set filesyst = CreateObject("Scripting.FileSystemObject")
WScript.Echo "Before Condtion"

drive =  filesyst.DriveExists("z")
WScript.Echo drive

                If filesyst.DriveExists("z") Then
                                WScript.Echo("The specified drive does exist.")
                Else
                                WScript.Echo("The specified drive does not exist.")
                End If
WScript.Echo "After Condtion"

Sort Excel Worksheet

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)

objExcel.Cells(1, 1).Value = "4"
objExcel.Cells(2, 1).Value = "1"
objExcel.Cells(3, 1).Value = "2"
objExcel.Cells(4, 1).Value = "3"
objExcel.Cells(1, 2).Value = "A"
objExcel.Cells(2, 2).Value = "B"
objExcel.Cells(3, 2).Value = "C"
objExcel.Cells(4, 2).Value = "D"

Set objRange = objWorksheet.UsedRange
Set objRange2 = objExcel.Range("A1")
objRange.Sort(objRange2)

Tuesday, February 15, 2011

Copy Specifc Data from One Excel to Other

' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
End If
On Error GoTo 0

'Create an object for Excel file

Set xlBook = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx")
Set xlSheet = xlBook.Worksheets(1)


' Select the range of data to be copy
objexcel.Range("D1:D46").Copy
set xlSheet = xlBook.Worksheets("Sheet2")
' Select the range where we want to copy
objexcel.Range("C3").Select
objexcel.Range("c3").Copy


objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

'Release an object

Set xlBook = Nothing
Set xlSheet = Nothing

Write in the Excel

strExcelPath = "C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx"

' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
End If
On Error GoTo 0


'Delete the Excel
objexcel.Worksheets.Delete strExcelPath

' Create a new workbook.
objExcel.Workbooks.Add

' Bind to worksheet by creating an object
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
'objSheet.Name = "User Groups"

' Populate spreadsheet cells with user attributes.
objSheet.Cells(1, 1).Value = "User Common Name"
objSheet.Cells(2, 1).Value = "sAMAccountName"
objSheet.Cells(3, 1).Value = "Display Name"
objSheet.Cells(4, 1).Value = "Distinguished Name"
objSheet.Cells(5, 1).Value = "Groups"

' Enumerate groups and add group names to spreadsheet.
k = 5

objSheet.Range("A1:A4").Font.Bold = True
objSheet.Select
objSheet.Range("B5").Select
objExcel.ActiveWindow.FreezePanes = True
objExcel.Columns(1).ColumnWidth = 20
objExcel.Columns(2).ColumnWidth = 30
objexcel.Worksheets.Add
Set objSheet = objExcel.ActiveWorkbook.Worksheets(4)

Set objSheet = objExcel.ActiveWorkbook.Worksheets(2)
objSheet.Cells(1, 1).Value = "User Common Name"
objSheet.Cells(2, 1).Value = "sAMAccountName"
objSheet.Cells(3, 1).Value = "Display Name"
objSheet.Cells(4, 1).Value = "Distinguished Name"
objSheet.Cells(5, 1).Value = "Groups"

objSheet.Cells(1, 1).Value = "User Common Name"


' Save the spreadsheet and close the workbook.
' Specify Excel7 File Format.

objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close

' Quit Excel.
objExcel.Application.Quit

' Clean Up  OR Realease the object
Set objUser = Nothing
Set objGroup = Nothing
Set objSheet = Nothing
Set objExcel = Nothing

Wscript.Echo "Done"

Get Cell Data

Dim objExcel
Dim oSheet
'Create an object for Excel Application
Set objExcel = WScript.CreateObject("Excel.Application")
Set temp = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx")

'Loop to go through each cell n get the data

Dim intRow , intCol
intRow = 1 : intCol = 1
WScript.Echo " Coloumn : Data "

Do Until objExcel.Cells(intRow,intCol).Value = ""
    Do Until objExcel.Cells(intRow, intCol) = ""
        WScript.Echo   objExcel.Cells(intRow, intCol).Value
        intRow = intRow + 1
    Loop
    WScript.Echo " "
     intCol = intCol + 1
     intRow = 1
Loop

objExcel.Workbooks.Close ()
objExcel.Quit

'Release the object
Set objExcel = Nothing

Get Sub Excel Name

Dim objExcel
Dim oSheet
'Create an object for Excel Application
Set objExcel = WScript.CreateObject("Excel.Application")
Set temp = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\new.xlsx")

MsgBox temp.Worksheets.Count
MsgBox temp.Worksheets.Application


'Loop get the name of sub excel

For Each oSheet In objExcel.Worksheets
    WScript.Echo oSheet.Name
    MsgBox oSheet.name
Next

objExcel.Workbooks.Close ()
objExcel.Quit

'Release the object

Set objExcel = Nothing

Delete the Excel sheet

Dim objExcel

'Create an object for Excel Application
Set objExcel = WScript.CreateObject("Excel.Application")
Set temp = objExcel.Workbooks.Open("C:\Documents and Settings\deepakpe\Desktop\Mine_doc\temp.xlsx")

MsgBox temp.Worksheets.Count
MsgBox temp.Worksheets.Application
temp.Worksheets.Application.DisplayAlerts = false
temp.Worksheets.Delete
objExcel.Workbooks.Close ()
objExcel.Quit

'Release the object
Set objExcel = Nothing

Monday, February 14, 2011

VBscript: Sample script for msg box with class

Class Hello_World
Public Sub Say_Hello(Name)
                        MsgBox "Hello, " & Name & ", welcome to " & Garden & "."
            End Sub
            Public Garden
End Class

Dim MyHello_World

Set MyHello_World = New Hello_World

            MyHello_World.Garden = "Fountain"
            MyHello_World.Say_Hello(MyHello_World.Garden)

            MyHello_World.Say_Hello "Sachin"
            MyHello_World.Say_Hello(MyHello_World.Garden)

Wednesday, February 9, 2011

Debug vbscript & Jscript from Command prompt


How to debug Windows Script Host (WSH) scripts, which can be written in any ActiveX script language (as long as the proper language engine is installed), but which, by default, are written in VBScript and JScript.
There are certain flags in the registry and, depending on the debugger used, certain required procedures to enable debugging.
To debug WSH scripts in Microsoft Visual InterDev, the Microsoft Script Debugger, or any other debugger, use the following command-line syntax to start the script:

               wscript.exe //d

This code informs the user when a runtime error has occurred and gives the user a choice to debug the application. Also, the //x flag can be used, as follows, to throw an immediate exception, which starts the debugger immediately after the script starts running:
               wscript.exe //d //x 

After a debug condition exists, the following registry key determines which debugger will be used:

HKEY_CLASSES_ROOT\CLSID\{834128A2-51F4-11D0-8F20-00805F2CD064}\LocalServer32

The script debugger should be Msscrdbg.exe, and the Visual InterDev debugger should be Mdm.exe.

If Visual InterDev is the default debugger, make sure that just-in-time (JIT) functionality is enabled. To do this, follow these steps:
  1. Start Visual InterDev.
  2. On the Tools menu, click Options.
  3. Click Debugger, and then ensure that the Just-In-Time options are selected for both the General and Script categories.
Additionally, if you are trying to debug a .wsf file, make sure that the following registry key is set to 1:

HKEY_CURRENT_USER\Software\Microsoft\Windows Script\Settings\JITDebug

Friday, February 4, 2011

Creating dictionary using Vbscript


Dim vKey
Dim sItem, sMsg
Dim oDict

'--- Creating a dictionary object
Set oDict = CreateObject("Scripting.Dictionary")

'--- Adding items to dictionary
oDict.Add "One", "Engine"
oDict.Add "Two", "Wheel"
oDict.Add "Three", "Tire"
oDict.Add "Four", "Spanner"

'--- Looping the Collection
For Each vKey In oDict
sItem = oDict.Item(vKey)
'sMsg = sMsg & sItem & vbCrLf
Next

'WScript.Echo (oDict.Count)
'oDict.Item("one") = "temp"
'WScript.Echo sMsg

'wscript.Echo (odict.item(one))
oDict.Key("One") = "nine"
'oDict.Item("nine") = "nine"
oDict.Add "Ten", "ten"

For Each vKey In oDict
sItem = oDict.Item(vKey)
sMsg = sMsg & sItem & " - " &vKey & vbCrLf
Next

WScript.Echo sMsg

WScript.Echo "===============Again============="

WScript.Echo (oDict.Exists ("One"))
WScript.Echo (oDict.Exists ("nine"))
WScript.Echo (oDict.Exists ("Ten"))

'WScript.Echo (odict.item(One))1
'WScript.Echo (odict.item(nine))
'WScript.Echo (odict.item(Ten))

For Each vKey In oDict
sItem = oDict.Item(vKey)
WScript.Echo (sItem & " - " & vKey)
Next

If oDict.Exists("One") Then
WScript.Echo "heello"
End If


If oDict.Exists("nine") Then
WScript.Echo "heello"
End If

Dim temp()
Dim count, counter
count = oDict.Count
WScript.Echo (count)

WScript.Echo
ReDim temp(count)
counter = 0
For Each key In oDict
temp(counter) = key
' WScript.Echo key & " - " & counter
' WScript.Echo temp(counter)
counter = counter+1
Next


'WScript.Echo ("oDict.Keys")

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | JCpenney Printable Coupons