Consulting

Results 1 to 9 of 9

Thread: "the drawing operation cannot be applied to the current selection"

  1. #1
    VBAX Regular AJS's Avatar
    Joined
    Sep 2004
    Location
    Melbourne
    Posts
    61
    Location

    "the drawing operation cannot be applied to the current selection"

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular AJS's Avatar
    Joined
    Sep 2004
    Location
    Melbourne
    Posts
    61
    Location
    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...

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by AJS View Post
    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).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular AJS's Avatar
    Joined
    Sep 2004
    Location
    Melbourne
    Posts
    61
    Location
    ... 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

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Did you actually paste an image?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular AJS's Avatar
    Joined
    Sep 2004
    Location
    Melbourne
    Posts
    61
    Location
    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 ...

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9

    Errant behaviour with bitmap paste?

    Quote Originally Posted by macropod View Post
    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 -
    faststone paste options = bitmap paste special @2021.08.14, 17_19_59.jpg greenshot paste options- default=png Paste Special @2021.08.14, 17_21_11.jpg

    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_DOESTworkIfGotoHereFromErrorWhileRunningTheMacro:
        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
    Last edited by Piecevcake; 08-14-2021 at 12:17 AM. Reason: add preference

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •