inkserious
Technical User
- Jul 26, 2006
- 67
Sometime back when I was using MS Word 2007 I had some help creating the following macro. After upgrading to Word 2010, I'm now receive the following error:
"Object Variable or With Block Variable not set."
Any ideas? Thanks for any help anyone can provide.
-edward
"Object Variable or With Block Variable not set."
Any ideas? Thanks for any help anyone can provide.
-edward
Code:
Option Explicit
Public fldFF As Word.FormField
Sub FillBM(strBM As String, strText As String)
' prcoedure that dynamically changes the bookmark content
' value is IN the bookmark, not outside it
Dim r As Range
Set r = ActiveDocument.Bookmarks(strBM).Range
r.Text = strText
ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
End Sub
Private Function CurrentFF() As Word.FormField
' gets the current formfield as an object
With Selection
If .FormFields.Count = 1 Then
' CheckBox or DropDown
Set CurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set CurrentFF = ActiveDocument.FormFields _
(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function
Sub myOnExit()
Application.ScreenUpdating = False
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"
Set fldFF = CurrentFF
With fldFF
If .Type = wdFieldFormTextInput Then
' files the BOOKMARK in the header with
' the formfield value
Call FillBM("h_date", CurrentFF.Result)
End If
' makes the value blank
.Result = ""
End With
If ActiveDocument.FormFields.Shaded = True Then ActiveDocument.FormFields.Shaded = False
If ActiveWindow.View.TableGridlines = True Then ActiveWindow.View.TableGridlines = False
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"
Else
End If
'delete the "Enter Date Here" text
ActiveDocument.Tables(1).Cell(Row:=1, Column:=1).Range.Select
Selection.Delete Unit:=wdCharacter, Count:=1
'Reprotect Document without resetting form fields
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True ', Password:="aPassword"
Application.ScreenUpdating = True
End If
End Sub