Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-15-2022, 07:50 PM
jmf_techs jmf_techs is offline Macro to help turn text in square brackets into dropdown content controls Windows 10 Macro to help turn text in square brackets into dropdown content controls Office 2021
Novice
Macro to help turn text in square brackets into dropdown content controls
 
Join Date: Oct 2022
Posts: 2
jmf_techs is on a distinguished road
Default Macro to help turn text in square brackets into dropdown content controls

Hello!

I am new here so I apologize in advance if I break any rules. I work with technical fields and choices are often indicated with square brackets e.g. [option 1][option 2]. I am trying to write a macro to turn a selection of square backets into a dropdown content control. Is this something that is feasible?
Below is my attempt. I am not a programmer, so it might not make sense. It is patchwork from different snippets I have found over various forums. I have also attached a picture to illustrate what I am trying to do.

Thanks so much!


Code:
Sub Macro ()

Application.ScreenUpdating = False
Dim objCC As ContentControl
Dim Strtext As String

  
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "\[*\]"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While Selection.Find.Found
    StrTxt = Selection.Find.Text
    Set objCC = Selection.ContentControls _
        .Add(wdContentControlComboBox)
    objCC.Title = "Selection"
         objCC.SetPlaceholderText _
        Text:="Please make a selection"
    For Each Strttext In Selection.Find.Text
    objCC.DropdownListEntries.Add StrTxt
    Loop
End With
Application.ScreenUpdating = True
End Sub

Attached Images
File Type: png Screenshot 2022-10-15 224811.png (8.1 KB, 13 views)
Reply With Quote
  #2  
Old 10-16-2022, 05:29 AM
macropod's Avatar
macropod macropod is offline Macro to help turn text in square brackets into dropdown content controls Windows 10 Macro to help turn text in square brackets into dropdown content controls Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For example, assuming you first select all the options:
Code:
Sub AddComboBox()
Application.ScreenUpdating = False
Dim objCC As ContentControl, StrTxt As String, i As Long
With Selection.Range
  If InStr(.Text, "[") = 0 Then Exit Sub
  If InStr(.Text, "]") = 0 Then Exit Sub
  If .Characters.Last <> "]" Then .MoveEndUntil "]", wdBackward
  StrTxt = .Text
  StrTxt = Replace(StrTxt, "]", "")
  .Text = vbNullString
  Set objCC = .ContentControls.Add(wdContentControlComboBox)
  With objCC
    .SetPlaceholderText Text:="Please make a selection"
    For i = 1 To UBound(Split(StrTxt, "["))
      .DropdownListEntries.Add Split(StrTxt, "[")(i)
    Next
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-16-2022, 06:24 AM
jmf_techs jmf_techs is offline Macro to help turn text in square brackets into dropdown content controls Windows 10 Macro to help turn text in square brackets into dropdown content controls Office 2021
Novice
Macro to help turn text in square brackets into dropdown content controls
 
Join Date: Oct 2022
Posts: 2
jmf_techs is on a distinguished road
Default

Thank you! It worked! Amazing!
Reply With Quote
Reply

Tags
content control, microsoft word, vba content control



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to help turn text in square brackets into dropdown content controls Content Controls - Dependent Dropdown someazguy Word VBA 14 02-05-2023 08:01 PM
Clear all content controls (Text, dropdown) AND change option button values to FALSE in MS Word Form Janet D Word VBA 7 01-14-2023 02:36 AM
Macro to help turn text in square brackets into dropdown content controls Content Controls - Dependent Dropdown & Text Mightymegs Word VBA 6 05-18-2020 05:34 AM
Macro to help turn text in square brackets into dropdown content controls Add a return between two square brackets jeffreybrown Word VBA 15 02-20-2017 05:35 PM
Macro to save docx to doc that checks compatibility and converts content controls to static content. staicumihai Word VBA 4 10-12-2016 08:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:07 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