1

I have shape object and i want to delete in any way the text between two string. e.g.:

Dim oRang As RangeSet oRang = ActiveDocument.Shapes(1).TextFrame.TextRange
Const firstWord = "father"
const secondWord = "mother"

I have text in oRang: "father ajgfhdgh aghdashdgsgh sdfyghudsdghkshgfy dgvssdfhyv gdfysdfv gfdsdfv mother".

I want to delete all words between father to mother. How can i do that?

Thanks,

Tal

tal_sshh
  • 41
  • 2
  • 9
  • You can use instr() to locate the positions of your two key words, then use combinations of Left(), Right(), and/or Mid() to break the string into pieces. Then use the concatenation operator (&) to put things back together, leaving out the middle part. Look at the VBA documentation for those functions. – Rich Holton Mar 26 '17 at 13:34

1 Answers1

0

Since this question is about string manipulation and doesn't have anything to do with shapes for convenience I will assume the input string to be in a variable called str and the result to be output to the console. Adjust as necessary.

There are basically two approaches. The first uses the built-in functions InStr to search in a string and Left and Mid to retrieve parts of a string:

Dim firstWordLocation As Long
Dim secondWordLocation As Long

firstWordLocation = InStr(str, firstWord)
secondWordLocation = InStr(str, secondWord)
If firstWordLocation > 0 And secondWordLocation > 0 Then
    Debug.Print Left(str, firstWordLocation + Len(firstWord) - 1) & Mid(str, secondWordLocation)
End If

Another way to approach this is to use regular expressions. They are much more powerful and can perform complex string manipulations in a very compact way. The library Microsoft VBScript Regular Expressions 5.5 provides a RegExp class that can be used as follows, (also see https://stackoverflow.com/a/22542835 for more details):

Dim regex As New VBScript_RegExp_55.RegExp

regex.Pattern = "(.*?" & firstWord & ").*?(" & secondWord & ".*)"
Debug.Print regex.Replace(str, "$1$2")

This replaces the input string with the content of the first and second parenthesis in Pattern, therefore omitting everything in between. Be aware that the reserved characters .*+?()[]{}^$\ must be escaped with a backslash \ if they should occur in firstWord or secondWord. If you have line breaks in your input string, you should have a look at https://superuser.com/q/399381.


If you want to place this string back in a shape and format certain parts of it using VBA, you have to use the Character property of the TextRange which represents each single text character in the shape. Now you can assign the desired format using those character objects.

See https://msdn.microsoft.com/de-de/library/office/ff743845.aspx for more (although that documentation is for PowerPoint, I think it will also apply to Word).

Community
  • 1
  • 1
Leviathan
  • 2,468
  • 1
  • 18
  • 24
  • Hi, it's remove from the text(when i check in debug i can see) but not update the object in document(i can't see the change after i run the code). – tal_sshh Mar 28 '17 at 04:43
  • Have you tried putting the result back in the shape? – Leviathan Mar 28 '17 at 06:51
  • I success to delete the text with the code you sugget and i put the new text: ActiveDocument.Shapes(1).TextFrame.TextRange.Text = newTextShape. but it is also delete the Design of the shape object(Design means font, Division into sections...). I think that i need to use with find function but i don't know to do that. – tal_sshh Mar 28 '17 at 12:24
  • I updated my answer with a paragraph on how to format single characters in a textframe of a shape. This will be quite messy and I suggest you create another queston if you need further help with format manipulation in textframes. – Leviathan Mar 28 '17 at 20:09