Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2010 VBA

Status
Not open for further replies.

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

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
 
If the bolded lines are not on a single line, then the IF statement is not closed!
Code:
Application.ScreenUpdating = False
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
    ActiveDocument.Unprotect ' Password:="aPassword"
Set fldFF = CurrentFF
As it stands:

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"

looks like it is on TWO lines, and the IF statement is not closed. This returns that error. When in doubt, ALWAYS explicitly close IF statements (and any other statement that requires closing).
Code:
    If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
        ActiveDocument.Unprotect ' Password:="aPassword"
    [b]End If[/b]


unknown
 
Sorry it took me so long to respond to this thread, but I am still receiving the same error. I added the End If statement after the following lines of code as described in the response; however, I am still receiving the same error. Any ideas. Thanks again.

-edward

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"
End If
 


You DO realize that you have MORE THAN ONE of those un-ended If statements, it appears.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
As it stands:

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"

looks like it is on TWO lines, and the IF statement is not closed

Actually it is closed. Right down the bottom of the myOnExit routine. There appear to be no unclosed If statements in this code (it is just difficult to see that).

Are we sure that the document has any bookmarks?
 
[blush] strongm is correct!

It helps me when I indent my code blocks...
Code:
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"
    End If
    Application.ScreenUpdating = True
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hmmm, admitedly I did not copy the code block into the VBE to properly see it, but I stand by my original statement.

"If the bolded lines are not on a single line, then the IF statement is not closed!

looks like it is on TWO lines, and the IF statement is not closed."

Although...blush, I was actually looking at (and wanted to use as the example):

If ActiveDocument.FormFields.Shaded = True Then ActiveDocument.FormFields.Shaded = False

because yes, clearly the

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect ' Password:="aPassword"

line IS properly terminated. The kety point was the IF it was not on a single line.

Nevertheless, strongm is correct.

I am wondering though:

Set fldFF = CurrentFF

Are you SURE you have the other function that sets the value of CurrentFF, and that CurrentFF is a Public variable?


unknown
 
Quite so, CurrentFF looks like the culprit. Hence my question about bookmarks (as a lack of bookmarks would be one explanation - of several - for CurrentFF failing to return a FormField, and thus ending up with the reported error message)
 
Wait. Stupid me. Yes, the Function to get CurrentFF is there. AND th ePublic variable if properly declared and Set.

Time to actually copy this stuff over and actually look at it. Especially as it appears to have orignated (at least in part) with me.




OK. Is it possible that you have created formfields with no name? This happens if you copy and paste formfields. They have no name, and therefore NO bookmark.


unknown
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top