PDA

View Full Version : Using form fields in Word 2010



cjmitton
05-24-2016, 08:22 AM
I have a number of template forms that are setup with fields for my users to complete (they end save and send for authorisation). Its now been requested that the 'emailing for Authorisation' part gets automated.

I can set up the email and attach the document (already converted to a PDF) no problems at all. My main issue comes from the 'subject' line of the email.

The subject line is very specific to the form and what's entered on the form. For example, Form1 would the short code of F1 then have the date entered on the form and the name of the person used in the form: F1 24-05-2016 Bob Smith

One of the bigger issue is depending on the form, the subject line could have up to 6 different fields used, so coding the actual automatic 'merge' of the field to make up the subject is not straight forward.

I was wondering if I could set up a new field (or something similar) that when one of the required fields in the subject is updated the 'subject' field held on the form is changed. I can then take the subject field instead of try to work out all the possibilities of the different forms.

I'm not sure if I could use the 'run macro on exit' field option or if it could just automatically up date?

Thanks

gmayor
05-24-2016, 09:47 PM
Can we assume legacy form fields? If so it is relatively straightforward to collect data from a number of fields and assemble it as a text string foir use as a subject. This would have to be created individually for each template as the number of fields apparently differs. Can you confirm that these are legacy fields before suggesting code.

cjmitton
05-25-2016, 02:00 AM
Yes they are legacy form fields. I'm trying to avoid creating a macro for each form as at the moment I currently have over 20 forms and I add to the forms on a regular basis as more parts of the firm become 'digital'!

I did not know if there's a way I could set up a formula type field in word where I could concatenate the bookmarked fields as I need to make you the subject line? I've done this for excel in the past to get over this issue with emails reports.

gmayor
05-25-2016, 05:56 AM
If the number of fields varies, there is always going to be some setting up involved, however in this instance you could simply use REF fields to reproduce the required string in the document footer e.g.
F1{ CREATEDATE \@ "ddMMyyyy" }{ REF Text1 }{ REF Text4 }
and use the paragraph range to create the name.

Take a template with four fields with the default names Text1 to Text4. and add the following code to the template.
Run SaveMyDoc on exit from the last field. Save as a macro enabled template

When a new document is created from the template the field construction is added to the new document footer. SaveMyDoc saves using the values from Text1 and Text4 along with some fixed text and the date as indicated in your original post, which it reads from the footer, and then the paragraph added to the footer is removed and the document saved again.

Alternatively you could use the footer text as a subject for your e-mail before it is deleted.


Option Explicit

Sub AutoNew()
'F1{ DATE \@ "ddMMyyyy" }{ REF Text1 }{ REF Text4 }
Dim oFooter As Range
Const strField1 As String = "Text1"
Const strField2 As String = "Text4"

'Unprotect the file
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertParag raphAfter
Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs. Last.Range
oFooter.End = oFooter.End - 1
oFooter.Text = "F1"
oFooter.Collapse (0)
oFooter.Fields.Add oFooter, wdFieldDate, "\@ " & Chr(34) & "ddMMyyyy" & Chr(34), False
Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs. Last.Range
oFooter.End = oFooter.End - 1
oFooter.Collapse 0
oFooter.Fields.Add oFooter, wdFieldRef, strField1, False
Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs. Last.Range
oFooter.End = oFooter.End - 1
oFooter.Collapse 0
oFooter.Fields.Add oFooter, wdFieldRef, strField2, False
'Reprotect the document.
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
lbl_Exit:
Set oFooter = Nothing
Exit Sub
End Sub

Sub SaveMyDoc()
Dim oStory As Range
Dim oFooter As Range
Dim strFname As String
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs. Last.Range
oFooter.End = oFooter.End - 1
strFname = oFooter.Text & ".docx"
ActiveDocument.SaveAs2 FileName:="C:\Path\" & strFname
If Not ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs. Last.Range.Delete
'Reprotect the document.
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""
ActiveDocument.Save
lbl_Exit:
Set oStory = Nothing
Set oFooter = Nothing
Exit Sub
End Sub

cjmitton
06-21-2016, 01:43 AM
Sorry for the late reply, I've had a break from work then a Malware attack so I've been a bit busy! Yesterday was able to spend some time on this and found a slightly simpler resolution.

As suggested I added the Ref fields as Graham Mayor suggested, then on the relevant field that it was referencing made sure the Calculate on exit was enabled.
Then as each field in the form is updated the 'subject line' for my email is then built as I need in in the relevant format / order that I need.
So I can then use the newly created subject line I highlighted the whole section (subject line) and created a bookmark for that section. All I then need to do for the macro was add the following line of code to select the subject.


EmailSub = ActiveDocument.Bookmarks("EmailSubject").Range.Text


I can then roll out my authorisation macro as part of a standard template or a template in its own right!