none
need simple code to select a custom shape which we have inserted just right now RRS feed

  • Question

  • hi friends 

    i had asked a similar question but i haven't got an exact answer to my question yet.

    my question is very simple.

    i need to know, when i just insert a custom shape in my word document, via what code i can select this shape which i have just inserted? ( so that i be able to paste clipboard contents into that shape).

    for example imagine that a page, i insert a shape & then i need a code so that when executed, it pastes the clipboard content into it.

    then at the middle of the document, again i insert 2nd instance of the same shape & now i need the code which pastes the current clipboard contents into this recently inserted shape.

    i know that i can't select these 2nd  instance & also next instances by specifying the name of the shape, because the name of my custom shape is the same all over the document.

    also i can't select them on the basis of the location of the shape in page, because they can be in any location within the page

    so in short, via what code i can specify that i want to select newly inserted shape ( and not previous inserted shapes even if their name is equal) ?

    really thanks in advanced 



    • Edited by john.s2011 Tuesday, November 25, 2014 7:55 PM
    Tuesday, November 25, 2014 7:47 PM

Answers

  • Hi John,

    Sorry for the mistake. To get the object return from a function, we need to use parenthesis.

    Here is the correct code for your reference:

    Dim aRange As Range
     Set aRange = Application.Templates( _
      "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
      .BuildingBlockEntries("_red_box").insert( where:=Selection.Range, RichText:=True)
    'if the custom shape is a shape
     aRange.ShapeRange(1).Select
    'if the custom shape is inline shape
    'aRange.InlineShapes(1).Select

    And based on the link provided in the above, the issue seemed to be fixed. If you have any other developing issue, please feel free to reopen a new thread in this forum.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by john.s2011 Monday, December 1, 2014 6:16 PM
    Monday, December 1, 2014 6:59 AM
    Moderator
  • Further to the solution I've posted at VBAX, you could make the code fully portable, thus:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, StrTxt As String
    Set Rng = Selection.Paragraphs(1).Range
    With Rng
      .Font.Size = 1
      .End = .End - 1
      StrTxt = .Text
      .Text = ""
    End With
    ActiveDocument.Shapes.AddTextbox msoTextOrientationHorizontal, 0, 0, 720, 54, Rng
    With Rng.Paragraphs(1).Range.ShapeRange(1)
      .LockAnchor = True
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeCenter
      .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
      .Top = 24
      .LockAspectRatio = False
      With .WrapFormat
        .Type = wdWrapTight
        .Side = wdWrapBoth
        .DistanceTop = InchesToPoints(20)
        .DistanceBottom = InchesToPoints(0)
      End With
      With .Line
        .ForeColor.RGB = RGB(255, 0, 0)
        .Weight = 2.25
        .Visible = True
        .Style = msoLineSingle
      End With
      With .TextFrame
        .WordWrap = False
        .AutoSize = False
        With .TextRange
          With .ParagraphFormat
            .Space1
            .SpaceBefore = 10
            .SpaceAfter = 10
          End With
          .Text = StrTxt
          With .Font
            .Name = "Arial"
            .Size = 20
            .Bold = True
            .ColorIndex = wdRed
          End With
        End With
      End With
    End With
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub

    The above code doesn't need the "_red_box" BuildingBlockEntry and it formats the textbox content independently of anything else in the document - simply adjust the textbox height, paragraph format, font specs, etc. to suit your requirements.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by john.s2011 Monday, December 1, 2014 6:21 PM
    Monday, December 1, 2014 9:32 AM

All replies

  • i myself guess something which maybe useful.

    if i can write a code that gets the number of the current paragraph (which shape is inserted) & then pastes the clipboard contents into the first shape in this current paragraph, this will be done.

    may someone please give me such VBA code?

    i have found something like this:

    Dim Rng As Range
        With ActiveDocument
        Set Rng = .GoTo(What:=wdGoToBookmark, Name:="\para")
        Selection.ShapeRange (1)
        Rng.Paste
        End With

    but when i use step info, i see that this line    Selection.ShapeRange (1)   doesn't select my object

    help is appreciated 


    • Edited by john.s2011 Wednesday, November 26, 2014 7:18 PM
    Wednesday, November 26, 2014 8:14 AM
  • Hi John,

    What's kind of code are you using for insert a shape? As far as I konw, the addshape method would return the shape you added. Here is an sample that insert a shape and some text then select the shape for your reference:

    Sub SelectShapeInserted()
    Dim myShape As Shape
    Set myShape = Shapes.AddShape(MsoAutoShapeType.msoShapeRound1Rectangle, 1, 1, 100, 200)
    Application.Selection.TypeText "Hello Word!"
    myShape.Select
    End Sub
    

    You can get more detail about Shapes object from link below:
    Shapes Object (Word)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 27, 2014 1:49 AM
    Moderator
  • Hi John,

    What's kind of code are you using for insert a shape? As far as I konw, the addshape method would return the shape you added. Here is an sample that insert a shape and some text then select the shape for your reference:

    Sub SelectShapeInserted()
    Dim myShape As Shape
    Set myShape = Shapes.AddShape(MsoAutoShapeType.msoShapeRound1Rectangle, 1, 1, 100, 200)
    Application.Selection.TypeText "Hello Word!"
    myShape.Select
    End Sub

    You can get more detail about Shapes object from link below:
    Shapes Object (Word)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Hi Fei

    thank you for reply

    my shape is a custom shape which i have created & is here:

            

    Application.Templates( _
     "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
     .BuildingBlockEntries("_red_box").insert where:=Selection.Range, RichText:=True

    so in short let me repeat my question:

    need a code that selects the shape which i have inserted in the the current paragraph (where cursor is blinking)  & then pastes the clipboard contents into this custom shape.

    Sub MyMacro ()

    Application.Templates( _
     "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
     .BuildingBlockEntries("MyShape").insert where:=Selection.Range, RichText:=True

    ' ----> now here i need a code to select this newly inserted shape in this paragraph

    ' warning: ( don't select by specifying the name of the shape, because the name of my custom  ' shape is the same all over the document & it causes all shapes with this name be selected).

    'also i can't select them on the basis of the paragraph number, because they can be in any location ' within the page)

    ' if above code in found, the rest of the operation will be done with this code:          ActiveDocument.ActiveWindow.Selection.Paste

    End sub

    so what would be the final code?

    thanks in advanced


    • Edited by john.s2011 Thursday, November 27, 2014 11:32 AM
    Thursday, November 27, 2014 11:23 AM
  • Hi John,

    Thanks for the detail information about this issue.

    Based on the code you were inserting custom shapes via inserting BuildingBlock. As since the the BuildingBlock.Insert methoed wolud return a range object that represents the contents of the building block within the document. We could use this range to get the shapes we inserted. Here is sample for your reference:

    Dim aRange As Range
     Set aRange = Application.Templates( _
      "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
      .BuildingBlockEntries("_red_box").insert (where:=Selection.Range, RichText:=True)
    'if the custom shape is a shape
     aRange.ShapeRange(1).Select
    'if the custom shape is inline shape
    'aRange.InlineShapes(1).Select

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, November 28, 2014 7:59 AM
    Moderator
  • Hi John,

    Thanks for the detail information about this issue.

    Based on the code you were inserting custom shapes via inserting BuildingBlock. As since the the BuildingBlock.Insert methoed wolud return a range object that represents the contents of the building block within the document. We could use this range to get the shapes we inserted. Here is sample for your reference:

    Dim aRange As Range
     Set aRange = Application.Templates( _
      "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
      .BuildingBlockEntries("_red_box").insert where:=Selection.Range, RichText:=True
    'if the custom shape is a shape
     aRange.ShapeRange(1).Select
    'if the custom shape is inline shape
    'aRange.InlineShapes(1).Select

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Hi again dear Fei

    really thanks for investigating on my problem.

    i tested your suggestion but i encounter the following error:

    where i am wrong ?

    may you also check the following link, which i have posted more details there:

    http://www.vbaexpress.com/forum/showthread.php?51255-VBA-code-to-select-the-first-shape-in-the-current-active-paragraph-in-MS-Word-2013&p=318005#post318005

    thanks

    • Edited by john.s2011 Saturday, November 29, 2014 6:03 PM
    Saturday, November 29, 2014 6:02 PM
  • Hi John,

    Sorry for the mistake. To get the object return from a function, we need to use parenthesis.

    Here is the correct code for your reference:

    Dim aRange As Range
     Set aRange = Application.Templates( _
      "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
      .BuildingBlockEntries("_red_box").insert( where:=Selection.Range, RichText:=True)
    'if the custom shape is a shape
     aRange.ShapeRange(1).Select
    'if the custom shape is inline shape
    'aRange.InlineShapes(1).Select

    And based on the link provided in the above, the issue seemed to be fixed. If you have any other developing issue, please feel free to reopen a new thread in this forum.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by john.s2011 Monday, December 1, 2014 6:16 PM
    Monday, December 1, 2014 6:59 AM
    Moderator
  • Further to the solution I've posted at VBAX, you could make the code fully portable, thus:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, StrTxt As String
    Set Rng = Selection.Paragraphs(1).Range
    With Rng
      .Font.Size = 1
      .End = .End - 1
      StrTxt = .Text
      .Text = ""
    End With
    ActiveDocument.Shapes.AddTextbox msoTextOrientationHorizontal, 0, 0, 720, 54, Rng
    With Rng.Paragraphs(1).Range.ShapeRange(1)
      .LockAnchor = True
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeCenter
      .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
      .Top = 24
      .LockAspectRatio = False
      With .WrapFormat
        .Type = wdWrapTight
        .Side = wdWrapBoth
        .DistanceTop = InchesToPoints(20)
        .DistanceBottom = InchesToPoints(0)
      End With
      With .Line
        .ForeColor.RGB = RGB(255, 0, 0)
        .Weight = 2.25
        .Visible = True
        .Style = msoLineSingle
      End With
      With .TextFrame
        .WordWrap = False
        .AutoSize = False
        With .TextRange
          With .ParagraphFormat
            .Space1
            .SpaceBefore = 10
            .SpaceAfter = 10
          End With
          .Text = StrTxt
          With .Font
            .Name = "Arial"
            .Size = 20
            .Bold = True
            .ColorIndex = wdRed
          End With
        End With
      End With
    End With
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub

    The above code doesn't need the "_red_box" BuildingBlockEntry and it formats the textbox content independently of anything else in the document - simply adjust the textbox height, paragraph format, font specs, etc. to suit your requirements.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by john.s2011 Monday, December 1, 2014 6:21 PM
    Monday, December 1, 2014 9:32 AM
  • Hi John,

    Sorry for the mistake. To get the object return from a function, we need to use parenthesis.

    Here is the correct code for your reference:

    Dim aRange As Range
     Set aRange = Application.Templates( _
      "C:\Users\Administrator\AppData\Roaming\Microsoft\Templates\Normal.dotm") _
      .BuildingBlockEntries("_red_box").insert( where:=Selection.Range, RichText:=True)
    'if the custom shape is a shape
     aRange.ShapeRange(1).Select
    'if the custom shape is inline shape
    'aRange.InlineShapes(1).Select

    And based on the link provided in the above, the issue seemed to be fixed. If you have any other developing issue, please feel free to reopen a new thread in this forum.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Dear Fei

    really thank you for your helps, that's great. 

    kind regards

    Monday, December 1, 2014 6:17 PM
  • Further to the solution I've posted at VBAX, you could make the code fully portable, thus:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range, StrTxt As String
    Set Rng = Selection.Paragraphs(1).Range
    With Rng
      .Font.Size = 1
      .End = .End - 1
      StrTxt = .Text
      .Text = ""
    End With
    ActiveDocument.Shapes.AddTextbox msoTextOrientationHorizontal, 0, 0, 720, 54, Rng
    With Rng.Paragraphs(1).Range.ShapeRange(1)
      .LockAnchor = True
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .Left = wdShapeCenter
      .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
      .Top = 24
      .LockAspectRatio = False
      With .WrapFormat
        .Type = wdWrapTight
        .Side = wdWrapBoth
        .DistanceTop = InchesToPoints(20)
        .DistanceBottom = InchesToPoints(0)
      End With
      With .Line
        .ForeColor.RGB = RGB(255, 0, 0)
        .Weight = 2.25
        .Visible = True
        .Style = msoLineSingle
      End With
      With .TextFrame
        .WordWrap = False
        .AutoSize = False
        With .TextRange
          With .ParagraphFormat
            .Space1
            .SpaceBefore = 10
            .SpaceAfter = 10
          End With
          .Text = StrTxt
          With .Font
            .Name = "Arial"
            .Size = 20
            .Bold = True
            .ColorIndex = wdRed
          End With
        End With
      End With
    End With
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub

    The above code doesn't need the "_red_box" BuildingBlockEntry and it formats the textbox content independently of anything else in the document - simply adjust the textbox height, paragraph format, font specs, etc. to suit your requirements.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    hi again

    that's really great because i can run it in any system without having that Red_box shape, as you mentioned.

    everything is ok, but only the location of text box must be adjusted, because after running this macro, it becomes :

    Now after running this macro, it becomes so, which is not fit:

    i really shame to ask furture guidance. you really helped me.

    as i noticed the Word Application ( & also VBA) are very wide, they have lots of thing to learn.

    i must begin to study hard instead of asking any question from you. too many things to learn.

    again really thanks

    kind regards

     

    Monday, December 1, 2014 6:31 PM
  • Try changing:

      With .WrapFormat
        .Type = wdWrapTight
        .Side = wdWrapBoth
        .DistanceTop = InchesToPoints(20)
        .DistanceBottom = InchesToPoints(0)
      End With

    (I don't know where 'InchesToPoints(20)' came from - it should have been 'InchesToPoints(0.28)') to:

      With .WrapFormat
        .Type = wdWrapTopBottom
        .DistanceTop = 20
        .DistanceBottom = 0
      End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 2, 2014 6:01 AM