Results 1 to 2 of 2
  1. #1
    Lounger
    Join Date
    Aug 2013
    Posts
    46
    Thanks
    11
    Thanked 0 Times in 0 Posts

    VBA word: how to design subroutin

    I have a repetitive task of printing 10 lines.
    Each line includes text, drop down list, and optional another drop down list.
    I wrote the following code per line, that does the job (at least I think so):
    Code:
    Selection.TypeText Text:="The Vital Capacity is:" & vbTab
    
    Dim objcc1 As ContentControl
    Set objcc1 = Selection.Range.ContentControls.Add(wdContentControlDropdownList)
    
        objcc1.DropdownListEntries.Add "Normal"
        objcc1.DropdownListEntries.Add "Low Normal"
        objcc1.DropdownListEntries.Add "Increased"
        objcc1.DropdownListEntries.Add "Decreased"
    
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.TypeParagraph
    This code is to be repeated 10 times, not a proper coding style.

    i was repeating to make a subroutine, something like:
    Code:
    Sub PrintLine(mytext As Integer, obj As ContentControl)
    
    End Sub
    The idea is to define the objcc1,objcc2 etc outside the subroutine, but the problem I see is that the SET line is also printing it, and the whole idea was to build the structure outside, and send it to the SUB for printing.
    Any idea how can I just declare and assign the drop down outside the SUB, and only printing it inside the SUB?
    And the first thing I saw is that if

  2. #2
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    Willow Grove, Pennsylvania, USA
    Posts
    205
    Thanks
    4
    Thanked 49 Times in 40 Posts
    Don't try to create the dropdowns in VBA. Instead, manually insert a dropdown content control in the body of the template that contains your macro, and manually add the list items to it. Then select the content control (click the little tab on its left side so the whole content control is selected), press Alt+F3, and define an AutoText entry -- a building block in the AutoText gallery -- and save it in the same template. Then delete the content control from the body of the template. Repeat the whole sequence for the second content control.

    In your code, insert the appropriate AutoText entry at the necessary place in each line. Hint: Don't use the Selection object in your code; instead, use a Range object that you declare in a Dim statement. In many respects a Range is similar to the Selection, but moving or redefining the Range object doesn't move the cursor or cause the screen to scroll, so it works much faster.

    Here's some sample code to illustrate, assuming that you named the first AutoText entry as "objcc1":

    Code:
    Sub PrintOneLine()
        Dim rng As Range
        Set rng = Selection.Range
        
        rng.Text = "The Vital Capacity is:" & vbTab
        rng.Collapse wdCollapseEnd
        
        ActiveDocument.AttachedTemplate.AutoTextEntries("objcc1").Insert _
            Where:=rng, RichText:=True
        
        Selection.MoveDown wdLine, 1
    End Sub
    
    Sub test()
        Dim i As Long
        For i = 1 To 10
            PrintOneLine
        Next
    End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •