Saturday, January 3, 2009

QTP Script 25 - How to retrieve data from a word document after removing the blank lines?

'Write the below code in a notepad and save it with .vbs extension and run it from command prompt. Remember to change the path of your file in the second line. I ran it using MS Word 2003.

Set word_app = CreateObject("Word.Application")
Set word_doc = word_app.Documents.Open("C:\wrd.doc")

'Counting the number of paragraphs. Blank lines are also considered as paragraphs, that is why we have an “if loop” below which helps in removing the blank lines. It does not matter how many blank lines are there in between two paragraphs, it removes all.

para_count = word_doc.Paragraphs.Count

For a = 1 to para_count

'Using paragraph and range object to get one paragraph at a time.
start_range = word_doc.Paragraphs(a).Range.Start
end_range =word_doc.Paragraphs(a).Range.End
Set t_range = word_doc.Range(start_range, end_range)
x_str = t_range.Text
if len(x_str) = 1 then
'Do Nothing
else
msgbox x_str
end if
Next

word_app.Quit
Set word_doc = Nothing
Set word_app = Nothing