Monday, December 17, 2012

Automating Word Document- 1

How to print all words in a word document?
Dim oWord, Doc, currentDocument
Doc = "C:\Desktop\Test.doc" 'contains 200 words
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = 0
oWord.Documents.Open Doc
Set currentDocument = oWord.Documents(1)
Dim nWords, n
nWords = currentDocument.Words.count
Print  "Number of Words:  " & nWords
For n = 1 to nWords
     Print currentDocument.Words(n)
Next
currentDocument.Close
Set currentDocument = Nothing
oWord.Quit
Set oWord = Nothing
********************************************
 How to count number of pages in a word document?
Below lines of code shows how to find total number of pages in a word document:
SystemUtil.CloseProcessByName("WINWORD.exe")
Dim oWord, Doc
Doc = "C:\Desktop\Test.doc" 'contains 20 pages
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open  doc
Dim nPages
nPages=oWord.ActiveDocument.BuiltInDocumentProperties("Number of pages")
msgbox nPages ' will display total number of pages
ActiveDocument.Close
Set ActiveDocument = Nothing
oWord.Quit
Set oWord = Nothing

Killing a Process

Below is the function that is used to kill any process:
Public Function fnProcessKill (ByVal strProcess)
Dim FunctionName: FunctionName = UCase ("fnProcessKill")
Dim strPreFix: strPreFix = "In " & pcThisLib & " - " & FunctionName & ","
Set objSWbemServices = GetObject("winmgmts:")
Set colSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM Win32_Process Where Name = '"& strProcess &"'")
If colSWbemObjectSet.Count > 0 Then
For i = 1 to colSWbemObjectSet.Count
SystemUtil.CloseProcessByName(strProcess)
Next
Else
fnProcessKill = micFail ' Did not find the process.
End If
Set objSWbemServices = Nothing ' Clear object from memory.
Set colSWbemObjectSet = Nothing ' Clear object from memory.
End Function

Function to close All Browsers except ALM

'Name: platform_web_CloseAllBrowsersExceptALM ()
'Desc: Closes all browsers except ALM
'Date Created:
'Author:
'Input Parameters: i
Public Sub platform_web_CloseAllBrowsersExceptALM ()
Call CloseAllBrowsersExceptALM ()
End Sub
Public Sub CloseAllBrowsersExceptALM ()
'Clean up the open browser windows from previous test
Dim vcol_Handles, vCtr, vHwnd, Flag, vLastHWnd,sCompare
vCtr = 0
Flag = 1
Set vcol_Handles = CreateObject("Scripting.Dictionary")
While (Window("regexpwndclass:=IEFrame","index:=" & vCtr).Exist(0) And Flag)
'wait 1
vHwnd = Window("regexpwndclass:=IEFrame","index:=" & vCtr).getroproperty("Hwnd")
If (vLastHWnd=vHwnd) Then
Flag = 0
Else
vcol_Handles.Add CStr(vcol_Handles.Count),vHwnd
vCtr = vCtr+1
End If
vLastHWnd = vHwnd
Wend
'Use reverse order in this for loop so that the text and Hwnd remain in correct order after each is closed
For vCtr = vcol_Handles.Count-1 to 0 step -1
vHwnd = vcol_Handles.Item(CStr(vCtr))
' Determine if the browser window has the word ALM in the caption bar
sALMCompareString = "HP Application Lifecycle Management"
sCompare=Instr(Window("hwnd:=" &vHwnd).getroproperty("title"),sALMCompareString)
'sCompare=Instr(Window("hwnd:=" &vHwnd).getroproperty("title"),"Quality Center")
' If it doesn't or the title bar is empty, close the window
If (sCompare=0) or (IsNull(sCompare)) then
Window("hwnd:=" &vHwnd).Close
End If
Next
End Sub

Function to sort Web table by COLUMN NAME in ASCending or DESCending order

sColumn_Name="Type/"
'declaring variables
Dim arrDCol(1000)
Dim arrDSortCol(1000)
Dim arrCol(1000)
Dim arrSortCol(1000)
If Browser("").Page("").WebTable("").Exist(5) Then
intRowcnt = Browser("").Page("").WebTable("").RowCount
intColcnt = Browser("").Page("").WebTable("").ColumnCount(1)
' adding and comparing table headers
Set ColList = CreateObject("System.Collections.ArrayList")
j=0
For j = 3 to intColcnt
Set objCmp = Browser("").Page("").WebTable("")
strCol= trim(objCmp.GetCellData(1,j))
if(instr(strCol,sColumn_Name)) then
sFinColumn_Name=sColumn_Name
sFinColNo=j
Exit FOR
end if
Next
‘clicking on table column header which you want to sort in ascending order
set oLink=Browser("").Page("").WebTable("").ChildItem(1,sFinColNo,"Link",0)
Browser("").Page("").Sync
Set AppDataList = CreateObject("System.Collections.ArrayList")
Set SortASCList = CreateObject("System.Collections.ArrayList")
Set SortDESCList = CreateObject("System.Collections.ArrayList")
i=0
If intRowcnt>1 Then
For i = 3 to intRowcnt
Set objCmp = Browser("").Page("").WebTable("")
AppDataList.Add trim(objCmp.GetCellData(i,sFinColNo))
AppDataList.remove("")
AppDataList.TrimToSize
SortASCList.Add trim(objCmp.GetCellData(i,sFinColNo))
SortASCList.remove("")
SortASCList.TrimToSize
Next
SortASCList.Sort
n = 0
For each se in AppDataList
arrCol(n) = se
n = n +1
Next
m = 0
For each ses in SortASCList
arrSortCol(m) = ses
m = m +1
Next
i=0
For i = 0 to n
If trim(arrCol(i)) <> trim(arrSortCol(i)) then
sortFlag = sortFlag+1
strMatch = strMatch&"Unsorted : "&arrCol(i)& "- Sorted in ascending order : "&arrSortCol(i)&";"
Else
strMatch = strMatch&"sorted : "&arrCol(i)& "- Sorted in ascending order : "&arrSortCol(i)&";"
End If
Next
If sortFlag>0 Then
Reporter.ReportEvent micFail, " Table data is not displayed in sorted order - Ascending","" &strMatch
Else
Reporter.ReportEvent micPass, " Table data is displayed in sorted order - Ascending ","" &strMatch
End If
'sortin in descending order
set oLink=Browser("").Page("").WebTable("").ChildItem(1,sFinColNo,"Link",0)
oLink.Click
Browser("").Page("").Sync
Set DESCList = CreateObject("System.Collections.ArrayList")
i=0
For i = 3 to intRowcnt
Set objCmp = Browser("").Page("").WebTable("")
DESCList.Add objCmp.GetCellData(i,sFinColNo)
DESCList.remove("")
DESCList.TrimToSize
SortDESCList.Add trim(objCmp.GetCellData(i,sFinColNo))
SortDESCList.remove("")
SortDESCList.TrimToSize
Next
n = 0
For each se in DESCList
arrDCol(n) = se
n = n +1
Next
SortDESCList.Sort
SortDESCList.Reverse
m = 0
For each ses in SortDESCList
arrDSortCol(m) = ses
m = m +1
Next
strMatch=""
sortFlag=0
i=0
For i = 0 to n
If trim(arrDCol(i)) <> trim(arrDSortCol(i)) then
sortFlag = sortFlag+1
strMatch = strMatch&"Unsorted : "&arrDCol(i)& "- Sorted in descending order : "&arrDSortCol(i)&";"
Else
strMatch = strMatch&"sorted : "&arrDCol(i)& "- Sorted in descending order : "&arrDSortCol(i)&";"
End If
Next
If sortFlag>0 Then
Reporter.ReportEvent micFail, " Table data is not displayed in sorted order - Descending","" &strMatch
Else
Reporter.ReportEvent micPass, " Table data is displayed in sorted order - Descending ","" &strMatch
End If
else
Reporter.ReportEvent micWarning, " No data available to sort",""
End If
else
Reporter.ReportEvent micWarning, " No table exists",""
End If

Queries,Code Writing in QTP

Hi Everyone,
If you have any queries in QTP or looking for assistance in code /logic Please do describe the issue in the comments section.Your queries will be resolved ASAP.
Ankur Jain

Function to create File shortcut

Fn_FileShortcut()
Set WshShell=createobject(“WScript.shell”)
strDesktop=WshShell.SpecialFolders(“Desktop”)
set appConfigShortcut=WshShell.CreatShortcut(strDeskTop &  “\AppConfig.lnk”)
appConfigShortcut.TargetPath=”D:\QTP\Test.txt”
appConfigShortcut.IconLocation=”notepad.exe”
appConfigShortcut.HotKey=”CTRL+ALT+C”
appConfigShortcut.WindowStyle=1
appConfigShortcut.Description=”Application config file”
appConfigShortcut.WorkingDirectory=strDesktop
appConfigShortcut.Save

Code Optimisation in QTP

Hi Everyone,
I am looking for suggestions to optimisation in QTP.Please share if you have any :)