vba macro for word : delete sub section - vba

I have sub sections in a word document which I want to delete based on user inputs in a custom user interface.
For e.g : I want to delete sub section 3.1.1 under the section 3
I used the following code, but it deletes the entire section but I want to delete only a specific sub section:
ActiveDocument.Sections(x).Range.Delete
Here I am not able to give x = 3.1.1, it only accepts just the integer value like 3 and that deletes the entire section.

Word doesn't have nested sections, so you probably need to cycle through the sections until you find one that matches your needs. So, let's say you have a Word document that looks like this:
Title___[continuous section break]
Stuff
Section 1___[continuous section break]
Stuff
Section 1.1___[continuous section break]
Stuff
You could loop through the sections and check the first paragraph of each:
For each objSect in ActiveDocument.sections
if trim(replace(objSect.Range.Paragraphs.First.range.Text, chr(13), "")) like "* 1.1" then objSect.range.delete
Next objSect
Of course, that means if you want to delete section 1 you'll need to delete it along with any sub-sections, one at a time.
If what you really want is something like what the navigation pane gives you, I don't know if that's supported in VBA. There don't appear to be any methods that would mimic the Navigation Pane Delete option.

Related

Get table values from certain section in word VBA

I have a large word document that has several tables in several different sections. Something that looks like the following:
Section 1
Section 1.1 (has table)
Section 1.2
Section 1.2.1 (has table)
Section 2
Section 2.1 (has table)
Section 2.1.1 (has table)
Section 2.2
Section 3 (has table)
However, I only want the tables in one section. Is there a way to create a loop that can get the data from all the tables only in a certain section?
I was thinking about using something like ActiveDocument.TablesOfContents(1), but I think I'll need more than that. Is there a way to constantly check which section you're in? Maybe some pseudocode like...
'Starting at the top of the document
'This would not start until the document found Section 2, and would end once it found section 3
While [ActiveDocument.TablesOfContents("name")]
'Which would include 2.1, 2.1.1, and 2.2 because they are a part of 2
TableCount = ActiveDocument.Tables.Count 'But only header 2
For tableNum = 1 To TableCount
' do something/get data
Next tableNum
Wend
The first thing you need to do is find the Heading 1 styled paragraph that marks the start of the range you want to look in. You can then use the document’s GoTo method to go to the predefined bookmark that will give you the entire range of the heading level, starting at the Heading 1 para and extending to the next Heading 1 para.
You can then access the Tables collection of that Range
Don’t have access to a PC right now to give you the code, but there are numerous examples here on SO, for example Word VBA: How to delete sections of text from a template document using a heading (purely conicidental that it is one of mine!)

MS-Word - Pulling down cell content to the next page if a table is broken by a page-break

I have a table in Word that has column titles. When the page breaks the table rolls over to the next page and the headers repeat. However, I also have section titles that are important to see as well. If you look at the example below, I have the section '2' at the top next to sub-section 'C'.
a) I will be generating MHTML dynamically for import into Word so if it is possible to generate MHTML that will enable the above then that would be great. Otherwise ...
b) Is there any way within Word to manually or using VBA mark up the sections so they know to roll over to the next page automatically, so that the table will update itself if there are any changes to page-break locations. Alternatively...
c) I might have to write some VBA that checks that the section numbers are in the right place every time the VBA code is manually run, although I suspect that might start to get messy as I will also have to remove any existing 'pulled' section numbers that might have been inserted.
Thanks

Convert automatic numbering and bullets to plain text

I have a word document with automatic numbering and bulleting.
I have selected the text where I need to convert automating numbering and/or bulleting to normal text.
In addition I need to keep both the formatting and numbers/bullets of the selected text.
What I have already tried:
cut the paragraphs and special pasted them (but it breaks formatting);
unpressed the "numbering"/"bulleting" button (but it erases all numbers and bullets);
used VBA-macro (but it returns an error):
Code (error, method or data member not found):
Sub convertNumbersAndBulletsToText()
Selection.ConvertNumbersToText
End Sub
What would you recommend me to do in order to keep both formatting and numbers/bullets?
You have practically done everything yourself!
This code will work:
Sub convertNumbersAndBulletsToText()
Selection.Range.ListFormat.ConvertNumbersToText
End Sub
Your example returns error because ConvertNumbersToText method doesn't work with Selection. It works with Range!
(look here: Change selected automatic numbered list to plain text in word)
Beware!
If you want to carry out many changes, you may find it easier to make them with ActiveDocument (look below).
But if want to do it manually (or through a loop),
then you'd better loop from the last element you want to convert till the first one
(not vice versa, because auto-numbers would then increment by one all the time)!
Small Tips
Personally I would recommend you to use this code instead:
Sub convertNumbersAndBulletsToText()
Dim myRange As Range
Set myRange = Selection.Range
myRange.ListFormat.ConvertNumbersToText
End Sub
Why this one? It is a little bit more flexible! Instead of Selection.Range you could use any other type of Range (ActiveDocument, ActiveDocument.Paragraphs, myRange.SetRange etc)
Here are some links from msdn to give you basic examples of Ranges: 1) Range Object (Word) (msdn), 2)
Range.SetRange Method (Word) (msdn).
Just for your information, you don't need to save VBA if you don't want to. You can use Immediate Window to launch VBA.
Press alt+f11 (VBA-editor), then ctrl+g (Immediate Window).
Paste the code bellow, press enter.
Voilà!
Code (for Immediate Window):
ActiveDocument.ConvertNumbersToText
(It converts auto-numbers and auto-bullets to normal numbers and bullets everywhere in ActiveDocument).
The result of any VBA here would be number+tab+text. If you want to have number+space+text you can:
either at the very end replace (press ctrl+h) this one .^t (dot and tab) for . (dot and whitespace),
or at the very beginning 1) select the list, 2) right click on it, 3) click "Adjuct list idents", 4) click "Follow number with: Space". (Look here: Adjust the spacing for a single list item (support.office))
You may need to have a leading zero in (auto-)numbering, then you can press ctrl+f9, write SEQ MyList \# "000" inside curly brackets, press alt+f9 to finish (look here: Insert fields in Word (support.office)). But this goes beyond the question, though you may find word fields really useful in some cases.
To sum up:
You can replace both bullets and numbers for plain text in Word:
for Selection (look above);
for ActiveDocument (look above);
with a Range (examples in msdn);
with a loop (examples are welcomed). But bear in mind that you are to loop from the end of the document to the beginning.

Working with multiple discontinuous selection

I'm trying to do something with a multiple selection. I wanna add some text before every selected paragraph but, when I select multiple discontinuous paragraphs, if I do Selection.Paragraphs.Count I always get "1".
How could I work with all paragraphs apart?
Example:
Paragraph1(Selected first)
Paragraph2
Paragraph3(Selected second)
What I got when I try to add some text at the beginning of these paragraphs:
Paragraph1
Paragraph2
TEXTParagraph3
What I really want to obtain:
TEXTParagraph1
Paragraph2
TEXTParagraph3
I'm working like this:
sub x()
dim p as paragraph
for each p in selection.paragraphs
p.range.insertbefore("TEXT")
next
End sub
Word simply cannot do what you'd like for it to do. Developers have wished for this since multiple selections were introduced in 2003 (I think it was, might have been version 2007). Word's object model simply does not support it.
If this is something you want to provide to the user to make life easier you'll need to give the tool a way to mark the paragraphs so your code can recognize them. You could provide a macro, for example, that assigns an incrementing bookmark name to each selection (the user selects, then runs your macro; repeat for each paragraph). Your code can then address each bookmark and perform the actions. To make this more user friendly you can assign the macro to a keyboard shortcut and/or a button in the Ribbon/QAT and/or the right-click menu.

Delete page in Word

I need VBA code to delete all sections(pages) except first in Word document
For this I use below code.
For Each oSec In ActiveDocument.Sections
If oSec.Index <> 1 Then
oSec.Range.Delete
End If
Next oSec
This works but does not delete second section only removes its content.
If I removes if condition in code it removes content of first page.
I want to preserve content of first page.
Please tell me where I am making mistake.
When deleting you need to include section break marks. Try to change this line:
oSec.Range.Delete
into this one:
ActiveDocument.Range(oSec.Range.Start - 1, oSec.Range.End).Delete
BTW, you should not think that page=section, they are different type of document units.