Results 1 to 2 of 2
2013-08-12, 16:35 #1
- Join Date
- Aug 2013
- 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):
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
i was repeating to make a subroutine, something like:
Sub PrintLine(mytext As Integer, obj As ContentControl) End Sub
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
2013-08-13, 20:34 #2
- Join Date
- Feb 2001
- Willow Grove, Pennsylvania, USA
- 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":
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