Word VBA Save As, Close Current Work and Open Saved Version Automatically

Tayl4n

Board Regular
Joined
Feb 17, 2021
Messages
84
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I hope that everything is going well with you!

I have a Word userform and it works clearly. After the work is finished, It can automatically save the pdf version in the same location with the word file.
After pressing the save button, I want it to save as word at the same time and I want it to close the current word document I am working with and open the new saved word file automatically. If you can help me, it will make my day.

Here is my pdf save codes. I couldn't edit this for word save.

VBA Code:
Dim strPath As String
    Dim strPDFname As String

    strPDFname = "Commercial Form " & Me.TextBox3.Value
    
    
    strPath = ActiveDocument.Path
    If strPath = "" Then    'doc is not saved yet
        strPath = Options.DefaultFilePath(wdDocumentsPath) & Application.PathSeparator
    Else
        'just add \ at the end
        strPath = strPath & Application.PathSeparator
    End If
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
                                       strPath & strPDFname & ".pdf", _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, _
                                       IncludeDocProps:=True, _
                                       CreateBookmarks:=wdExportCreateWordBookmarks, _
                                       BitmapMissingFonts:=True

UserForm1.Hide
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Note: It would be more useful if there was an option to open the new saved as file or not.

Like: Would you like to open the saved form?
Yes No button
 
Upvote 0
All your code does is save a PDF version of the Word file - it does not also save it as a Word document. Unless you save the modified Word document, all you'll have once you've closed it is the PDF. What you need is something like:
VBA Code:
Dim strPath As String, strName As String
strName = "Commercial Form " & Me.TextBox3.Value
With ActiveDocument
  strPath = .Path
  If strPath = "" Then strPath = Options.DefaultFilePath(wdDocumentsPath)   'doc is not saved yet
  strPath = strPath & Application.PathSeparator
  .SaveAs FileName:=strPath & strName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
  .SaveAs FileName:=strPath & strName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
End With
UserForm1.Hide
The saved Word document will now be open and active.
 
Upvote 0
All your code does is save a PDF version of the Word file - it does not also save it as a Word document. Unless you save the modified Word document, all you'll have once you've closed it is the PDF. What you need is something like:
VBA Code:
Dim strPath As String, strName As String
strName = "Commercial Form " & Me.TextBox3.Value
With ActiveDocument
  strPath = .Path
  If strPath = "" Then strPath = Options.DefaultFilePath(wdDocumentsPath)   'doc is not saved yet
  strPath = strPath & Application.PathSeparator
  .SaveAs FileName:=strPath & strName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
  .SaveAs FileName:=strPath & strName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
End With
UserForm1.Hide
The saved Word document will now be open and active.
Hey thank you so much! It works perfect.

Now they are saved as word and pdf in the same place and with the name I want. However, after pressing the Save button, the userform and the document should close not only userform. Then the newly saved word file should be opened, is this also possible?
 
Upvote 0
It seems from what you've described and the code you've posted that your 'original' document is just that, not a Word template. You should be using a Word template for this; then your userform would remain accessible to your newly-saved document. Under the current arrangement, the only way to end up with the 'original' document open is by adding code to re-open it. That's because once you've saved, the document is no longer the 'original' document. It would also be helpful if you could explain what you're trying to achieve by having both documents open.
 
Upvote 0
It seems from what you've described and the code you've posted that your 'original' document is just that, not a Word template. You should be using a Word template for this; then your userform would remain accessible to your newly-saved document. Under the current arrangement, the only way to end up with the 'original' document open is by adding code to re-open it. That's because once you've saved, the document is no longer the 'original' document. It would also be helpful if you could explain what you're trying to achieve by having both documents open.
I'm trying to understand so If I missed a point thank you for your patience in advance. In this case can we just close original document without saving after save as new verison ? Because this document will be used over and over again. therefore I do not want the original form to be saved and closed accidentally when the userform makes changes to the document.
 
Upvote 0
As soon as you start editing your 'original' document, it's no longer the 'original'. The SaveAs simply saves the edited document under a new name; you didn't close the 'original' - the current document simply ceased being the 'original'. As I said, you should be using a Word template as the basis for new documents instead of a 'template' document that risks being overwritten.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top