Repeating code with slight variations

J

Jason Logue

Sub CommandButton1_Click()
Hi, I have posted this question before, but I think my posting was too
long and convoluted. Okay, a user is filling out a template in Word
by using automated userforms. On the third userform to appear, there
are four added command buttons that open a window with a list of
Chapter numbers (27 chapters). The user clicks a chapter command
button and access the another window with a list of all the sections
in that chapter (these sections are bookmarked in the chapter
documents). The user then clicks on a section button and that
information populates the corresponding textbox, which can then be
edited by the user and eventually populates the template form.
Because there are four textboxes, I had to repeat the code for each
different textbox. As you can imagine, this too long time. Below I
have put pound signs where the changing variables are located. The
code that pulls the bookmarked information into the template is the
same, except that the textboxes change each time. Right now I have
over a 100 userforms. This is causing my template size to get rather
large, and I am concerned for problems later. I am sure ther is an
easier way to do this, but since I am a newbie, I am not sure how.
I'd appreciate any help. One person suggested using Autotext, but I
am not sure how that fits into this.



Sub CommandButton1()


Dim i As Integer, source As Document
Set source = Documents.Open("C:\Documents and
Settings\Logue\My Documents\Allied
Title\exceptions\chapter#(chapters 1-27).doc")
For i = 1 To source.Bookmarks.Count


Userform3.TextBox*.Text = source.Bookmarks(# (this depends on how
many bookmarks are in that particular document)).Range

- This selects the first bookmark in chapter one. and
inserts the info in Textbox8.

Next i

Documents("C:\Documents and Settings\Logue\My
Documents\Allied Title\exceptions\Chapterone.doc").Close
SaveChanges = False

End Sub


-TIA,
Jason
 
W

Word Heretic

G'day (e-mail address removed) (Jason Logue),

you can pass a control as an object to a generic routine that fills it
for you.


(e-mail address removed) (Jason Logue) was spinning this yarn:
Sub CommandButton1_Click()
Hi, I have posted this question before, but I think my posting was too
long and convoluted. Okay, a user is filling out a template in Word
by using automated userforms. On the third userform to appear, there
are four added command buttons that open a window with a list of
Chapter numbers (27 chapters). The user clicks a chapter command
button and access the another window with a list of all the sections
in that chapter (these sections are bookmarked in the chapter
documents). The user then clicks on a section button and that
information populates the corresponding textbox, which can then be
edited by the user and eventually populates the template form.
Because there are four textboxes, I had to repeat the code for each
different textbox. As you can imagine, this too long time. Below I
have put pound signs where the changing variables are located. The
code that pulls the bookmarked information into the template is the
same, except that the textboxes change each time. Right now I have
over a 100 userforms. This is causing my template size to get rather
large, and I am concerned for problems later. I am sure ther is an
easier way to do this, but since I am a newbie, I am not sure how.
I'd appreciate any help. One person suggested using Autotext, but I
am not sure how that fits into this.



Sub CommandButton1()


Dim i As Integer, source As Document
Set source = Documents.Open("C:\Documents and
Settings\Logue\My Documents\Allied
Title\exceptions\chapter#(chapters 1-27).doc")
For i = 1 To source.Bookmarks.Count


Userform3.TextBox*.Text = source.Bookmarks(# (this depends on how
many bookmarks are in that particular document)).Range

- This selects the first bookmark in chapter one. and
inserts the info in Textbox8.

Next i

Documents("C:\Documents and Settings\Logue\My
Documents\Allied Title\exceptions\Chapterone.doc").Close
SaveChanges = False

End Sub


-TIA,
Jason

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

Replies offlist may require payment.
 
J

Jason Logue

Steve,

Thank you much. Since I am a newbie, is it possible you could give me
an example of what you mean in the below statement? I think I may
have an idea (and I will play with it some), but I just want to know
if I am close.

Thanks,
Jason
 
T

Tony Strazzeri

Hi Jason

Try this to see if it helps.

Dim i As Integer, source As Document
Dim txtB As TextBox
Dim txtBname As String

Set source = Documents.Open("C:\Documents and Settings\Logue\My
Documents\Allied Title\exceptions\chapter#(chapters 1-27).doc")
For i = 1 To source.Bookmarks.Count
txtBname = TextBox & i
Set txtB = userform3.TextBox(txtBname)
txtB = source.Bookmarks(i)
' Userform3.TextBox*.Text = source.Bookmarks(# (this depends on
how many bookmarks are in that particular document)).Range

' - This selects the first bookmark in chapter one. and inserts
the info in Textbox8.

Next i

Cheers
TonyS
 
W

Word Heretic

G'day (e-mail address removed) (Jason Logue),

Public Sub FillMyBox(aCtrl as Control)

actrl.add "YEAH BABY!"

end sub


Public Sub Filler()
FillMyBox Box1
FillMyBox Box6
end sub

To keep it simple, I usually define aCtrl to be a specific control
type, like listbox, during development, and then change it to the
generic control holder when the code works, so its more flexible and
works with dropdowns etc as well.



(e-mail address removed) (Jason Logue) was spinning this yarn:
Steve,

Thank you much. Since I am a newbie, is it possible you could give me
an example of what you mean in the below statement? I think I may
have an idea (and I will play with it some), but I just want to know
if I am close.

Thanks,
Jason

Steve Hudson

Word Heretic, Sydney, Australia
Tricky stuff with Word or words for you.
wordheretic.com

Replies offlist may require payment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top