0

I used the MS-Word VBA macro recorder to create a screenshot shortcut macro in MS-Word 2016 / Word 365. The problem with the Windows 10 Pro Snipping Tool is that it creates its own window that gets in the way and requires an extra couple of steps to execute and save the screen capture.

Adding the button to the QuickAccess Toolbar also does not give me a way to delay for 1-2 seconds while the value of a chart object (price charts in ThinkOrSwim) at the current location of the mouse cursor refreshes. The QuickAccess Toolbar also requires 2 mouse clicks instead of a simple 1-click action. A quick 3-finger shortcut is faster than the extra mousing around.

The keyboard sequence macro initially generated the following error, but I was able to fix it by including the PtrSafe keyword before the Sub keyword:

Compile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute.

However, now the macro generated below does not use the correct document name. It always grabs the exact screenshot from the clipboard and pastes that into the document multiple times.

The SaveWithDocument:= True is correct, but the FileName:= C:\Users\POWERU~1\AppData\Local\Temp\21479D6.tmp value is wrong.

How do I update this macro to get a new image after a 1-2 second delay and then save it into the current MS-Word document filename?

Sub ScreenCapture()
'
' ScreenCapture Macro
'
'
    Selection.InlineShapes.AddPicture FileName:=current
        SaveWithDocument:=True
End Sub
Sub ScreenCaptureShortcut()
'
' ScreenCaptureShortcut Macro
'
'
    Selection.InlineShapes.AddPicture FileName:= _
        "C:\Users\POWERU~1\AppData\Local\Temp\21479D6.tmp", LinkToFile:=False, _
        SaveWithDocument:=True
End Sub

What I also tried was using the Macro Recorder to capture the keyboard shortcut strokes and assigning it to a button, but it still uses a temp file, instead of the current MS-Word file.

Sub ScreenCaptureButton()
'
' ScreenCaptureButton Macro
'
'
    Selection.InlineShapes.AddPicture FileName:= _
        "C:\Users\POWERU~1\AppData\Local\Temp\214774D.tmp", LinkToFile:=False, _
        SaveWithDocument:=True
    ActiveDocument.Save
End Sub

I never liked using VBA in Office, because it has a complex object model, with horrid dependencies. I use it so infrequently that I must re-learn it almost every time I go to use it. Next I will try using Python to automate this, because at least I fully understand the Python object model and there are Python packages to automate the use of Office apps.

Any assistance to get a working solution to this will be most appreciated.

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44

0 Answers0