Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-24-2020, 08:25 PM
gmayor's Avatar
gmayor gmayor is offline userForm input for filename Windows 10 userForm input for filename Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There is no need to use F12. As you have the information in your userform, you can simply use it to save the document. I also recommend testing to ensure that the bookmarks are present as they are easily deleted (or better still use content controls)
https://www.gmayor.com/insert_content_control_addin.htm will convert your bookmarks to content controls.




For bookmarks


Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim oRng As Range
Dim strPath As String

    If TextBox1.Text = "" Then
        MsgBox "Complete PTFN"
        TextBox1.SetFocus
        Exit Sub
    End If
    If TextBox2.Text = "" Then
        MsgBox "Complete PTLN"
        TextBox2.SetFocus
        Exit Sub
    End If
    If TextBox3.Text = "" Then
        MsgBox "Complete DRFN"
        TextBox3.SetFocus
        Exit Sub
    End If
    If TextBox4.Text = "" Then
        MsgBox "Complete DRLN"
        TextBox3.SetFocus
        Exit Sub
    End If


    If ActiveDocument.Bookmarks.Exists("PTFN") = True Then
        Set oRng = ActiveDocument.Bookmarks("PTFN").Range
        oRng.Text = TextBox1.Text
        ActiveDocument.Bookmarks.Add "PTFN", oRng
    End If

    If ActiveDocument.Bookmarks.Exists("PTLN") = True Then
        Set oRng = ActiveDocument.Bookmarks("PTLN").Range
        oRng.Text = TextBox2.Text
        ActiveDocument.Bookmarks.Add "PTLN", oRng
    End If

    If ActiveDocument.Bookmarks.Exists("DRFN") = True Then
        Set oRng = ActiveDocument.Bookmarks("DRFN").Range
        oRng.Text = TextBox3.Text
        ActiveDocument.Bookmarks.Add "DRFN", oRng
    End If

    If ActiveDocument.Bookmarks.Exists("DRLN") = True Then
        Set oRng = ActiveDocument.Bookmarks("DRLN").Range
        oRng.Text = TextBox4.Text
        ActiveDocument.Bookmarks.Add "DRLN", oRng
    End If

    strPath = Environ("USERPROFILE") & "\Documents\"
    ActiveDocument.SaveAs strPath & TextBox2.Text & TextBox1.Text & ".docx"

    Set oRng = Nothing
    Unload Me
End Sub
for content controls
Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim oCC As ContentControl
Dim strPath As String

    If TextBox1.Text = "" Then
        MsgBox "Complete PTFN"
        TextBox1.SetFocus
        Exit Sub
    End If
    If TextBox2.Text = "" Then
        MsgBox "Complete PTLN"
        TextBox2.SetFocus
        Exit Sub
    End If
    If TextBox3.Text = "" Then
        MsgBox "Complete DRFN"
        TextBox3.SetFocus
        Exit Sub
    End If
    If TextBox4.Text = "" Then
        MsgBox "Complete DRLN"
        TextBox3.SetFocus
        Exit Sub
    End If

    For Each oCC In ActiveDocument.ContentControls
        Select Case oCC.TITLE
            Case "PTFN": oCC.Range.Text = TextBox1.Text
            Case "PTLN": oCC.Range.Text = TextBox2.Text
            Case "DRFN": oCC.Range.Text = TextBox3.Text
            Case "DRLN": oCC.Range.Text = TextBox4.Text
        End Select
    Next oCC


    strPath = Environ("USERPROFILE") & "\Documents\"
    ActiveDocument.SaveAs strPath & TextBox2.Text & TextBox1.Text & ".docx"

    Set oCC = Nothing
    Unload Me
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



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 09:37 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