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!)
Related
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
How does one continue the page numbering from a specific section in MS Word?
Consider the following scenario:
MS Word document with 3 sections A, B, and C.
Page numbering starts at 1 on the first page of section A (ends at, let's say, 3, on page 3).
No page numbering (or alternate page numbering) in section B.
In section C, page numbering continues from that of section A (first page-number of section C is <last_page_A> + 1 = 4).
Now let the number pages in section A, B, and C vary and the page numbering
to automatically adapt. The document has a Table of Contents.
Is this possible using the MS Word GUI through some trickery I am not aware of? If not, how could one do this with MS Office Field Codes or VBA?
To my knowledge, the MS Word GUI only lets you continue page numbering from the previous section, or alternatively, lets you manually handle the page numberings:
To insert a page number cross-reference, follow these steps:
Insert a bookmark on the page containing the information you want to cross-reference.
Position the insertion point where you want the cross-reference to occur.
Press Ctrl+F9 to insert field brackets. Make sure the insertion point stays between the brackets.
Type pageref followed by the bookmark name used in step 1.
Press F9 to update the field information.
Please refer to the following link:
Inserting Page Number Cross-References
Controlling the Format of Cross-References
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.
Is there a way to have vba select a field in a word doc using the NAME attribute of the field CODE property? maybe using a pseudo selector?
background:
i have 8 fields (DOCPROPERTY Name_First, Name_Last, etc) in my document in 4 places (total 32 items)
instead of looping through the entire collection of fields, I want to be able to return a collection of fields that match Name_First, and then make changes,InsertAfter, etc, then call the update method on those fields only. Updating all fields is causing a noticeable delay on each form field when exited to the next field.
The MSDN documentation only shows examples using ordinal index numbers, which isn't very helpful in my real world. If someone changes the ordinal position of a field, DonkeyKong!
A bookmark can be referenced by name. If you want to update say 4 fields to reflect Name_First, you could place bookmarks on all 4 fields with names like "Name_First_1", "Name_First_2" etc. Then update the fields like:
For i = 1 To 4
ActiveDocument.Bookmarks("Name_First_" & i).Range.Fields(1).Update
Next
In your words: the bookmarked range is the pseudo selector. The field to update is always the first field in the referenced range.
I'm trying to create a script in VBA for a Word document that bookmarks and hyperlinks matching strings with numbering to a location later in the document. Right now I have two sets of matching for matching text that looks like this (sometimes with hundreds of more entires):
Contents
'There is a bookmark here named "InpCon"
4326: Info 1
32534: Info 2
7980: Info 3
Body
'There is a bookmark here named "InpBod"
4326: Info 1
32534: Info 2
7980: Info 3
This is what I'm trying to do:
Table of Contents
'There is a bookmark here named "InpCon"
4326: Info 1 'Hyperlink to Bookmark Name: "Info1"
32534: Info 2 'Hyperlink to Bookmark Name: "Info2"
7980: Info 3 'Hyperlink to Bookmark Name: "Info3"
Body
'There is a bookmark here named "InpBod"
4326: Info 1 'Bookmark Name: "Info1"
32534: Info 2 'Bookmark Name: "Info2"
7980: Info 3 'Bookmark Name: "Info3"
I'm trying to implement this at the end of a larger script I have. The larger script copies everything under the "InpBod" bookmark to the "InpCon" bookmark at one point. I don't know if inputting a process there to do this would be more efficient.
Basically, my VBA knowledge is pretty limited. I've tried looking for something that accomplishes this task, but haven't found anything. I'd paste my code, but it's pretty large and on a stand alone system. This would shave hours off my week if I can get it done. Can someone help me out or point me in the right direction? Thanks you in advance.
Don't "manually" generate "InpCon". Use Word to generate the TOC. When generating "InpBod", everytime you come across something you want to link to, mark it and then actomatically generate the TOC. Two possible ways to do so:
Use heading styles and then generate a TOC. Everytime you stumble upon an entry to be marked, mark the paragraph as a heading Selection.Range.Paragraphs.Style = ActiveDocument.Styles(wdStyleHeading1). You may want to change the default heading styles. Then insert a TOC at the beginning.
Use bookmarks if the document uses headers for something else. Everytime you stumble upon an entry to be marked, create a bookmark like this: ActiveDocument.Bookmarks.Add Range:=Selection.Range.Collapse wdCollapseEnd, Name:=**THEBOOKMARKNAME**. Notice that you'll have to remove spaces and that kind of stuff for the bookmark name. Then iterate thorugh all the activedocument.Bookmarks inserting the links using Selection.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _
wdContentText, ReferenceItem:=ITEM, InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
The macro recorder will expose every object you need to do this.
Using newer info from the comments:
The Word's list object model is pretty well illustrated here: Looping a Word macro over many list paragraphs causes memory issue and here http://oreilly.com/catalog/writewordmacro/chapter/ch17.html with more detail. Here's the proper documentation: http://msdn.microsoft.com/en-us/library/aa223019(v=office.11).aspx
You should loop through the lists collection members (for what I gather you should only have one list, the InpBod list). Then loop through the paragraphs in that list selecting each like this:
Dim para as Range
For Each para In ActiveDocument.Lists(1).ListParagraphs
para.Range.Select
Next para