Saving an open word document to a specific file directory with user input for file name

fireman112473

New Member
Joined
May 9, 2017
Messages
8
I am trying to create a macro that will allow me to open a word file and immediately save it as a new file in a specific file directory. Below is how far I have gotten. I can save to a specific file directory with a predetermined name, but can't get it to allow for user input for new file name.

Sub IL09NFPA722010()
Documents.Open FileName:="S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.docx", ReadOnly:=True
ActiveDocument.SaveAs FileName:="S:\Employee Folders\Scott McBride\0 2017 Plan Review Letters" & "doc1", Fileformat:=wdFormatXMLDocument
End Sub

I want a user input for the area shown in bold.

Thanks
Scott
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I am trying to create a macro that will allow me to open a word file and immediately save it as a new file in a specific file directory. Below is how far I have gotten. I can save to a specific file directory with a predetermined name, but can't get it to allow for user input for new file name.

Sub IL09NFPA722010()
Documents.Open FileName:="S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.docx", ReadOnly:=True
ActiveDocument.SaveAs FileName:="S:\Employee Folders\Scott McBride\0 2017 Plan Review Letters" & "doc1", Fileformat:=wdFormatXMLDocument
End Sub

I want a user input for the area shown in bold.

Thanks
Scott

Can you just user an input box to let the user assign that value?

Rich (BB code):
Sub IL09NFPA722010()

dim UserFileName as String

UserFileName = Application.Inputbox "Please type in in the File Name"

Documents.Open FileName:="S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.docx", ReadOnly:=True
ActiveDocument.SaveAs FileName:="S:\Employee Folders\Scott McBride\0 2017 Plan Review Letters" & UserFileName, Fileformat:=wdFormatXMLDocument

End Sub

That would allow them to type in the value to use? (This is untested code)
 
Last edited:
Upvote 0
You need to supply the extension also, thus:
Code:
Sub IL09NFPA722010()

dim UserFileName as String

UserFileName = Application.Inputbox "FileName"

Documents.Open FileName:="S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.docx", ReadOnly:=True
ActiveDocument.SaveAs2 FileName:="S:\Employee Folders\Scott McBride\0 2017 Plan Review Letters" & UserFileName & ".docx", Fileformat:=wdFormatXMLDocument

End Sub
Note that I've also used the SaveAs2 Method instead of SaveAs.

You may (or may not) want to add 'AddToRecentFiles:=False' to both the Open and SaveAs2 methods.

Frankly, though, if all you're doing is copying a document from one folder to another and renaming the copy, I don't see any reason for involving Word at all in that process.
 
Last edited:
Upvote 0
getting an error at "dim UserFileName as String"
I don't know why you would get an error when defining your variable. I did have problems when I tried to use the code, but it was because I couldn't get the Documents.Open to work as it was. I tweaked the code and got this to work for me. Perhaps you can modify from here?

Code:
Sub IL09NFPA722010()

Dim UserFileName As String
Dim WordObject As Object, DocObject As Object


    Set WordObject = CreateObject("Word.Application")
    WordObject.Visible = True


    UserFileName = Application.InputBox("Please type in the File Name")


    Set DocObject = WordObject.Documents.Open("G:\Testing.docx", vbReadOnly = True)


    DocObject.SaveAs2 Filename:="G:\" & UserFileName & ".docx"


ThisWorkbook.Activate


WordObject.Quit
Set WordObject = Nothing


End Sub
 
Last edited:
Upvote 0
One important point to note about your revised code is that assumes Word is being automated (i.e. run from a different application). Regardless, since all that our first replies did was to tweak fireman's own code, it's hard to see how defining a variable within that code could possibly introduce an error.
 
Upvote 0
Delfinus25

Now I am getting an error on the .Activate. I tried substituting ThisDocument for ThisWorkbook, but to my dismay it did no work. Otherwise, it is working perfectly.

Thank you
Scott

 
Upvote 0

Forum statistics

Threads
1,223,793
Messages
6,174,635
Members
452,575
Latest member
Fstick546

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