Microsoft Word VBA radio buttons

Important Notice


Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.

  • I am currently working on to make a VBA code to insert new page with some text boxes and code. I simply want the code to be assigned to the radio button. But if i make a radio button on the current page, On assigning the code to it, it works and inserts the new page above the current page. But it doesn't show another radio button on the newly created page. I want the this radio button to be populate to each new page i insert upon clicking.


    I wrote a code that is named "Macro1"


    then i assigned the code to the radio button on page2.


    Code
    private sub CommandButton1_click()           Call Macro1
               End Sub

    #


    Can someone take a look on this ?

  • Re: Microsoft Word VBA radio buttons


    You don't provide enough information, such as the code to insert the control, to allow us to help you.


    Alternatives:
    Option 1 - You could make the page a template with the controls already on it.


    Option 2 - Add as many pages as you think the document will need, with the controls in place, then delete redundant pages.

  • Re: Microsoft Word VBA radio buttons


    To help you get a visual of what I am seeking,The Word document is dynamic and requires the ability to make changes and allow songs to be deleted or inserted into the word document.


    Here is the idea of how the user will manipulate the document. The user finds a song he/she needs to include in the document. For example we will say the song title is David Bowie-"Let's Dance". The user will open the word document and scroll to where the artist names start with "D". The user will find the page he/she wants to insert a new song in front or after . This should mean each page has 2 buttons. One button perhaps at the top right hand corner of the page that allows an insertion of a page above the current page, and one button perhaps at the bottom right hand corner of the page, that allows an insertion of a page after the current page where the button is pressed.
    Perhaps the user finds the page with Artist and Title named; Dave Matthews-"Grace Is Gone" and decides this new David Bowie song will be inserted before the Dave Matthews song. The user will press the top right button linked to a macro that allows for a new template page to be inserted before this current page, "Dave Matthews". Upon pressing the "add page" button a new page will appear before the "Dave Matthews" song.
    The user then scrolls to the new template page he/she just inserted using the button above. This new template has a textbox in the left hand corner, an Artist and Song Title Heading and then an area for lyrics



    Here is the macro which i have written and recorded.


    Sub Macro1()

    Selection.InsertBreak Type:=wdSectionBreakNextPage
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.MoveUp Unit:=wdLine, Count:=14

    Dim Shp As Shape
    Set Shp = ActiveDocument.Shapes.AddTextbox( _
    Orientation:=msoTextOrientationHorizontal, _
    Left:=50, Top:=50, Width:=90, Height:=54)
    Shp.TextFrame.TextRange.Text = "Key:" & vbCrLf & "BPM:" & vbCrLf & "notes:"
    Shp.TextFrame.TextRange.Font.Name = "Georgia"
    Shp.TextFrame.TextRange.Font.Size = 8
    Set Shp = Nothing


    Selection.Font.Name = "Georgia"
    Selection.Font.Bold = wdToggle
    Selection.Font.UnderlineColor = wdColorAutomatic
    Selection.Font.Underline = wdUnderlineSingle
    Selection.Font.Size = 18
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.TypeText Text:="Artist & Title"
    Selection.Style = ActiveDocument.Styles("Heading 1")
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.Font.UnderlineColor = wdColorAutomatic
    Selection.Font.Underline = wdUnderlineNone
    Selection.Font.Bold = wdToggle
    Selection.Font.Size = 12
    With Selection.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpace1pt5
    .Alignment = wdAlignParagraphCenter
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 0
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    End With

    Selection.Font.Bold = wdToggle
    Selection.TypeText Text:="Lyrics"
    Selection.TypeParagraph
    Selection.TypeText Text:="Lyrics"
    Selection.TypeParagraph
    Selection.TypeText Text:="{Chorus}"
    Selection.TypeParagraph
    Selection.TypeText Text:="Lyrics"
    Selection.TypeParagraph
    Selection.TypeText Text:="[Bridge]"
    Selection.TypeParagraph
    Selection.TypeText Text:="Lyrics"
    Selection.TypeParagraph
    Selection.TypeText Text:="{Chorus}"
    Selection.TypeParagraph
    Selection.TypeText Text:="Outro"
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1



    End Sub

  • Re: Microsoft Word VBA radio buttons


    Please use Code Tags when posting code.


    I don't know whether I can help you, but I'll give it a whirl.


    Forget the optionbuttons for now.


    Quote

    The user will open the word document and scroll to where the artist names start with "D"...

    I assume this is a master file of some type?


    Can you tell me a little more about it?


    Can I assume the exported document will be printed?


    I'm thinking we could have a list of all songs/artists in a listbox. Select the songs for export to new document. Using two listboxes, we can 'move' the songs from one list to the other, giving the user time to order the songs as required.


    As far as editing the created document, that could be problematic. We may be able to create the document using bookmarks so that we could create a new list for editing.


    Could you post a sample master file and a template file?

  • Re: Microsoft Word VBA radio buttons


    Thanks mate for your reply again,


    Actually this is not a master file, i am creating a word document which will contain Lyrics of songs from various artists.Under this file i have written a code to insert a new page. The newly "inserted page" is just a template with pre-set text boxes and per-defined text pattern where a user can easily type out or copy paste the lyrics of there desired song/artist.


    I am being able to assign this code to a radio button on a certain page and it also does work and inserts new page above that particular page. But the problem is that i want the new page to contain this radio button too. So that if i want to insert another page above that "newly created" page, i can easily do that.


    I am attaching the word document with the macro named as macro1 and sorry i have just joined any forum so i don't the rules of posting the code.


    Thanks

  • Re: Microsoft Word VBA radio buttons


    Seems to me this is more suited to Access rather than Word - you're storing structured data (even if some of it are song lyrics), Access will give you the ability to search on various key fields, if you include them, and so on.


    Any output can be easily sent to Word if the formatting options in Access reports are not to your liking.


    Alternatively, perhaps a quick Google might turn up something already written.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!