如何通过VBA截取Attachmate Reflection(IBM屏幕)窗口截图并粘贴至Word文档?解决objWord.Paste报错问题
Let's break down why your paste is failing and fix it, plus a more reliable alternative to using keybd_event for capturing the Attachmate screen.
Why the Current Paste Fails
The error happens because you're calling objWord.Paste (the Application-level paste method) without having a valid selection or Range context in Word. Word's Application.Paste expects an active selection or targeted range to paste into—when you call it directly like that, it doesn't know where to place the content, hence the "not supported" error.
Quick Fix for the PrintScreen Approach
Adjust your code to target a specific Range in the Word document instead of using the Application-level paste. Also, add a small delay to ensure the clipboard has the screenshot ready after the keybd_event call:
' Define the PrintScreen key constant at the top of your module Const VK_SNAPSHOT = &H2C ' ... your existing code up to the keybd_event line ... Call keybd_event(VK_SNAPSHOT, 0, 0, 0) ' Add a short delay to let the clipboard capture the screenshot Application.Wait Now + TimeValue("00:00:01") ' Target the end of the document to paste Dim targetRange As Range Set targetRange = objDoc.Content targetRange.Collapse Direction:=wdCollapseEnd targetRange.Paste ' Use Range.Paste instead of objWord.Paste ' ... rest of your code (quit Word, etc.) ...
Better Alternative: Use Attachmate Reflection's Native API
Relying on keybd_event is fragile—it depends on the window being active, clipboard state, and timing. Attachmate Reflection has built-in methods to capture the screen directly as an image, which is far more reliable.
Here's how to modify your code to use this approach:
'Reflection screen part Set view = frame.CreateView(terminal) Set screen = terminal.Screen ' Capture the Reflection screen as an StdPicture object Dim screenPic As StdPicture Set screenPic = view.CaptureScreen() ' Word document part Set objWord = CreateObject("Word.Application") Set objDoc = objWord.Documents.Add objWord.Visible = True ' Add your text first Dim para As Paragraph Set para = objDoc.Paragraphs.Add para.Range.Text = Inp_Str & vbCrLf & vbCrLf & vbCrLf para.Range.ParagraphFormat.SpaceAfter = 10 objDoc.Paragraphs.Add.Range.Text = "Line 2 hello" ' Insert the captured image at the end of the document Dim targetRange As Range Set targetRange = objDoc.Content targetRange.Collapse Direction:=wdCollapseEnd objDoc.InlineShapes.AddPicture _ FileName:="", _ LinkToFile:=False, _ SaveWithDocument:=True, _ Range:=targetRange ' Replace the placeholder picture with our screen capture objDoc.InlineShapes(objDoc.InlineShapes.Count).Picture = screenPic ' ... rest of your code (quit Word, etc.) ...
Notes for the Native API Approach:
- Add references to "Microsoft Word xx.x Object Library" and "StdPicture and StdTextFont" (under the "COM" tab in Tools > References) for this to work, or use late binding if you prefer compatibility across versions.
- This method avoids clipboard issues entirely and ensures you capture exactly the Attachmate screen, even if other windows are in front.
Final Checks
- If using the PrintScreen method, confirm the Attachmate Reflection window is active before triggering the screenshot.
- For the native API, verify your
viewandterminalobjects are correctly instantiated and connected to an active session.
内容的提问来源于stack exchange,提问作者Lakshanth C

