Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2023, 05:05 AM
Formd Formd is offline Inserting text from checkboxes Windows 7 32bit Inserting text from checkboxes Office 2007
Advanced Beginner
Inserting text from checkboxes
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default Inserting text from checkboxes

Hello,
Is there another way to write the codes I have below?
The idea is to insert the text shown in the text box including "and" if there are multiple entries.

If Me.Chbx2 = True And Me.Chbx3 = True And Me.Chbx4 = True And Me.Chbx5 = True And Me.Chbx6 = True Then


Selection.GoTo What:=wdGoToBookmark, Name:="First"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.TypeText Text:=" CheckBox2, "
Selection.TypeText Text:="CheckBox3, CheckBox4, CheckBox5 and CheckBox6"

OR if the below is my selection

If Me.Chbx2 = True And Me.Chbx3 = False And Me.Chbx4 = True And Me.Chbx5 = And Me.Chbx6 = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="First"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.TypeText Text:=" CheckBox2, CheckBox4 and CheckBox6"


Thanks in advance.
Reply With Quote
  #2  
Old 08-08-2023, 05:06 AM
Formd Formd is offline Inserting text from checkboxes Windows 7 32bit Inserting text from checkboxes Office 2007
Advanced Beginner
Inserting text from checkboxes
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default Correction

If Me.Chbx2 = True And Me.Chbx3 = True And Me.Chbx4 = True And Me.Chbx5 = True And Me.Chbx6 = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="First"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.TypeText Text:=" CheckBox2, "
Selection.TypeText Text:="CheckBox3, CheckBox4, CheckBox5 and CheckBox6"

OR if the below is my selection

If Me.Chbx2 = True And Me.Chbx3 = False And Me.Chbx4 = True And Me.Chbx5 = False And Me.Chbx6 = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="First"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
Selection.TypeText Text:=" CheckBox2, CheckBox4 and CheckBox6"
Reply With Quote
  #3  
Old 08-08-2023, 05:45 AM
gmayor's Avatar
gmayor gmayor is offline Inserting text from checkboxes Windows 10 Inserting text from checkboxes Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

The following should work for you
Code:
    Select Case True
        Case Chbx2 = True And Chbx3 = True And Chbx4 = True And Chbx5 = True And Chbx6 = True
            FillBM "First", "CheckBox2, CheckBox3, CheckBox4, CheckBox5 and CheckBox6"
        Case Chbx2 = True And Chbx3 = False And Chbx4 = True And Chbx5 = False And Chbx6 = True
            FillBM "First", " CheckBox2, CheckBox4 and CheckBox6"
    End Select
The code calls the following sub to write the value to the named bookmark
Code:
Public Sub FillBM(strbmName As String, strValue As String)
'Graham Mayor - https://www.gmayor.com
Dim oRng As Range
    With ActiveDocument
        On Error GoTo lbl_Exit
        If .Bookmarks.Exists(strbmName) = True Then
            Set oRng = .Bookmarks(strbmName).Range
            oRng.Text = strValue
            oRng.Bookmarks.Add strbmName
        End If
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
You might consider using a content control instead of a bookmark as content controls are less likely to be accidentally deleted. The following will write to a titled content control
Code:
Public Sub FillCC(strCCTitle As String, strValue As String, bLock As Boolean)
'Graham Mayor - https://www.gmayor.com - Last updated - 03 Sep 2021
Dim oCC As ContentControl
    On Error GoTo lbl_Exit
    For Each oCC In ActiveDocument.ContentControls
        If oCC.Title = strCCTitle Then
            oCC.LockContents = False
            oCC.Range.Text = strValue
            oCC.LockContentControl = True
            oCC.LockContents = bLock
            Exit For
        End If
    Next oCC
lbl_Exit:
    Set oCC = Nothing
    Exit Sub
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
  #4  
Old 08-08-2023, 03:40 PM
Guessed's Avatar
Guessed Guessed is offline Inserting text from checkboxes Windows 10 Inserting text from checkboxes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
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

The Checkboxes can only be True or False so lines like this
Code:
Case Chbx2 = True And Chbx3 = True And Chbx4 = True And Chbx5 = True And Chbx6 = True
can be made much shorter
Code:
Case Chbx2 And Chbx3 And Chbx4 And Chbx5 And Chbx6
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 08-10-2023, 04:49 AM
Formd Formd is offline Inserting text from checkboxes Windows 7 32bit Inserting text from checkboxes Office 2007
Advanced Beginner
Inserting text from checkboxes
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default

Thank you for this. Select Case worked for me however, I got an error with FillBM; not sure if I was supposed to substitute something there. I used the following and it works.

Select Case True
Case Chbx2 = True And Chbx3 = True And Chbx4 = True And Chbx5 = True And Chbx6 = True
Selection.TypeText Text:= "First", "CheckBox2, CheckBox3, CheckBox4, CheckBox5 and CheckBox6"
Case Chbx2 = True And Chbx3 = False And Chbx4 = True And Chbx5 = False And Chbx6 = True
Selection.TypeText Text:= "First", " CheckBox2, CheckBox4 and CheckBox6"
End Select


Content Controls, I am not familiar with but I am going to try it.
Reply With Quote
  #6  
Old 08-10-2023, 05:37 AM
gmayor's Avatar
gmayor gmayor is offline Inserting text from checkboxes Windows 10 Inserting text from checkboxes Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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

FillBM is a function that writes a value to a bookmark. In your case it writes to a bookmark named "First" called from the case statement. The bookmark must exist.
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
UserForm with checkboxes that hide/show bookmarked text in document dohertym Word VBA 4 05-16-2022 09:48 PM
Inserting text from checkboxes Use VBA to create checkboxes and text boxes in Word rotunda Word VBA 6 06-16-2021 03:13 PM
Macro-inserting text box BLUEPUPIL Word VBA 14 04-16-2018 06:59 AM
Inserting text from checkboxes Inserting and rearranging text by inspecting existing text rok123 Word VBA 9 02-05-2016 07:31 PM
Inserting text from checkboxes Mutually exclusive checkboxes with text jt3writer Word VBA 2 01-21-2015 12:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:15 PM.


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