PDA

View Full Version : Macro Save as active document name in new directory



fiddystorms
02-03-2012, 04:55 PM
This might be the easiest VBA questions there is, but I spent hours looking for a solution and can't get an answer. :banghead:

I simply need to save multiple documents using the active document full name in a new directory.

so, I open S:\Terr1-Greg\MI 2011\acs#23100 and I save it as S:\Terr1-Greg\MI 2012\acs#23100

then I need to open S:\Terr1-Greg\MI 2011\acs#23101 and save it as S:\Terr1-Greg\MI 2012\acs#23101

The macro I recorded just keeps saving over the original (acs#23100) in the new 2012 directory. I need that acs#XXXXX to be dynamic, not stagnant.

I tried this as a solution with no luck (yeah, you're right... I have 0 VBA experience... how did you know?)

Sub save_as()
'
' save_as Macro
'
'
ChangeFileOpenDirectory "S:\Terr1-Greg\MI 2012\"
ActiveDocument.SaveAs2 FileName:="S:\Terr1-Greg\MI 2012\" & ActiveDocument.FullName,
FileFormat:=wdFormatDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=0
End Sub

fumei
02-03-2012, 08:29 PM
Where do you have the code?

fumei
02-03-2012, 08:29 PM
Oh...and why even use a macro? Why not just do a SaveAs and save your new files into the 2012 folder?

fiddystorms
02-04-2012, 09:58 AM
Oh...and why even use a macro? Why not just do a SaveAs and save your new files into the 2012 folder?


The Code is in its Word 2010 default location after recording a macro. It's in the normal.dot template.

I decided to create the macro because I have a couple hundred business quotes to update from 2011 to 2012. They will all be saved in the new 2012 folder and eventually receive new quote #'s. I have a macro that is successfully changing the date and updating the letterhead to our new letterhead.

If there is a SaveAs command I could write into the first macro I would consider that as well. I just wanted the ability to review the changes made to the 2011 quotes because we do not use forms and not every quote can successfully be updated through the first macro.

Also, I am intensely interested in learning VBA and thought this would be an easy challenge to use as an example.

fumei
02-04-2012, 02:33 PM
"They will all be saved in the new 2012 folder and eventually receive new quote #'s."

Oh? Eventually? Would this nt be part of the saving process?

In any case try:Sub To2012()
Dim NewPath As String
NewPath = "S:\Terr1-Greg\MI 2012\"
ActiveDocument.SaveAs Filename:=NewPath & ActiveDocument.Name
End Sub


I avoid using ChangeFileOpenDirectory (in fact I never use it). Especially when you are actually SAVING files, not opening them.

The above will save as you describe.

fiddystorms
02-04-2012, 09:32 PM
Thank you very much, it definitely works. Yes, eventually. I haven't been given all of the corresponding quote #s yet. that will have to be done separately.

A couple more questions if you don't mind. I expanded greatly on my macro to make a few changes and including saving it in the 2012 directory.

first, back to my first issues, what is all of this for:

FileFormat:=wdFormatDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=0

I do have another question that I am still working on. I want to ask a comprehensive question & its not ready yet.

Thanks again, VBA is pretty cool and I am grateful that people like you are around to help people like me.

fumei
02-04-2012, 11:14 PM
FileFormat:=wdFormatDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=0


These ae DEFAULT parameters. Many commands/instructions/objects have them. If you do not need them, do not use them. Default parameters generally appear when you record code. Programmers only use parameters when they are needed. For example, if you saving as a normal Word document file, there is no need to use FileFormat:=wdFormatDocument. It is the default. If you do not use it, the parameter will BE wdFormatDocument.

BTW: the F1 key is your friend. For example put your cursor on .SaveAs in your code, and press F1. You get the Help listing, and describing, all available parameters.

"I haven't been given all of the corresponding quote #s yet. that will have to be done separately."

This seems odd. Are these going to be part of the filename? If so, should they not be part of the original process? If not, of what relevance is this?

fiddystorms
02-05-2012, 03:50 PM
"I haven't been given all of the corresponding quote #s yet. that will have to be done separately."

This seems odd. Are these going to be part of the filename? If so, should they not be part of the original process? If not, of what relevance is this?

great, I did not know that about the F1 key, I will utilize it from here on. regarding the quote #s, you are absolutely correct. I was just anxious to learn and I jumped the gun a little. It seems daunting to me to have to have this macro reference an excel spreadsheet to pull the quote # from (which is what I assume would have to happen).

I have struggled for 2 days now just automating last years quote to copy over to the new company letterhead with some minor corrections and a new format, I can't imagine tackling that aspect of it on top of what I've gone through already.

I am struggling right now with making things dynamic after recording a macro. everything wants to default to a static document name. The following is an example that has caused me quite a bit of trouble figuring out. I think this is the root cause of all of my headaches. I get an error and I just delete the line, but the new letterhead is never part of the end result.

Windows("acs#22128.doc [Compatibility Mode]").Activate

to put it into a little more context, the macro I created copies the existing text of the 2011 quote and pastes it onto the new letterhead. Its bizarre, I've tried several different methods of getting the old quotes onto the new letterhead but they don't seem to want to cooperate. in the end, I still have the old letterhead which was not even done using a header. I incorporated a header into my letterhead. I can't imagine that's the problem.

Selection.Find.Execute
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Copy
Documents.Add Template:= _
"C:\Users\Greg\AppData\Roaming\Microsoft\Templates\ACS Letterhead New.dot.dotm" _
, NewTemplate:=False, DocumentType:=0
Selection.TypeParagraph
Windows("acs#22128.doc [Compatibility Mode]").Activate
ActiveWindow.Close

fiddystorms
02-05-2012, 09:25 PM
Actually, I figured it out. I had to convert the original document first. (For the record, the conversion button is found under File>Info)

Now, my only hangup is renaming the document before it saves. for the time being, I just want it to retain the 2011 file name. I'll F1 this one to death.

secondary problem I just realized, it saves as a .docx file. most everyone who will receive these quotes will not be able to open them as .docx. any suggestion on code to convert it back to .doc?

I found another little mistake that relates to the compatibility issue I was having. I copied your code above to make the file name being saved dynamic rather than static. somewhere along the way I started using the SaveAs2 command. this apparently has a significant effect on the macro.

fiddystorms
02-07-2012, 06:28 AM
Okay, I know what I want to do, I just can't find the correct keywords to search for it. If someone can help point me in the right direction, I would sincerely appreciate it.

i am going to direct my macro to create a form field where currently there exists only text. i want this form field to contain the information for which the file will be saved as. it needs to be dynanic since it will never be the same twice.

fumei
02-07-2012, 12:36 PM
I am not sure this is a good idea. What do you mewan by the formfield being dynamic???

fiddystorms
02-07-2012, 02:47 PM
Dynamic; I need it to save a new filename each time as opposed to static which would be the same filename each time. Every macro I do seems to want to save the filename as the original and just overwrites the file that was previously saved. I need it to save as a new filename each time and I would like the saveas command to reference the quote # that is on the existing quote. right now it is just part of the text.

My thought is that I have to create a text field where this quote # will appear and then reference that field in the saveas code. does that make sense?

fumei
02-07-2012, 09:53 PM
The VALUE of a variable has nothing whatsoever to do with the formfield. There is no such thing as a dynamic formfield. A formfield is simply a content container. The value it holds is ALWAYS "dynamic".

It - the formfield - does not save anything. Your code does.

I am not sure what is happening with the compatibility issue you mention. I do not have anything like that.

I have no idea what you are doing with the ActiveWindow.Close line. What is this doing?

Please post your entire code, ALL of it.


I fail to see any requirementy for a formfield.