0
votes

I edit and create word-document forms for an energy company. Field personnel will be opening these forms on a windows tablet in the field. We use Microsoft Word 2010. I have used the developer tab to add check-boxes and date pickers, and other functions to make the filling in of these forms quicker and easier on a tablet - but we have a few needs that will need to be coded.

-The forms are password protected, with only certain fields available for the end user to edit.

-My boss would like the user to have the option to add an additional page to the document with the click of a button, preferably on the form itself, but still have the form be password protected. He would like this additional page to be for adding photos of the work they are performing and checking for quality. He would like them to be able to add as many pages of photos as they need (I thought we could "call" a specific template with a button, add the button to the bottom of the page, and as they fill a page in, they could click the button on the template, and repeat the process).

-I have created a template page for this additional page, which is also password-protected, and has two of the "insert picture" fields from the developer tab, and a 1x1 table beneath each for description of the picture.

I really do want to learn VBA, and would rather not just copy and paste someone else's code. I want to understand why it works and build on it, as needed.

I have coded in html, css, and javascript (the basics).

1
The problem you have is a bit too broad to answer. Start with smaller pieces of your feature and use google to find starting points. - Chris

1 Answers

0
votes

After many days of cursing and trawling forums, I came up with this solution, and decided to leave it here in case someone else is interested in this for their own project:

Private Sub PhotoPageTrigger_Click()

' PhotoPage Macro

ActiveDocument.Unprotect Password:="password" 'unprotect the document with the password

Selection.GoTo What:=wdGoToBookmark, Name:="\EndofDoc" 'move cursor to the end of the last page

Selection.TypeParagraph 'insert new paragraph to make a new page

Selection.Font.Size = 12 'make the new page have a text size of 12

ChangeFileOpenDirectory _
    "I:\FileLocation\" 'Go to this file location

Documents.Open FileName:= _
    "I:\FileLocation\Form Picture Template.docm" _
    , ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
    WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
    wdOpenFormatAuto, XMLTransform:="" 'Open this template document and read with these settings

Selection.GoTo What:=wdGoToBookmark, Name:="\StartofDoc" 'move cursor to the beginning of the document

Selection.WholeStory 'select all

Selection.Copy 'copy all

Selection.GoTo What:=wdGoToBookmark, Name:="\EndofDoc" 'move cursor to home place at the end of the document

ActiveDocument.Close 'close the open template doc

Selection.PasteAndFormat (wdUseDestinationStylesRecovery) 'paste contents of clipboard to now-active form doc at the cursor location

Dim oData   As New DataObject 'object to use the clipboard

oData.SetText Text:=Empty 'Clear the clipboard
oData.PutInClipboard

ActiveDocument.Protect Password:="password", NoReset:=False, Type:= _
    wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=True 'reprotect document with password and these settings

End Sub

Pretty happy to have made it work all on my own! :)