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

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

Related

Change Heading Number copied from another document

How can I change the Heading Number with VBA code?
For example, "1.1 Computer system"
I'd like to change "1.1" to "1.2".
I can read it with:
Selection.Paragraphs(1).Range.ListFormat.ListString
I can't find a way to change it.
Basic Function Test
1.1. LED Function Test Purpose: To make sure all the LED Functions are working as the Product Specification Resource Requirements:
The context is shown above. Sometimes, I copy from another document. The pasted heading number is not correct.
I tried to record the macro but the recorded macro is empty.
To force Heading 2 to start the numbering from 1.2, all you need is:
ActiveDocument.Styles(wdStyleHeading2).ListTemplate.ListLevels(2).StartAt = 2
Your update shows you're trying to do something quite different, however.
The only reliable way to retain the original numbering when copying/pasting between documents is to either:
convert the source numbering to static text before copying; or
paste the copied content as unformatted text.

In MS-Word how do you VBA split a table inserting a Section Break AND a paragraph mark

wdSectionBreakNextPage does not come with a trailing paragraph mark
I have a massive table i copy from excel to word sprinkled throughout with homemade fields PAGEX and SECTIONX. My macro will change these to page and section breaks, splitting the table for each (what I want). But page breaks come with a trailing paragraph mark before the table and section breaks do not. My feeble attempts to insert a paragraph mark after a section break always ends up with the paragraph mark inside the table.
My goal is to get the same gap between my header and the beginning of the table after both section breaks and page breaks. I think I simply want a macro to insert a paragraph mark after my section breaks similar to the way wdPageBreak works (i.e., between the break and the table). But I'm open to suggestions.
As perhaps further clarification, doing this manually involves creating a new row after a section break, then converting that row to text (quite impractical for the counts I'm looking at).
Do While .Execute
Selection.Delete
Selection.InsertBreak Type:=wdSectionBreakNextPage
Loop
Try:
ActiveDocument.Compatibility(wdSplitPgBreakAndParaMark) = False
thanks free and macro. i probably should have noted that i'm a vba novice (cut-and-paste mostly).
i decided it was easiest to just insert a page break after each section break, and then do a replace of ^m^p by ^p.
this works in my case, but won't work for others reading this if they have existing page breaks they don't want to lose.
GJ

Continue page numbering from a specific section in MS Word?

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

Unable to extract and re-insert MS Word Content Control using VBA and InsertXML

This question is related to my other question: Range.InsertXML using Transform
In MS Word it is easy to insert a content control using VBA, for example:
ThisDocument.ContentControls.Add wdContentControlRichText, Selection.Range
I've recently started exploring more in the XML side of things, e.g.:
Debug.Print ThisDocument.Range.XML seems to (or actually does) produce the XML for a Word document. However, if I create a NEW, BLANK document and add a Content Control I am unable to extract and reinsert the Content Control (oCC).
My steps:
added 2 blank paragraphs to a new document
added oCC to the 2nd paragraph
selected the oCC paragraph
immediate window: thisdocument.Paragraphs(1).Range.InsertXML selection.Range.XML
At first glance it LOOKS like the Content Control was duplicated, BUT on closer inspection, it was deleted and only the formatted text remains (see image, top paragraph is actually just formatted text).
Thinking I could out smart MS Word I set the properties of the Content Control to '...can not be deleted', but that didn't help.
I've also tried to insert into a separate document in case the issue had something to do with duplication of something that ought to have been unique.
In a nutshell:
To answer this question I need a way to insert a Content Control to a document using a combination of VBA and XML (or confirmation that what I am attempting is not possible).
Just realized I should use Selection.Range.WordOpenXML instead of Selection.Range.XML

Formatting issues duplicating table

Forward
I am making a "label program" that will print waybill information. Consists of a table in Word on a custom 3x5 inch document with 0 margins.
I currently have a simple form that, if you need copies, it will edit one of the cells so that each time it prints the "pieces count" is incremented. 1of10, 2of10, 3of10.....
While that worked the code submitted a separate print job for each "label". That created a problem for the end user where they would have to wait about 5-10 seconds between print jobs. When printing a couple of hundred of these at a time those seconds can add up.
Corrective solution
To try an alleviate this problem I wanted to make copies of the table so that 100 labels would be printed as one 100 page document. Found several solutions for copying pages of text but I have tables which complicated things. The closest solution I have found was:
With ActiveDocument
.Tables(1).Range.Copy
.Range.Select
'.Range.InsertAfter (Chr(11))
Selection.Collapse wdCollapseEnd
Selection.Paste
End With
And this does make a perfect copy of the table however it is merging the tables together. So if I wanted to loop this to create several more labels it would be doubling up the results every time since the code above just copies the first tabel.
To try and fix that issue I added a line break (vertical tab) before the paste. You will see that as the commented out line in the above snippet. This breaks up the tables but adds too much whitespace in between.
Page breaks seems like the solution here. While those did make the table break up they ended up creating a blank page in between each label which I was having enough of a time clearing from the GUI let alone in VBA.
The actual question
How can I take a table that is perfectly designed to fit on one 3x5 inch page and duplicate it X times. The caveat is I need to be able to find the cell programically that contains the pieces text. Currently I can use these absolute reference for the first table
ActiveDocument.Tables(1).Cell(5, 1).Range.Text
So if I had 3 tables for instance I need to be able to call each table and edit the text of the Cell(5, 1).
In case you ask
I know this functionality is better placed inside actual label programs like Bartender but those cost money that the company will not allocate for the only label my company uses.
The right concept was there. We needed to add a break in between the tables to disassociate them. Adding the vertical tab Chr(11) was obviously not the correct way to do it.
A proper section break would be the route to go here. Looking at MSDN you can see there are multiple types of section breaks. After testing a desirable outcome was acheived from using wdSectionBreakNextPage which is a "Section break on next page."
Basically just needed to add one line to the above code.
With ActiveDocument
.Tables(1).Range.Copy
.Range.Select
End With
With Selection
.Collapse wdCollapseEnd
.InsertBreak (wdSectionBreakNextPage)
.Paste
End With
I hate the use of Selection and I am going to look into that but for now this does function properly.
Since the tables are not single units we are able to query .Tables in order to edit each page individually.