PDA

View Full Version : Word Macro to add predefined content at the end of document on click of button



drexciyan
03-22-2016, 11:02 AM
I have an document in word, which has some fields to be filled, and an button to which I want to assign a macro. When that button is clicked, that same form with empty fields needs to be appended to the end of document, including the button (which can be clicked again and do the same thing).
Document is in attachment.

gmaxey
03-22-2016, 02:33 PM
That isn't going happen. You can create a building block of the content (less the button) and save it as building block. You can add code to insert that building block at the end of the document each time the button is clicked. You can't reassign the code from the old button to the new button though. Why don't you add a button to the Ribbon or QAT that inserts the content?

drexciyan
03-22-2016, 03:34 PM
Thanks for your reply. I cant use the ribbon button because I intend to distribute this document with macro to my clients which need to fill it. Upon your advice, I managed to save an Building Block, but I'm not sure how to "attach" it to document, so when I send this document to my clients, macro works as it should, inserting that Building Block on end of document each time the button is clicked.

gmayor
03-22-2016, 11:04 PM
You can certainly add buttons to the ribbon of your document, but the fact that you are going to distribute the document creates other issues. You cannot save building Blocks in a document and the document must be macro enabled (and contain the code) to run macros from a document. You are going to find it a challenge to get some users to run macro code, particularly in a corporate environment.

gmaxey
03-23-2016, 04:42 AM
You could get around the need for a building block and template by using a bookmark to define the content range. You could use a plain text CC (On Enter Event) as the "trigger" initiate the process. You will still have the potential issues that Graham mentions regaring macros.

Bookmark your content "Content" Replace your button with a plain text CC. Tag it "Trigger" Use:


Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Dim oRng As Word.Range
Dim oTarget As Word.Range
Dim oCC As ContentControl
Set oRng = ActiveDocument.Bookmarks("Content").Range
Set oTarget = ActiveDocument.Range
oTarget.Collapse wdCollapseEnd
oTarget.InsertBefore vbCr
oTarget.Collapse wdCollapseEnd
oTarget.FormattedText = oRng.FormattedText
For Each oCC In oRng.ContentControls
If oCC.Tag = "Trigger" Then
oCC.Tag = ""
Exit For
End If
Next
ActiveDocument.Bookmarks.Add "Content", oTarget
End Sub

gmayor
03-23-2016, 07:53 AM
Greg
I would assume that a user would add a page after filling the first one. Isn't that going to add another ready filled page? I guess you would need to clear the values from the content controls from the duplicate page? I still think it might be better with a ribbon button to duplicate the range, though I like the bookmark idea. :)

gmaxey
03-23-2016, 08:38 AM
Graham

Yes it can use some polish. Probably page break before and some code to reset the content.

gmaxey
03-23-2016, 01:09 PM
[]Graham,

Pondered this while trying to weld pipe today. Has some good ideas but didn't weld so good :-(

I think a rich text CC would be better than a bookmark as a container for the content. See attached.ATTACH]15745[/ATTACH