Wednesday, March 23, 2011

How to create a word document from QTP?

Dim objWordDoc

' Initaiate the Word Document object
Set objWordDoc = CreateObject(“Word.Application”)

' New word document
objWordDoc.Documents.Add

' Type some charecters in the newly created document
objWordDoc.Selection.TypeText "My first Line" & vbnewline & "My Second Line"

' Save the document
objWordDoc.ActiveDocument.SaveAs "C:\QTP\Docs\MyWordDoc.doc"

' Close word
objWordDoc.Quit

'Release the object
Set objWordDoc=Nothing


For more QTP example scripts click here

17 comments:

  1. Helpful post for beginners like me... please keep posting such stuffs it will helps a lot... thanks :)

    ReplyDelete
  2. Sure, will do that when I find time.

    ReplyDelete
  3. Hey i'm getting error for this program.. Can you mention the details

    ReplyDelete
    Replies
    1. In which line You are Getting Error Plz Mention So We Can help you.

      Delete
  4. Please copy paste the error details.

    ReplyDelete
  5. Invalid character

    Line (1): "Set objWordDoc = CreateObject(“Word.Application”)".

    ReplyDelete
    Replies
    1. here within Create object use double inverted comma as in QTP script editor

      Delete
  6. You have included the open inverted and close inverted comma's instead of "

    watch the ones in your code.. it is not "

    ReplyDelete
  7. This gives error at save statement...
    i.e for below statement,
    objWordDoc.ActiveDocument.SaveAs "C:\QTP\Docs\MyWordDoc.doc"

    ReplyDelete
    Replies
    1. hay just give instead of that u r won path that stored in u r system ok c drive r d drive

      Delete
  8. Will same format work for remaining file types(excel or xml , ) ...pls help me

    ReplyDelete
  9. How to create a excel or html file in qtp ...plz help me

    Set objWordDoc = CreateObject(“excel.Application”)i'm getting a error if i write like this .

    ReplyDelete
  10. Hi guys,

    I'm Having trouble saving the word doc. It gives me the following error. Pls help.

    Word cannot save this file because it is already open elsewhere.
    (C:\...\Desktop\UFT Screenshots.docx)

    Line (144): "objWordDoc.ActiveDocument.SaveAs "C:\Documents and Settings\324025351\Desktop\UFT Screenshots"".

    ReplyDelete
  11. Super!!! It works! Thanks... Would greatly appreciate if you could provide the code to create Excel file with different column names (let's say "Links Name" and "URL"). Best regards,

    ReplyDelete
  12. chage it as

    Set objWordDoc = CreateObject("Word.Application")

    Darshan Thakar

    ReplyDelete

Please leave your comment here...