Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-13-2022, 08:28 AM
mdr mdr is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2019
Novice
Code to reload userform checkbox value after unload
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default Code to reload userform checkbox value after unload

My apologies if I phrase this incorrectly, but I have self-taught myself some VBA based on threads here and on a few other forums.



I have a userbox that has textboxes that links to document variables and a checkbox named ChxMultiDef. When I unload the userform, I need a way to save the value of the checkbox so if I reopen the userform, the value of the checkbox comes back. I have found a way to do this for my textboxes/document variables, but not for the checkbox values.

This is what I do for the
For the textboxes/document variables, my code in the "OK" button is:
With ActiveDocument

.Variables("plaintiff").Value = Me.txtPlaintiff.Value

.Range.Fields.Update

End With
Unload me

AND my code in the Userform_initialize is:
With ActiveDocument
Me.txtPlaintiff.Value = .Variables("plaintiff").Value
End With
When I do this, when i unload the userform and then reload it, the textbox information is back. I just cannot figure out how to do the same with the checkbox.

Also, I cannot use the Me.Hide for a long reason that isn't necessary to explain here. And I don't want to save to the registry. When I have looked on excel forums, they seem to save to a hidden cell, but I don't think that works for Word.

Any advice would be greatly appreciated -- I've been pulling my hair out for days on this issue.
Reply With Quote
  #2  
Old 01-13-2022, 01:49 PM
Charles Kenyon Charles Kenyon is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,124
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

See Create a Simple Userform by Graham Mayor, MVP. It is a long page. He gives examples on reloading a userform.
Reply With Quote
  #3  
Old 01-13-2022, 02:12 PM
mdr mdr is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2019
Novice
Code to reload userform checkbox value after unload
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default

Thanks Charles -- I frequently find helpful information in yours and Graham's posts. I have read that post and I can't find info on the checkbox values. Basically, if the checkbox is clicked (or unclicked) when the userform is closed, i want the checkbox to remain clicked (or unclicked) when i reopen the userform again.
Reply With Quote
  #4  
Old 01-13-2022, 03:10 PM
Guessed's Avatar
Guessed Guessed is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,967
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Checkboxes don't typically have a name that begins txt. Are you sure it is a checkbox and not a textbox?

The type of the userform textbox and the variable must match with the code you are using. If one is a string and the other is a boolean then you will need to convert it.
For example, does ActiveDocument.Variables("plaintiff").Value return "True" or True? These are different - one is a string and one is a boolean. Can document variables hold anything other than strings?

Perhaps try to initialize the value with
Code:
Me.txtPlaintiff.Value = ActiveDocument.Variables("plaintiff").Value = "True"
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 01-13-2022, 04:41 PM
mdr mdr is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2019
Novice
Code to reload userform checkbox value after unload
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Default

Sorry, I wasn’t clear—the code I posted was what I am using for my textboxes. I need something for the checkboxes, because I can’t seem to get the textbox code to work for the checkboxes. And the textboxes relate to variables but the checkboxes don’t (they deal with bookmarks). I hope that makes sense. 😞
Reply With Quote
  #6  
Old 01-13-2022, 10:31 PM
gmayor's Avatar
gmayor gmayor is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You need to store the value somewhere, or read it directly from the document e.g.


Code:
Private Sub CommandButton1_Click()
    ActiveDocument.Variables("varMultiDef").value = ChxMultiDef.value
    Unload Me
End Sub

Private Sub UserForm_Initialize()
Dim oVar As Variable
    For Each oVar In ActiveDocument.Variables
        Select Case oVar.Name
            Case Is = "varMultiDef"
                ChxMultiDef.value = oVar.value
            Case Else
        End Select
    Next oVar
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 01-14-2022, 09:30 AM
mdr mdr is offline Code to reload userform checkbox value after unload Windows 10 Code to reload userform checkbox value after unload Office 2019
Novice
Code to reload userform checkbox value after unload
 
Join Date: Jan 2022
Posts: 8
mdr is on a distinguished road
Thumbs up Solved

Graham, I cannot thank you enough! This worked perfectly! Thank you!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Word userform checkbox (checked/unchecked) is not saved on the Application Quit matapagi2019 Word VBA 3 03-31-2019 02:29 AM
Code to reload userform checkbox value after unload Show Userform when Content Control CheckBox ticked derajlance Word VBA 1 05-13-2016 01:55 PM
VBA Code in a UserForm module to delete a Command Button which opens the userform Simoninparis Word VBA 2 09-21-2014 03:50 AM
Code to reload userform checkbox value after unload Checkbox in Userform lukael Excel Programming 5 02-18-2014 05:20 AM
Code to reload userform checkbox value after unload Checkbox on Userform result in Text in Word Dolfie_twee Word VBA 1 06-22-2010 07:54 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:51 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft