cancel
Showing results for 
Search instead for 
Did you mean: 

SAP GUI Scripting trying to paste screenshot of SAP to excel sheet(HardcopyToMemory)

Former Member
0 Kudos

Dear Experts,

I have been trying to access the clipboard for screenshot of SAP copied through "session.findById("wnd[0]").HardcopyToMemory" and pasting the same in an Excel Sheet.

I have been trying the same for last two weeks unsuccessfully.

Requesting for your Kind help if someone have already addressed this issue.

Please let me know if i have to furnish some more details..

Regards,

Kumar Santharam

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor

Hello.

Shortest way is copy Picture as file and insert into Excel from file. After this is done delete created file.

With some smart code using shapes you can Position Pictures on sheet.


Sub image_sap()

If Not IsObject(Applic) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set Applic = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = Applic.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If

session.findById("wnd[0]").HardCopy "C:\tmp\test.jpg", 1
ActiveWorkbook.ActiveSheet.Pictures.Insert "C:\tmp\test.jpg"
Kill "C:\tmp\test.jpg"

End Sub

Former Member
0 Kudos

Thanks alot Holger for sharing this cool piece of code... This has addressed my issue... 

In the meanwhile can u please shed some light into when really we have to use the  "session.findById("wnd[0]").HardcopyToMemory" code...

holger_khn
Contributor
0 Kudos

Hello. In SAP Scripting Help file (CHM) you can find this Piece of code:


If Not IsObject(application) Then

   Set SapGuiAuto = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

If Not IsObject(session) Then

   Set session = connection.Children(0)

End If

Image = session.findById("wnd[0]").HardCopyToMemory()

Const adTypeBinary = 1

Const adSaveCreateOverWrite = 2

 

Dim BinaryStream

Set BinaryStream = CreateObject("ADODB.Stream")

 

BinaryStream.Type = adTypeBinary 

BinaryStream.Open

BinaryStream.Write Image

BinaryStream.SaveToFile "C:\screenshot.bmp", adSaveCreateOverWrite

MsgBox "Done"

With some API-functions you should also be able to read Image from Memory and paste in Excel sheet. But first Piece of code is much smaller.

Former Member
0 Kudos

Hi Holger,

Thanks again for sheding some light on the 'HarcopyToMemory' code...

Have a Great day ahead,

Kumar Santharam

0 Kudos

Hello Holger Kohn,

I tried the code you mentioned but it is still giving me error as Object required - ActiveWorkbook.

Appreciate if you could help resolve this error.

Regards,

Nikhil

holger_khn
Contributor
0 Kudos

Hello.


ActiveWorkbook is the Excel instance from where you call the code. It could be as well ThisWorkbook or a specific one. This is up to you how you define your code in Excel module. Of course this will not run from a vbs file. Best regards, Holger

Answers (0)