![]() |
#4
|
||||
|
||||
![]()
I usually try to modularise the code to make it easier to maintain the userforms and add/remove elements on the userform without having to update the code as well. The following code assumes you have populated the Tag property of each textbox on your userform and then you can use that tag value to find the relevant bookmark or content control that aligns with that textbox.
Code:
Private Sub CommandButton1_Click() Dim aCtrl As Control, sTag As String, strPath As String, aRng As Range For Each aCtrl In Me.Controls Select Case TypeName(aCtrl) Case "TextBox" sTag = aCtrl.Tag 'aCtrl.Name 'if you prefer If ActiveDocument.Bookmarks.Exists(sTag) Then Set aRng = ActiveDocument.Bookmarks(sTag).Range aRng.Text = aCtrl ActiveDocument.Bookmarks.Add Name:=sTag, Range:=aRng ElseIf ActiveDocument.SelectContentControlsByTitle(sTag).Count > 0 Then ActiveDocument.SelectContentControlsByTitle(sTag)(1).Range.Text = aCtrl End If End Select Next aCtrl strPath = Environ("USERPROFILE") & "\Documents\" ActiveDocument.SaveAs strPath & Me.TextBox2 & Me.TextBox1 & ".docx" Me.Hide End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
filename field not displaying correct filename when that name starts with # | plrsmith | Word | 1 | 07-06-2018 03:10 AM |
![]() |
UnlimitedPower | Word VBA | 1 | 08-19-2016 12:22 AM |
Userform calls other userform, then populate worksheet | Lehoi | Excel Programming | 0 | 02-03-2016 02:58 PM |
VBA Code in a UserForm module to delete a Command Button which opens the userform | Simoninparis | Word VBA | 2 | 09-21-2014 03:50 AM |
![]() |
BoringDavid | Word VBA | 5 | 05-09-2014 09:08 AM |