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



## fireman112473 (May 9, 2017)

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


----------



## Delfinus25 (May 9, 2017)

fireman112473 said:


> 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
> ...



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


```
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)


----------



## Macropod (May 9, 2017)

You need to supply the extension also, thus:

```
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.


----------



## fireman112473 (May 10, 2017)

Getting an error at "dim UserFileName as String"


----------



## fireman112473 (May 10, 2017)

getting an error at "dim UserFileName as String"


----------



## Delfinus25 (May 10, 2017)

fireman112473 said:


> 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?


```
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
```


----------



## Macropod (May 10, 2017)

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.


----------



## fireman112473 (May 11, 2017)

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


----------



## Delfinus25 (May 11, 2017)

Is this code in an Excel module?


----------



## Macropod (May 11, 2017)

Delfinus25 said:


> Is this code in an Excel module?


Valid question, indeed, given that neither the thread title 'Saving an open word document' nor anything fireman's posted so far indicates anything other than Word is involved.


----------



## fireman112473 (May 9, 2017)

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


----------



## fireman112473 (May 12, 2017)

Delfinus25 said:


> Is this code in an Excel module?


No I am doing this in word. Our process is that we open a letter template from the server. Then saveas with a new name in a differenr folder on the server and the customize it for the purposes at hand.

I want to open word choose my template, saving the file with a new name in an existing folder, and saved file remaining open to customize accordingly. There are many people accessing these templates and trying to remove the temptation to do a save vs saveas.

Cant get the new file to remain active. It saves it, but the I have to manually open it.

Getting closer.


----------



## Delfinus25 (May 12, 2017)

Well, as this forum is called MrExcel - I don't know how they feel about Word questions - but I'm sure the mods will answer that one.

Regardless, it would have been helpful to clarify that from the beginning.   I made the assumption it was all for Excel, accessing Word from outside.

Unfortunately, I don't do much coding in Word, so I'm not sure I can be of further help.


----------



## Macropod (May 12, 2017)

Delfinus25 said:


> Well, as this forum is called MrExcel - I don't know how they feel about Word questions - but I'm sure the mods will answer that one.


A nanosecond looking at the discussions in this forum would answer that - positively.


----------



## Macropod (May 12, 2017)

fireman112473 said:


> Our process is that we open a letter template from the server. Then saveas with a new name in a differenr folder on the server and the customize it for the purposes at hand.
> 
> I want to open word choose my template, saving the file with a new name in an existing folder, and saved file remaining open to customize accordingly. There are many people accessing these templates and trying to remove the temptation to do a save vs saveas.


The problem, though, is that you're not actually using a template. What your code shows you're actually doing is opening an existing Word document, which you're then trying to save with a new name. The file:
"S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.docx"
is a _Document_, not a _Template_, and you're opening it using Documents.Open. If you were using a _Template_, it would have a .dotx or .dotm extension and, instead of using Documents.Open, you'd use Documents.Add. For example:
Documents.Add "S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.dotx"
When you create a new document from a _Template_, there is negligible risk of it being overwritten by the document created from it. Indeed, if you were using a _Template_, there wouldn't be any need for a macro such as the one you're trying to use to protect it. Try saving your 'IL Alarm PR Letter IBC 2009 NFPA 2010' as a _Template_, via File|Save As, then close Word (or at least the template) and double-click on the template to see what happens.


----------



## Delfinus25 (May 12, 2017)

Macropod said:


> A nanosecond looking at the discussions in this forum would answer that - positively.


Being text, I can't tell if this is snark or not, and I honestly can't even tell which way you're answering.


While I'm not a terribly active member, I've been here a while, and I can't say I have ever come across any mention of Word while poking through these questions, so I truly had / have no idea how it is received here.


----------



## Macropod (May 13, 2017)

Well, the thread currently adjacent to this one is titled 'Automatically saving a text file (created with a macro from csv) as Word.doc silently' and simply scrolling down the thread list for this forum shows plenty of others referencing Word in their titles, too.


----------



## fireman112473 (May 15, 2017)

Macropod said:


> The problem, though, is that you're not actually using a template. What your code shows you're actually doing is opening an existing Word document, which you're then trying to save with a new name. The file:
> "S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.docx"
> is a _Document_, not a _Template_, and you're opening it using Documents.Open. If you were using a _Template_, it would have a .dotx or .dotm extension and, instead of using Documents.Open, you'd use Documents.Add. For example:
> Documents.Add "S:\Data\FORMS\Master Plan Review Letters\2009 IBC and MBC Forms\Illinois\IL Alarm PR Letter IBC 2009 NFPA 2010.dotx"
> When you create a new document from a _Template_, there is negligible risk of it being overwritten by the document created from it. Indeed, if you were using a _Template_, there wouldn't be any need for a macro such as the one you're trying to use to protect it. Try saving your 'IL Alarm PR Letter IBC 2009 NFPA 2010' as a _Template_, via File|Save As, then close Word (or at least the template) and double-click on the template to see what happens.



I misspoke. I am not using a template either.  It is a word document that we are opening. Multiple users are accessing these documents. everything is happening that I need it to do from I just need to be able from *Delfinus25's *assistance except for keeping the file open for further editing. It is getting caught up at the .activate.

Any ideas of how to resolve?

Thank you.


----------



## fireman112473 (May 15, 2017)

It is getting caught up at the ThisWorkbook.activate. I tried substituting ThisDocument.Activate


----------



## Delfinus25 (May 15, 2017)

You want to keep the newly saved document open?

What about just REOPENING it after saving?  Might that be a suitable workaround?
(Once again, I don't really do Word VBA, I have no idea how much it differs)


----------



## Macropod (May 15, 2017)

fireman112473 said:


> I misspoke. I am not using a template either.  It is a word document that we are opening. Multiple users are accessing these documents. everything is happening that I need it to do from I just need to be able from *Delfinus25's *assistance except for keeping the file open for further editing. It is getting caught up at the .activate.
> 
> Any ideas of how to resolve?


Your reply quoted the optimal solution. You might at least try it.


----------



## fireman112473 (May 9, 2017)

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


----------



## Macropod (May 15, 2017)

fireman112473 said:


> It is getting caught up at the ThisWorkbook.activate. I tried substituting ThisDocument.Activate


The 'solution' in post #6 is a complete misdirection. Ignore it.


----------



## fireman112473 (May 15, 2017)

I was able to figure out how to get it to work. I used an document.open. I will post later the week the exact code I used.


----------



## Joe4 (May 16, 2017)

> Well, as this forum is called MrExcel - I don't know how they feel about Word questions - but I'm sure the mods will answer that one.


Just to clarify any confusion - we have no problem with people posting Word questions.  They should be posted in the "*General Excel Discussion & Other Questions*" forum (not in the "*Excel Questions*" forum), and you will want to be sure to include that it is a Word question in the Title or original question, to remove any doubt.

Paul is a Word MVP, and is one of of most knowledgeable posters on Word questions.


----------



## Delfinus25 (May 16, 2017)

Joe4 said:


> Just to clarify any confusion - we have no problem with people posting Word questions.  They should be posted in the "*General Excel Discussion & Other Questions*" forum (not in the "*Excel Questions*" forum), and you will want to be sure to include that it is a Word question in the Title or original question, to remove any doubt.
> 
> Paul is a Word MVP, and is one of of most knowledgeable posters on Word questions.



Thank you for the clarification.   I'll admit, it was my mistake not realizing where the thread was posted.   I generally poke through the "zero reply posts" and it slipped my mind that it could come from other portions of the forum, as I generally only participate in the "Excel Questions" section.
When I read the question (and title), it just didn't dawn on me that the user was not using Excel and instead needed the code in Word itself.  Next time I will look closely to see where it was posted.

Thanks again.


----------

