View Single Post
 
Old 08-24-2020, 08:25 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 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