Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA - manipulate text in Word - delete sections

Status
Not open for further replies.

OddEinar

Programmer
May 11, 2006
8
NO
Hi, I need to make a macro for MS Word that finds different parts of my document and deletes these parts given sertan conditions. How do I do this? How do I select the different parts, and how do I delete them?

Thanks for all help!
 
As a starting point you may consider playing with the macro recorder and then examine the generated code.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Well - I have tried, but it makes no sense... The VBA code just tells me that I took one step this way, and one step the other way, and deleted this and that... It doesn't give me any clue on how I can define wich parts of the text I want to delete. And I need that to use the conditions.

Imagine that there are several boolene variables, and different text is supose to be deleted based on the value of the variables. Then I need some kind of way (bookmarks?) to define the different parts, and a way to find them, mark them, and delete them. It's the variation in which text to delete that complicates everything. (If it was a standard part I would like to erase every time, I could just take delete it in the .dot file...)

Hope for some good answers, 'cus I'm not coming any further!
 
Bookmarks are PRECISELY the best way to do this.

Go through your document and select the chunks you want to define. Insert > Bookmark and name the chunks. Now you can use the names of the bookmarks to do whatever you want with them.

You will need to define your logic, but basically it is straightforward. Although the possibilities are almost infinitely variable - yuck yuck haha. You mention Boolean, so say you have a area of text bookmarked as Chunk_A, and another as Chunk_B - and you can name these whatever you want. And another as Chunk_C. Here is one kind of possibility
Code:
Dim Client As Boolean
If Client = True Then 
  ActiveDocument.Bookmarks("Chunk_A").Range.Delete
End If
Or doing a check of the contents of the bookmark:
Code:
If ActiveDocument.Bookmarks("Chunk_B").Range.Text = _
   "Harry Belafonte" Then
   ActiveDocument.Bookmarks("Chunk_C").Range.Delete
End If

Hope this helps.


Gerry
 
Hi, I have now made this code:

Option Explicit
Sub AutoNew()
frmSletting.Show
End Sub

Private Sub cmdCANCEL_Click()
Unload Me
End Sub

Private Sub cmdOK_Click()
Application.ScreenUpdating = False
If Check1 = False Then
ActiveDocument.Bookmarks("x1").Range.Delete
End If

If Check2 = False Then
ActiveDocument.Bookmarks("x2").Range.Delete
End If

If Check3 = False Then
ActiveDocument.Bookmarks("x3").Range.Delete
End If

If Check4 = False Then
ActiveDocument.Bookmarks("x4").Range.Delete
End If

Application.ScreenUpdating = True

Unload Me

End Sub

It is related to a user form with checkboxes. The thing is - when I don't check Check1, all my bookmarks are deleted. Why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top