![]() |
|
#3
|
||||
|
||||
|
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
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 |
|
|
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 |
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 |
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 |