Quick test professional

QTP Tips QTP codes QTP Faqs and more

How To open Password Protected Excel sheets

Function UnprotectXL(filePath,fileName,pwd,writeresPwd)
   Set objExcel=CreateObject(“Excel.Application”)
   objExcel.Visible=false
   testData=filePath&”\”&fileName
 
   Set oWorkbook=objExcel.Workbooks
 
   Set myWkbook=objExcel.Workbooks.open (testData,0,False,5,pwd,writeresPwd)
   objExcel.DisplayAlerts=False
   oWorkbook(fileName).Activate
   For Each w in objExcel.Workbooks
        w.SaveAs testData,,””,””
      
   Next
 
   objExcel.Workbooks.Close
   objExcel.Quit
   Set oWorkbook=Nothing
   Set objExcel=Nothing
  
End Function
Function ProtectXL(filePath,fileName,pwd,writeresPwd)
     On Error Resume Next
     Set objExcel=CreateObject(“Excel.Application”)
     objExcel.Visible=False
     testData=filePath&”\”&fileName
     Set oWorkbook=objExcel.Workbooks
     Set outputWkbook=objExcel.Workbooks.open (testData,0,False)
     oWorkbook(testData).Activate
     objExcel.DisplayAlerts=False  
     outputWkbook.SaveAs testData,,pwd,writeresPwd
   
     outputWkbook.Close
     objExcel.Workbooks.Close
     objExcel.Quit
     Set outputWkbook=Nothing
     Set objExcel=Nothing
  
End Function

‘Call ProtectXL(“C:\Documents and Settings\kmohankumar\Desktop”,”4.xls”,”test123″,”test123″)
‘Call UnprotectXL(“C:\Documents and Settings\kmohankumar\Desktop”,”4.xls”,”test123″,”test123″)

Author: Mohan Kakarla

Advertisements

November 27, 2008 Posted by | Excel Automation | 15 Comments

Merge 2 XML using Dotnet Factory

Dim objReader1,objReader2,objds1,objds2
Set objReader1 = DotNetFactory.CreateInstance(“System.Xml.XmlTextReader”, “System.Xml”,”C:\Documents and Settings\kmohankumar\Desktop\11.xml”)’ Creates the instance for the XMLWriter
Set objReader2 = DotNetFactory.CreateInstance(“System.Xml.XmlTextReader”, “System.Xml”,”C:\Documents and Settings\kmohankumar\Desktop\12.xml”)’
Set objds1 = DotNetFactory.CreateInstance(“System.Data.DataSet”, “System.Data”)
Set objds2 = DotNetFactory.CreateInstance(“System.Data.DataSet”, “System.Data”)
objds1.ReadXml(objReader1)
objds2.ReadXml(objReader2)
objds1.Merge(objds2)
objds1.WriteXml(“C:\Documents and Settings\kmohankumar\Desktop\13.xml”)
objReader1.close
objReader2.close
Set objReader1=Nothing
Set objReader2=Nothing
Set objds1=nothing
Set objds2=nothing
  

Author:Mohan Kakarla

November 27, 2008 Posted by | DotNetFactory, XML Scripting | 1 Comment

Create XML using Dotnet Factory

 Dim objWriter,XmlWriter
   Set objWriter = DotNetFactory.CreateInstance(“System.Xml.XmlWriter”, “System.Xml”)’ Creates the instance for the XMLWriter
 set XmlWriter=objWriter.Create(“C:\Documents and Settings\kmohankumar\Desktop\1234.xml”)’ creates xml document
 
 XmlWriter.WriteStartElement(“Books”)’ start writing the element
 XmlWriter.WriteStartElement(“Author”)

XmlWriter.WriteAttributeString “Name”, “Mohan”‘ writes  attributes and its values to the element
 XmlWriter.WriteEndElement()’ ends writing the element
 XmlWriter.WriteStartElement(“Title”)

XmlWriter.WriteAttributeString “Title1”, “QTP”‘
 XmlWriter.WriteEndElement()
 XmlWriter.WriteFullEndElement()

XmlWriter.close()

Set objWriter =Nothing

Author:Mohan kakarla

November 27, 2008 Posted by | DotNetFactory, XML Scripting | 3 Comments

Read XML using Dotnet Factory

Set objReader = DotNetFactory.CreateInstance(“System.Xml.XmlReader”, “System.Xml”)
set XmlReader=objReader.Create(“C:\Documents and Settings\kmohankumar\Desktop\3.xml”)

while (XmlReader.Read())

If  XmlReader.NodeType = “XmlDeclaration” or XmlReader.NodeType = “Element”Then
  
    Reporter.ReportEvent micPass, “NodeType:”&XmlReader.NodeType &”  Name :”& XmlReader.Name& ” is at Depth:”& XmlReader.Depth,””

    If XmlReader.HasValue Then
        Reporter.ReportEvent micPass, ” Value: “& XmlReader.Value,””
    End If

    Reporter.ReportEvent micPass, “Element:”& XmlReader.Name&”  has attribute(s)”&”:”& XmlReader.AttributeCount,””

  If XmlReader.AttributeCount > 0 Then

        while  XmlReader.MoveToNextAttribute()
     fn = XmlReader.Name
     ns = XmlReader.NamespaceURI
      Reporter.ReportEvent micPass,” Attribute:”&fn &”  Value: “& ns&” Attribute:”& XmlReader.GetAttribute(fn, ns),””
        wend
     End If
End If
wend
XmlReader.Close()

Author: Mohan Kakarla

November 27, 2008 Posted by | DotNetFactory, XML Scripting | 3 Comments