Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 08-24-2020, 10:08 PM
Guessed's Avatar
Guessed Guessed is offline userForm input for filename Windows 10 userForm input for filename Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
 



Similar Threads
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
userForm input for filename Filename 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
userForm input for filename Is it possible to take an input from a UserForm in one document to a UserForm in a do BoringDavid Word VBA 5 05-09-2014 09:08 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:45 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft