Dynamic sections of Words documents and mapping to XML documents - vsto

I've been working on generating Word documents by using XML, an XSD schema, and a Word document project. The simple scenario is an invoice with invoice line items. I'm able to create content controls and by using the tutorials at MSDN, I've been able to pop open a document and see the top level items, such as billing address, shipping address, etc. My question now, though, is I'm getting into the dynamic part, the part where the invoice line items are displayed. An invoice obviously has one or more invoice line items. I don't see any datagrid-like object that I just bind a list to, so I started going down the route of finding where the table that should contain the invoice line items begins, and start adding PlainTextControls to the document. This requires that I find the exact paragraph location to start inserting and I'm not sure if it will work longterm if the document ever changes. Here's the relavent code:
Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
this.Paragraphs[47].Range.InsertParagraphBefore();
this.Paragraphs[47].Range.Select();
PlainTextContentControl ctl = vstoDoc.Controls.AddPlainTextContentControl("textControl1");
ctl.PlaceholderText = "Enter your first name";
With this code, I can start to work on adding invoice line items to the document, but this feels like a hack, keeping track of the which paragraph I'm on, and then what happens when I have too many lines for the table that I've created? Do I start adding lines to the document through code?
Before I go too far down this rabbit hole, I wanted to make sure that this was the prescribed method and I just have to suck it up and keep track of paragraphs, lines, etc. Am I on the right track or should I be doing something different?

Related

vba word 2016 saveas document without changing module name

I suspect my strategy is incorrect as I can't seem to apply my search results for the keywords I've used over the last few days. ( https://stackoverflow.com/questions/54496552] seems to be the closest. )
I am able to populate a single document from the two forms I've built and save it under a new name
...
MsgBox "Saving as " & aFullPath
ActiveDocument.SaveAs FileName:=aFullPath, FileFormat:=wdFormatDocumentDefault
But that changes the name of my parent document that contains my forms (Word2016 document name: waiver.docm). In practice, that won't be a problem because the user will not be saving "waiver.docm" except by accident.
But that's why I think my approach is wrong.
Ideally my VBA code would
Load a protoype waiver template with the page heading, bookmarks and table as I have now in my waiver.docm.
Upon filling that template, append another waiver template as a new page.
And return control to the form
Repeat above two steps of appending of sheets until the user indicates completion (e.g. "Finish" command button). Typically after 1 to 4 pages
Print and save the entire document.
Right now I interrupt the process after each page to force my prospective user to print and save the document (and under a unique name).

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

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

Word / PDF - Merge Documents

I am looking to merge two documents, however it is not your typical merge.
My first document is a mailmerge, creating a cover letter, basically each page has a name and address
My next document is a static document that cannot be changed.
I need to insert the static document into my first merged document, but after every page, therefore, for every one page a document is inserted.
I have tried the insert document in both word 2010 and pdf using adobe acrobat, and as you have thought it only inserted one document after the first page.
I'm looking at VBA, but I have never utilized VBA and word before
Any pointers would be appreciated.
Many thanks
I should have spent more time on this.
The original template contains fields to merge.
On the static document that I mention, click insert tab, Text Section, select Object - Text From File
Select the cover letter / template that contains the fields to merge. This will insert the template followed by the static document that cannot be changed
Note I have spotted some formatting changes on the template following merge - further work required
From this point start your mail merge, and complete merge to Adobe or word.
This creates a mail merged document containing the cover letter with name and address fields followed by the static document.
Extremely simple. I always over complicate things!
I'll work on the changed formatting, but other then that this works

Using VBA in MS Word 2007 to define page elements?

I'd like to be able to create a page element which I can feed text and it will form itself into the preferred layout. For instance:
{MACRO DocumentIntro("Introduction to Business Studies", "FP015", "Teachers' Guide")}
with that as a field, the output should be a line, the first two strings a certain size and font, centred, another line and then the third string fonted, sized and centred.
I know that's sort of TeX-like and perhaps beyond the scope of VBA, but if anyone's got any idea how it might be possible, please tell!
EDIT:
Ok, if I put the required information into Keyword, as part of the document properties, with some kind of unique separator, then that gets that info in, and the info will be unique to each document. Next one puts a bookmark where the stuff is going to be displayed. Then one creates an AutoOpen macro that goes to that bookmark, pulls the relevants out of the keywords, and forms the text appropriately into the bookmark's .Selection.
Is that feasible?
You're certainly on the right track here for a coding solution. However, there is a simpler way with no code - this is the type of scenario that Content Controls in Word 2007 were built for and with Fields/Properties, you can bind to content controls (CC). These CC can hold styles (like centered, bold, etc.). No VBA required.
The very easiest thing to do is to pick 3 built-in document properties that you will always want these to be. For example, "Title" could be your first string, "Subject" your second string and "Keywords" your third. Then, just go to the Insert ribbon, Quick Parts, Document Properties and insert, place and format those how you like. Then go to Word's start button (the orb thingy) and then under Prepare choose Properties. Here you can type, for example "Introduction to Business Studies", into the Title box and then just deselect it somehow (like click in another box). The Content Control for Title will be filled in automatically with your text.
If you want to use this for multiple files, just create this file as a .dotx (after CC insertion/placement/formatting and before updating the Document Properties' text). Then every time all you'll have to do is set these three properties with each new file.
Well, yes, it did turn out to be feasible.
Sub autoopen()
Dim sKeywords As String
sKeywords = ActiveDocument.BuiltInDocumentProperties(4)
ActiveDocument.Bookmarks("foo").Select
Selection.Text = sKeywords
End Sub
Okay, I have some filling out to do, but at least the guts of it are there.