VBA to Retain the content of some Word Form fields while clearing the content of all remaining Form fields

Hello,

 

I have a protected Word Form that contains a variety of form fields. The document is storing project information.

 

At the one year anniversary, most of the entries in the fields need to be reevaluated and thus blanked out, but some of the field entries should remain unchanged.

 

The fields that have data that will be kept unchanged have Bookmarks.  Those Bookmarks are used in some instances to pass the contents of a field to other areas in the document via Cross References.  In other instances, those Bookmarks are used in Macros to pass the contents of the field to another field.

 

Part 1 of my question –Is it possible to keep the entries of the fields that will be unchanged while at the same time clearing the entries of those fields that need be changed?  For example I would like to keep the contents of the fields whose Bookmarks are “ProjectNumber” and “StartDate”, but I would like to clear all the remaining fields.

 

Part 2 of my question – If this is possible, is the coding different for Text vs Drop-Down vs Check Box form fields?

 

Part 3 of my question – Will the cross references created from the “ProjectNumber” and StartDate” bookmarks still reflect their original data or will I need to do some coding for that?

 

Thank you

 

Mark

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFF As FormField
For Each oFF In ActiveDocument.Range.FormFields
  Select Case oFF.Name
    Case "ProjectNumber", "StartDate"

       'Do nothing. Answer 1.

    Case Else

      'Answer 2

      Select Case oFF.Type
        Case 70: oFF.Result = vbNullString
        Case 83: oFF.DropDown.Value = 1
        Case 71: oFF.CheckBox.Value = False
      End Select
  End Select
Next oFF

'Answer 3

ActiveDocument.Fields.Update
End Sub

Greg Maxey
***
Death smiles at us all, but all a man can do is smile back.


For more help with Word visit:
http://gregmaxey.com/word_tips.html

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Hello Greg,

Thank you for the wonderful macro you created.

It works splendidly with one hitch.  The macro did save the entry in the "ProjectNumber", and"StartDate" fields and erased the contents in all the other fields in the document.    Unfortunately during the execution of the macro, MS Word displays a (Not Responding) message in the title bar.  When that message appeared, I performed the following steps:

- I press Ctrl+Break

- A Visual Basic screen appears stating "Code execution has been interrupted."

- I click the "Debug" button from within the screen.

- The "End Select" line directly below the "Case 71: oFF.CheckBox.Value = False" line is highlighted in Yellow.

I have tried running the macro a few times now .  I obtain the same results each time.  I waited 10 minutes to see if MS Word would start responding again to no avail.

Any suggestions you have will be appreciated.

Thank you

Mark

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Here's an alternate method

Dim StrProjectNumber As String, strStartDate As String
With ActiveDocument
    StrProjectNumber = .FormFields("ProjectNumber").Result
    strStartDate = .FormFields("StartDate").Result
    .Unprotect
    .Protect wdAllowOnlyFormFields, False
    .FormFields("ProjectNumber").Result = StrProjectNumber
    .FormFields("StartDate").Result = strStartDate
End With

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

It seems you might have a form field that's reporting a Type value that isn't one of the three cases. That shouldn't be possible -- there are only three types of FormFields -- but the symptom is that of a Select Case that doesn't know what to do.

The quick fix is to insert another Case Else statement between the "Case 71" line and the "End Select". That might leave one or more form fields still filled; you could delete those fields from the document and reinsert them to see if that helps.

The more definite solution is to find out what VBA thinks the field is. When you stop the macro and click Debug, select the expression oFF.Type, right-click it, and click Add to Watch. Don't change anything in the dialog box that appears, but just press Enter. A window will open at the bottom of the VBA editor, and it will show the current value of oFF.Type. If that value isn't 70, 71, or 83, what is it?

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 1,378 Applies to: