Monday, March 4, 2013

How to add an image at the end of the Word document

Here i divided the task into 2 steps.
1. Capture the desktop image/application.
2. Insert the captured image at the end of the word document.

sImageFilePath="D:\TempImage.png"
sWordFilePath="D:\Programming Samples\QTP\SampWord.docx"

CapturePrintScreen sImageFilePath
AddImageToWord sWordFilePath,sImageFilePath

Sub CapturePrintScreen(sFilePath)
   Desktop.CaptureBitmap sFilePath,true
End Sub

Sub AddImageToWord(sWrdFilePath,sImgFilePath)
   Const END_OF_STORY = 6
    Const MOVE_SELECTION = 0
   Set oWord=createobject("Word.Application")
    oWord.Visible=true

    set oDoc=oWord.Documents.Open(sWrdFilePath)

    Set oSelection=oWord.Selection
    oSelection.EndKey END_OF_STORY,MOVE_SELECTION

    oSelection.InlineShapes.AddPicture(sImgFilePath)

    oDoc.Save

    oWord.Application.Quit
   
    Set oWord=nothing
End Sub

No comments: