PDA

View Full Version : [SOLVED:] "the drawing operation cannot be applied to the current selection"



AJS
01-04-2017, 02:57 PM
Hi There,

I recently wrote a small (crude) macro to paste, resize and position a copied picture into a word document. It was working fine when I finished it late last year. Yesterday it was working fine, except that the vertical alignment switched from the top of the page to the top of the margin. This morning it crashes immediately after the paste operation, and I get the following error message: "Run-time error '4605': The RelativeVerticalPosition method or property is not available because the drawing operation cannot be applied to the current selection". My code is below. Any idea what might be going wrong?

Thanks, Aaron

Edit: I'm running Windows 7 and Office 2013.


Sub MCGTrace()

Selection.Paste
Selection.ShapeRange.ScaleHeight 0.9, msoTrue
Selection.ShapeRange.ScaleWidth 0.9, msoTrue

Selection.ShapeRange.RelativeVerticalPosition = wdRelativeVerticalPositionPage

Selection.Application.WordBasic.AlignCenterHorizontal
Selection.Application.WordBasic.AlignTop

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

End Sub

macropod
01-04-2017, 03:33 PM
The problem is most likely that you've configured Word to paste the shape in the in-line format rather than as a wrapped shape. Inlineshapes don't have a 'RelativeVerticalPosition' property. The following code should address that issue - and implement the positioning using VBA instead of WordBasic.

Sub MCGTrace()
Dim Shp As Shape
With Selection
.Collapse wdCollapseStart
.Paste
If .ShapeRange.Count = 1 Then
Set Shp = .ShapeRange(1)
Else
.Start = .Start - 1
Set Shp = .InlineShapes(1).ConvertToShape
End If
With Shp
.ScaleHeight 0.9, msoTrue
.ScaleWidth 0.9, msoTrue
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Top = wdShapeTop
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.Left = wdShapeCenter
End With
'.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With
End Sub
Note that I've commented-out the last code line - it's not needed for simply centring a wrapped image.

AJS
01-04-2017, 04:53 PM
Thank you, it works perfectly! As an aside, any idea why my code might have stopped working overnight? The odd thing is that both yesterday and today I was working on the same document, so the in-line vs wrapped formatting setting should not have changed...

macropod
01-04-2017, 05:11 PM
As an aside, any idea why my code might have stopped working overnight?
It could be something as simple as the image you copied having been formatted as inline or because you were copying from another app and you'd set Word's default 'Insert/paste picture as' format to inline (see under File|Options|Advanced>Cut, copy & paste).

AJS
01-05-2017, 07:21 PM
... and unfortunately, this afternoon I get a new error message: "Run-time error '5941': The requested member of the collection does not exist". The error occur after the paste operation, and the debugger takes me to the following line:

Set Shp = .InlineShapes(1).ConvertToShape

macropod
01-05-2017, 08:07 PM
Did you actually paste an image?

AJS
01-05-2017, 08:17 PM
Yes, an image was pasted while the macro was running.

I just tried the macro again on the next document, and it's working fine again :dunno...

macropod
01-05-2017, 08:36 PM
If you find a pattern to any errant behaviour, post back. The other possibility that occurs to me is that you pasted an inline image with something else following. Since the macro only looks back one character, even a single extraneous character in what was copied could throw it off.

Piecevcake
08-14-2021, 12:02 AM
If you find a pattern to any errant behaviour, post back. The other possibility that occurs to me is that you pasted an inline image with something else following. Since the macro only looks back one character, even a single extraneous character in what was copied could throw it off.

My macro (below) works perfectly pasting from the clipboard (Ctrl_C) from one application, but not from another. The only difference I could find was in the paste special options -
28837 28836

It seems the macro works on shapes pasted as pictures but does not recognise shapes pasted as bitmaps. and throws the drawing operation error.

But if I run the macro again (skipping the paste) on the pasted image, it works perfectly!

Are you able to throw any light on how to formulate the macro to work on both? (I would actually prefer to paste both as bitmaps as I need the accurate picture.)


Sub S___Floating_PasteAndSetPosition_ShiftAlt_V()
'WORKS WITH GREENSHOTS (pasted as PICTURE "PNG") 9/8/21,
'!!! NW WITH PASTES FROM FASTSTONE= (not recognised as shape! pasted as DEVICE INDEPENDENT BITMAP)???
'- Pastes but won't set floating shapes positioning=>ERROR,
' "the drawing operation cannot be applied to the current selection"
'- Resize no error but not resized!,
'- If rerun this same macro after exit, from label "ReRun" below, all operations are executed OK!
'- but if RETURN to the same label after error, before the macro has ended, SAME ERROR! -????!!!!
'- if CALL another macro from this macro - SAME ERROR!
'>>>> WORKAROUND= paste all then run position and resize macro on all in doc.

'?????? BOTH pastes show type = "msoFormControl"!!! although Rclick dialog says "format PICTURE"-???

' change word options default paste to in front of text (Word options)
Options.PictureWrapType = wdWrapMergeFront

selection.Paste

' change default paste back to inline after paste (Word options, doesn't affect selection)
Options.PictureWrapType = wdWrapMergeInline

'2.==== change position wrap etc - relative to character and line to put in correct initial position
' On Error GoTo NOT_SHAPE 'triggered if text or BITMAP shape pasted!

ReRunMacroFromHereWORKSForPastedBitmaps_ONLYafterEXITEDMacro_DOESTworkIfGot oHereFromErrorWhileRunningTheMacro:
With selection.ShapeRange
' .Select 'NW=ERROR

'set anchor position relative to - [DEFAULT when pasted = PARA, no word option to change it?]
'!!! NEED TO SET RELATIVE POSITION BEFORE INSERT?

'set anchor position 'NW FOR PASTED BITMAPS
.RelativeHorizontalPosition = wdRelativeHorizontalPositionCharacter '(sets at beginning of cell or para the orig insertion point was in) DIDN'T REMEMBER CURSOR POSITION

.RelativeVerticalPosition = wdRelativeVerticalPositionLine '0 positions at TOP of line or para (covering it)

'move shape to anchor position 'NW FOR PASTED BITMAP !MUST BE AFTER ANCHOR POSITION SET
.Left = InchesToPoints(-0.1) 'right of RelativeHorisontalPosition object: right of column=moves to column BORDER in table
.Top = InchesToPoints(0) '=below RelativeVerticalPosition object
' .Top = wdShapeCenter '=vertical position, centred on para/line/page etc
'other wrap settings
' .LockAnchor = True 'anchor stays in same place when image dragged doesnt move with image or separately
.LayoutInCell = False 'NW FOR PASTE FROM FASTSTONE
.LockAspectRatio = msoTrue '

''---- RESIZE OPTION - NO ERROR BUT NOTHING HAPPENS ON PASTED BITMAP
If MsgBox("Set width to 4.63cm?", vbYesNoCancel) = vbYes Then

.Width = CentimetersToPoints(4.63)
End If

End With
Exit Sub

NOT_SHAPE: 'errors go to here - TEST CALL MACRO
If MsgBox("Paste wasn't a shape? Call Faststone macro?", vbYesNo) = vbYes Then
Call S___Floating_FastStonePastesFormatRedo 'X same error in called macro - code ONLY works after this macro ended!
End If

End Sub