PDA

View Full Version : Delete section containing given string



agent86
11-10-2013, 01:52 PM
Word 2010

I have a section I have used vba to insert.

I need the vba code to remove a section that contains the string "xman goes here" . I need to remove the section, not just the string. There will only be one section that contains that string.

Is there a way to name a section so I can remove a section with a given name?

fumei
11-10-2013, 02:09 PM
Sure. Bookmarks are simply named ranges. So bookmark the section. BTW, once made a bookmark you can still add (or remove) content out of the section and it will not make a difference. The bookmark expand and contracts appropriately.

agent86
11-10-2013, 03:44 PM
I'm confused but I am new to VBA.

Here is the code I use to insert the section. What is the way to delete the entire section I have inserted with this code? I want the section breaks and everything in between them to disappear.





Sub CreateSection()
Dim strImageTag
strImageTag = "<ImageTable: xman>xman goes here"
System.Cursor = wdCursorWait
Selection.InsertBreak Type:=wdSectionBreakContinuous

Selection.Font.Hidden = True
Selection.TypeText (strImageTag)
Selection.Font.Hidden = False

Selection.InsertBreak Type:=wdSectionBreakContinuous

End Sub

macropod
11-10-2013, 05:00 PM
Why are you inserting two section breaks, plus content, if all you're going to do is delete it? Which of the two Sections created by your code do you want to delete?

Note: Working with selections is slow and, usually, unnecessary.

agent86
11-10-2013, 05:24 PM
I insert the section breaks into an existing document to give me a place to insert an external document which is read-only. Everything above the first section break stays editable, and everything below the second section break stays editable. After I insert the section breaks, I replace the "xman goes here" with a Selection.InsertFile. I then "protect" the entire document and the section that has the inserted document is protected and the cursor can't be inserted in the protected section. The rest of the word document is editable except between the section breaks. After the document is protected, the second section break is marked 'End of protecetd section'. I need to be able to delete the section because after the inserted document is removed I don't want the section in the document any longer. The issue is that the user may want to delete or edit the inserted document and they are not allowed to edit while it is inside the word document. They go to another program to edit that document.

macropod
11-10-2013, 05:44 PM
It seems to me, then, that you would be better served by either:
a) using a bookmarked range into which you can insert both the Section breaks and the content. You could then delete the bookmarked content if needed; or
b) having the section breaks in the document on a permanent basis, populating/depopulating the Section as needed.

As indicated previously, nothing needs to be selected for either approach.

agent86
11-10-2013, 05:50 PM
How do I VBA a bookmark into the document and then how do I delete a bookmarked area with VBA?

macropod
11-10-2013, 07:03 PM
You don't create the bookmark with VBA, rather, you use a bookmark that you've already created. For example, to insert a file you might use something like:

Sub Demo1()
Dim BkMkNm As String, BkMkRng As Range, TmpRng As Range, StrFile As String
BkMkNm = "MyBookmark"
StrFile = "C:\Users\" & Environ("userName") & "\Documents\myFile.docx"
With ActiveDocument
Set BkMkRng = .Bookmarks(BkMkNm).Range
Set TmpRng = .Bookmarks(BkMkNm).Range
TmpRng.End = TmpRng.End + 1
BkMkRng.InsertFile StrFile
TmpRng.End = TmpRng.End - 1
BkMkRng.End = TmpRng.End
.Bookmarks.Add BkMkNm, BkMkRng
End With
End Sub

To clear the same bookmarked range you might use something like:

Sub Demo2()
Dim BkMkNm As String
BkMkNm = "MyBookmark"
ActiveDocument.Bookmarks(BkMkNm).Range.Text = vbNullString
End Sub

agent86
11-10-2013, 07:16 PM
What part of the code protects the inserted file from being edited by the user? The inserted file MUST not be editable.

macropod
11-10-2013, 07:28 PM
Quite obviously, some demo code can't have all of the variables, especially when you haven't said what they are or where they're coming from. I'd have thought it clear enough that you'd need to change whatever you populate the 'StrFile' variable with to suit your own requirements. After all, how are you now populating your strImageTag variable?

The inserted file MUST not be editable
It's not a big deal to add the section breaks as well. For example:

Sub Demo1()
Dim BkMkNm As String, BkMkRng As Range, TmpRng As Range, StrFile As String
BkMkNm = "MyBookmark"
StrFile = "C:\Users\" & Environ("userName") & "\Documents\myFile.docx"
With ActiveDocument
Set BkMkRng = .Bookmarks(BkMkNm).Range
Set TmpRng = .Bookmarks(BkMkNm).Range
With TmpRng
.End = .End + 1
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
.InsertAfter vbCr & vbCr
.Sections(1).Range.Paragraphs.First.Range.InsertFile StrFile
While .Characters.Last.Previous = vbCr
.Characters.Last.Previous = vbNullString
Wend
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
.InsertAfter vbCr
End With
BkMkRng.End = TmpRng.End
.Bookmarks.Add BkMkNm, BkMkRng
End With
End Sub
Note the addition of a loop to clean out extra paragraph breaks from the end of the insertion

agent86
11-10-2013, 07:57 PM
First off let me thank you taking the time to answer my questions. I truly appreciate it!

OK. Does your code remove the bookmark? The problem is the external file will not always be inserted at the same location in the word document. Does a bookmark have a beginning bookmark and an ending bookmark so we can completely remove any sign the bookmark was ever on the document?

fumei
11-10-2013, 07:59 PM
I think there may be some confusion as to what you want to happen. macropod's statement:


a) using a bookmarked range into which you can insert both the Section breaks and the content. You could then delete the bookmarked content if needed; The key point is the word content. Why is the section itself so important?

That is, you make an empty bookmark in the document. Now you can insert or delete any content you like, inside that bookmark. You do not use VBA to put in the bookmark. You decide where the bookmark is. If it remains empty, then for all intents and purposes there is nothing there content-wise.

So. Make a section break (and another, so you can have a separate section.) There. Done. You can put content into as you like, protect it as you like, allow editing or not, as you like.

I need to be able to delete the section because after the inserted document is removed I don't want the section in the document any longer. Why? Why not keep it?

macropod
11-10-2013, 08:06 PM
To preserve both the bookmarks and whatever surrounds them, use something like:

Sub Demo1()
Dim BkMkNm As String, BkMkRng As Range, TmpRng As Range, StrFile As String
BkMkNm = "MyBookmark"
StrFile = "C:\Users\" & Environ("userName") & "\Documents\myFile.docx"
With ActiveDocument
Set BkMkRng = .Bookmarks(BkMkNm).Range
Set TmpRng = .Bookmarks(BkMkNm).Range
With TmpRng
.End = .End + 1
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
.InsertAfter vbCr & vbCr
.Sections(1).Range.Paragraphs.First.Range.InsertFile StrFile
While .Characters.Last.Previous = vbCr
.Characters.Last.Previous = vbNullString
Wend
.Collapse wdCollapseEnd
.InsertBreak Type:=wdSectionBreakContinuous
.InsertAfter vbCr
End With
BkMkRng.End = TmpRng.End
.Bookmarks.Add BkMkNm, BkMkRng
End With
End Sub
and:
Sub Demo2()
Dim BkMkNm As String, BkMkRng As Range
BkMkNm = "MyBookmark"
With ActiveDocument
Set BkMkRng = .Bookmarks(BkMkNm).Range
BkMkRng.End = BkMkRng.End - 1
BkMkRng.Text = vbNullString
.Bookmarks.Add BkMkNm, BkMkRng
End With
End Sub

macropod
11-10-2013, 08:17 PM
I need to be able to delete the section because after the inserted document is removed I don't want the section in the document any longer.
Why? Why not keep it?
Indeed, it would require less work and there would be no need to reference a bookmark that might accidentally (or otherwise) get deleted. Instead, one could always reference the Section by its index number.

fumei
11-10-2013, 08:50 PM
Hmmm, quite a few posts while I was writing.

The problem is the external file will not always be inserted at the same location in the word document. Does a bookmark have a beginning bookmark and an ending bookmark so we can completely remove any sign the bookmark was ever on the document? This is a rather critical piece of information, and yes, that does affect having a bookmark do the work.

A bookmark is a range object. it can be as samll as literally nothing (sort of). It can be defined as a single point. Virtually invisible. But if you are talking about placing your inserted content ANYWHERE, then I suppose you are stuck with Selection.

I have to say, I am confused.

After I insert the section breaks, I replace the "xman goes here" with a Selection.InsertFile. I then "protect" the entire document and the section that has the inserted document is protected and the cursor can't be inserted in the protected section. The rest of the word document is editable except between the section breaks. After the document is protected, the second section break is marked 'End of protecetd section'. I need to be able to delete the section because after the inserted document is removed I don't want the section in the document any longer.
Let's walk through that.

1. A section is inserted (a start of the section, and an end).
2. a file content is inserted into that section.
3. that section is protected so no editing.
4. that section is deleted after NO editing, and after the file content is removed ("I need to be able to delete the section because after the inserted document is removed ")

Sorry, but that makes no sense to me. The inserted file is inserted to only allow it to be viewed...and then it is removed?

agent86
11-11-2013, 08:43 AM
Hmmm, quite a few posts while I was writing.
This is a rather critical piece of information, and yes, that does affect having a bookmark do the work.

A bookmark is a range object. it can be as samll as literally nothing (sort of). It can be defined as a single point. Virtually invisible. But if you are talking about placing your inserted content ANYWHERE, then I suppose you are stuck with Selection.

I have to say, I am confused.

Let's walk through that.

1. A section is inserted (a start of the section, and an end).
2. a file content is inserted into that section.
3. that section is protected so no editing.
4. that section is deleted after NO editing, and after the file content is removed ("I need to be able to delete the section because after the inserted document is removed ")

Sorry, but that makes no sense to me. The inserted file is inserted to only allow it to be viewed...and then it is removed?

agent86
11-11-2013, 08:56 AM
How do I get the index of the section break I just or am about to insert? Can section breaks be given a name? These questions don't matter if I can't remove a section break.

The content of the file is created in another application. It is possible that the user will want to remove the content or put it somewhere else in the Word document. They may choose to put it in, then decide they do not want it in the Word document at all. I do not want "empty" sections in the document.

The functional requirement is I need to insert and delete section breaks, leaving no trace they were ever in the document. I may insert up to 5 section breaks "pairs" then remove one or two of the section break pairs. I will be inserting the section breaks in pairs so the content between them can be marked "protected" without affecting the existing Word document content.

The thing that surprises me is that it is very easy to insert a section break with VBA, but very difficult to remove one.

macropod
11-11-2013, 02:12 PM
I do not want "empty" sections in the document.

The functional requirement is I need to insert and delete section breaks, leaving no trace they were ever in the document.
Why does it matter that there are empty sections in the document? After all, if the Section is protected, and the breaks are formatted correctly, there'll be no visible evidence of their presence in the output. And, in that scenario, given that the Sections of interest start off empty, there'll be no evidence that they might have at one stage contained anything if you decide to delete the content. In all material respects, except for being more robust it's no different to having an empty bookmark.

[The thing that surprises me is that it is very easy to insert a section break with VBA, but very difficult to remove one.
They are not at all difficult to remove. What you're failing to grasp is that, having inserted two Section breaks and some content programmatically, you need to be able to tell Word where all of that is later on if you decide to remove it. Indeed, given the way you're going about the insertion/deletion of content, I can't see what purpose is achieved at any stage in the process by programmatically inserting the Section breaks.

fumei
11-11-2013, 02:32 PM
Still confused.

It is possible that the user will want to remove the content or put it somewhere else in the Word document. How are they going to do that, if you are protecting it from any edits? Especially the idea of moving it.


The thing that surprises me is that it is very easy to insert a section break with VBA, but very difficult to remove one. You are incorrect. They are not hard at all. And neither is deleting a section.

ActiveDocument.Sections(3).Range.Delete deletes the range. However, the trailing section is the only one actually deleted. The first one is not.

Sub DeleteSection2()
Dim r As Range
Set r = ActiveDocument.Sections(2).R3nge
r.MoveStart unit:=wdCharacter, Count:=-1
r.Delete
End Sub backs of the start of the declared range by one character, thus including the initial section break. Both initial and trailing breaks are deleted (along with of course everything in between). The section is entirely removed.

This STILL does not help me with your contradiction of having a section that is protected YET still allowing users to move it. NOR explain why it is even there in the first place.

How do I get the index of the section break I just or am about to insert? Can section breaks be given a name? These questions don't matter if I can't remove a section break. 1. You can remove section breaks.
2. regarding "about to insert", simply place your fingers over your closed eyes and concentrate. Word will be able to read your mind.
3. You should put at least one paragraph into your new section. When you do your new section breaks, back up one character (placing the selection into the section). Now you can get the section index number.

Sub makeBreaks()
With Selection
.InsertBreak Type:=wdSectionBreakContinuous
.TypeParagraph
.InsertBreak Type:=wdSectionBreakContinuous
.Move unit:=wdCharacter, Count:=-1
End With
MsgBox Selection.Information(wdActiveEndSectionNumber)
End Sub Obviously, if you store the number into a variable, you can later use it to delete the section (including section breaks).

I hope this helps, but I still have to say that this process seems horribly flawed to me.

agent86
11-11-2013, 02:52 PM
This is exactly the information I needed. Thank you. I have found another post on the internet that showed how to find the index of the section. It appears I now have everything I need to accomplish my task.

The reason I want to get rid of the unused section breaks is that if I want to insert another file/document into the Word document it will put it in the first empty section break it finds. It may not be in the location I want the new file/document inserted. I protect and unprotect sections of the document not the whole document. Now knowing how to find the index of the section breaks I guess I could design a method to skip empty sections if I tell it which "section.index" to put the file into.

I appreciate the intellectual discussion about why I am or am not doing something but I do not want to "bore" everyone with my problem but am looking for a solution to a general task. By keeping it simple the answer may be able to help someone else in the future who is not doing exactly what I am. I just wanted to know how to identify and remove a section. Now I do. Thank you to all who contributed.

fumei
11-11-2013, 07:56 PM
if I want to insert another file/document into the Word document it will put it in the first empty section break it finds. Could you please post that code? Because the code as you have posted does NOT do that. It inserts a section, it does no finding...of anything.


I appreciate the intellectual discussion about why I am or am not doing something but I do not want to "bore" everyone with my problem Bored, no. I would not have asked if I did not want to know. I still want to know. I am not sure why you are so reluctant to tell me.

macropod
11-11-2013, 09:44 PM
Besides, what's to stop someone inserting one of these files into an unprotected Section?? That can be done by:
• opening the source file and using copy/paste;
• using Insert|File;
• using an INCLUDETEXT field;
• etc.