Closing UserForms

L

LEU

I have a document that if the user clicks any where in the Bookmark area it
opens a UserForm to be filled out. When I close the UserForm using the
Command Button (Unload Me) or Red X in the upper right hand corner of the
Userform, then reopen the UserForm, it does not initialize with the data from
the document correctly. The problem is with Check Boxes. I have other
UserForms without check Boxes and they work fine. Here are my macros:

Macros to open the Userform

Option Explicit
'reserve memory for an application variable
Private WithEvents wdApp As Word.Application

Private Sub Document_New()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub Document_Open()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument Then Exit Sub
'get out of the header if we're in it
Select Case Sel.StoryType
Case wdMainTextStory
If Sel.Range.InRange(ActiveDocument.Bookmarks("bkRollingHistory").Range) Then
ActiveDocument.Bookmarks("bkSignature").Select
UserForm1.Show
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("bkOversight").Range) Then
ActiveDocument.Bookmarks("bkOS1").Select
UserForm2.Show
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("CRNbr1").Range) Then
ActiveDocument.Bookmarks("CRNbrReturn").Select
UserForm3.Show
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("CRNbr2").Range) Then
ActiveDocument.Bookmarks("CRNbrReturn").Select
UserForm3.Show
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("CRNbr3").Range) Then
ActiveDocument.Bookmarks("CRNbrReturn").Select
UserForm3.Show
ElseIf Sel.Range.InRange(ActiveDocument.Bookmarks("CRNbr4").Range) Then
ActiveDocument.Bookmarks("CRNbrReturn").Select
UserForm3.Show
End If
Exit Sub
End Select
End Sub


My UserForm Macros

Sub CommandButton1_Click()
With ActiveDocument
.FormFields("bkText52").Result = TextBox1
.FormFields("bkText33").Result = TextBox2
.FormFields("bkText53").Result = TextBox3

For i = 1 To 50
If Me.Controls("CheckBox" & i).Value = True Then
.FormFields("CB" & i).CheckBox.Value = True
Else
.FormFields("CB" & i).CheckBox.Value = False
End If
Next i
End With
Me.Hide
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub

Sub UserForm_Initialize()
On Error Resume Next
Dim i As Integer
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
TextBox1.Value = oBMs("bkText52").Range.Text
TextBox2.Value = oBMs("bkText33").Range.Text
TextBox3.Value = oBMs("bkText54").Range.Text

For i = 1 To 50
If oBMs("CB" & i) = True Then
Me.Controls("CheckBox" & i).CheckBox.Value = True
Else
Me.Controls("CheckBox" & i).CheckBox.Value = False
End If
Next i
End Sub
 
D

Doug Robbins - Word MVP

Use

With ActiveDocument
For i = 1 To 50
If .FormFields("CB" & i).Checkbox.Value = True Then
Me.Controls("CheckBox" & i).Value = True
Else
Me.Controls("CheckBox" & i).Value = False
End If
Next i
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
L

LEU

Thanks, that worked.



Doug Robbins - Word MVP said:
Use

With ActiveDocument
For i = 1 To 50
If .FormFields("CB" & i).Checkbox.Value = True Then
Me.Controls("CheckBox" & i).Value = True
Else
Me.Controls("CheckBox" & i).Value = False
End If
Next i
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top