none
Deleting a specific text box using Visual Basic RRS feed

  • Question

  • I have a Text Box that contains values from two document properties. These values have been repositioned and the text box must now be removed, not just cleared, but deleted. The Text Box apears on the cover page of the document, it is in the body, not the header.

    How do I best do this? Do I need to identify the object number of this Text Box and if so how?
    Is there any easy script to delete this single Text Box. I cannot be sure if users have added other Text Boxes on the same page, so a deletion of all Text Boxes is not a good idea.

    thanks

    Robin

    Wednesday, August 23, 2017 10:17 AM

All replies

  • I have managed to delete this using

    ActiveDocument.Shapes(1).Delete

    but if the script is run again it will definitely delete the next text box, which would be part of the content that must not be deleted, so I'm still stuck here. If I could limit it to deletion of the text box on page 1, that is the cover page, then that would be usable, accepting that no one has placed other text boxes there.

    Robin

    Wednesday, August 23, 2017 7:26 PM
  • There are various ways of approaching this. If you know the textbox's relative order in the document, you can use it's index number, as in:
    ActiveDocument.Shapes(#).Delete
    where # is the index number.

    Alternatively, if you retrieve the textbox's name, which you might do as a once-off exercise with code like:
    MsgBox Selection.ShapeRange(1).Name
    you could later delete it with code like:
    ActiveDocument.Shapes("Text Box Name").Delete
    where 'Text Box Name' is the name returned by the MsgBox. If you're worried that textbox might lo longer be present when you run the code, you could simply use:
    On Error Resume Next
    ActiveDocument.Shapes("Text Box Name").Delete
    On Error Goto 0


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Wednesday, August 23, 2017 11:44 PM Expansion
    • Proposed as answer by Terry Xu - MSFT Thursday, August 24, 2017 3:21 AM
    Wednesday, August 23, 2017 11:42 PM
  • Hi rkiskan,

    I think you could also Shape.ID to identify a specific text box.

    Here is the example.

    Dim shp As Shape
    
    For Each shp In ActiveDocument.Shapes
    
    Debug.Print shp.ID
    
    If shp.ID = 17 Then
    
    shp.Delete
    
    Exit For
    
    End If
    
    Next shp

    Best Regards,

    Terry

    Thursday, August 24, 2017 5:14 AM
  • Hi Terry and Paul,

    Yes, both your suggestions work OK - I have tried them now. But I see the situation is a bit more complex. I now realize I cannot just delete the text box, but I must move it (simply because I cannot be sure what authors have done on their documents, for example maybe asigned bookmarks). I managed to move them to do quite easily in VB.

    The problem is that the new corporate design requires the one property to be dynamically colored. To do this on new properties I have used bookmarks as suggested by Paul, and that works fine. But if I move the old property fields then the bookmark is not there. Any suggestions on :

    1. how to assign a bookmark called "ColorField" to a Custom Document Property?

    2. how to assign a name to a text box? I see someone has done this before, but it's not in the current VBA script. I can see the names in the message dialog suggested by Paul.

    thanks again
    Robin

    Thursday, August 24, 2017 6:38 AM
  • You can handle bookmarks that get deleted (e.g. because the textbox gets deleted) via a simple If test:

    If ActiveDocument.Bookmarks.Exists("BookmarkName") Then _
        ActiveDocument.Bookmarks("BookmarkName").Range.Font.ColorIndex = wdRed

    You can name the selected textbox (or any selected Shape object, for that matter) via:

    Sub Demo()
    With Selection.ShapeRange(1)
      .Name = InputBox("Add/edit the textbox name", , .Name)
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, August 24, 2017 6:51 AM
  • Hi Paul,

    Yes, but I have to set the bookmark on the property behind the scenes so the color can be applied.

    The user can't have a dialog appear - they have no clue what the bookmark must be. For the user it must just be same field (property) just in a different location, but for VBA it must have a the bookmark applied so the color can be applied.

    rgrds
    Robin

    Thursday, August 24, 2017 7:07 AM
  • You can't apply a bookmark to a Document Property, or vice-versa. You can bookmark a DOCPROPERTY field, though, just as you can any other range in a document. Regardless, if you know you want to colour the output of a given DOCPROPERTY field, you don't need a bookmark - you can simply loop through the fields collection till you find the one you want. For example:

    Sub Demo()
    Dim Fld As Field
    With ActiveDocument
      ' Ensure the field code display is switched off
      .ActiveWindow.View.ShowFieldCodes = False
      For Each Fld In .Fields
        With Fld
          If .Type = wdFieldDocProperty Then
            If Split(Trim(.Code.Text), " ")(1) = "MyPropertyName" Then
              ' Clear any Mergeformat & Charformat switches from the field
              .Code.Text = Replace(Replace(.Code.Text, "\* Mergeformat", ""), "\* Charformat", "")
              Fld.Result.Duplicate.Font.ColorIndex = wdBlue
              Exit For
            End If
          End If
        End With
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, August 24, 2017 7:53 AM
  • OK. I will have to try this approach. What I basically did before was create a building block with the fields in their new correct positions and then add that building block with VB to the document. The problem being that while the values are correctly applied, the "original" field was never bookmarked and still showed. I did apply a bookmark to the field in the building block so that the color could be applied, and that works fine. I can't delete the original field's Text Box, because I may start deleting other Text Boxes that I am not aware of. I thought I could just move it and then bookmark it correctly. Now as you say, bookmarks and Document Properties do not go, so this script may be the best solution.

    Thanks again
    Robin

    Thursday, August 24, 2017 9:31 AM
  • I tried you code Paul, but it gave no result. I'm not exactly sure what to specify for Mergeformat and Charformat ... that could be why. I tried with wdBlue, but will there was no error, nothing changed in the field color. Instead of the wd.. colors I will need to use RGB due to the color choices that are demanded.

    rgrds
    Robin

    Thursday, August 24, 2017 6:47 PM
  • You don't need to do anything with the Mergeformat and Charformat switches - the relevant line in the code simply deletes them if they're present so as to ensure they don't mess up the output. I've revised the code slightly, so the applied colour will 'stick' if something later causes the field display to refresh:

    Sub Demo()
    Dim Fld As Field
    With ActiveDocument
      ' Ensure the field code display is switched off
      .ActiveWindow.View.ShowFieldCodes = False
      For Each Fld In .Fields
        With Fld
          If .Type = wdFieldDocProperty Then
            If Split(Trim(.Code.Text), " ")(1) = "MyPropertyName" Then
              ' Clear any Mergeformat & Charformat switches from the field
              .Code.Text = Replace(Replace(.Code.Text, "\* Mergeformat", ""), "\* Charformat", "")
              .Code.Font.Color = RGB(121, 256, 123)
              .Update
              Exit For
            End If
          End If
        End With
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Sunday, August 27, 2017 9:45 PM
    Thursday, August 24, 2017 11:59 PM
  • Thanks again Paul!
    Sunday, August 27, 2017 7:28 AM
  • Hi rkiskan,

    It seems that your original issue has been solved by Paul, I suggest you mark useful reply to help other developers use this forum efficiently. Thanks for understanding.

    Best Regards,

    Terry

    Tuesday, August 29, 2017 1:20 AM