PDA

View Full Version : save as



northernstar
08-16-2007, 03:39 PM
hi everyone

i am new to this site

i have used vba in excel for sometime now, but i am now moving onto word but i have a little problem

i would like to save a document to a particular folder, either naming it using some text from the document or for the user to enter the file name

any help would be much appreciated

thanks

vksingh24
08-16-2007, 11:31 PM
To Save You file using SaveAs dialg box use the following code in your marcor

Application.Dialogs(wdDialogFileSaveAs).Show

or You can save it by code. The file will be saved in the Apllication directory

ActiveDocument.SaveAs "test.doc"

mdmackillop
08-17-2007, 12:14 AM
A simple solution; select the document text, then run the macro. Note that you need to avoid illegal characters.
Sub Dosave()
ActiveDocument.SaveAs InputBox("Enter save name", "Saving File", Trim(Selection))
End Sub

northernstar
08-19-2007, 11:47 AM
thanks for that but how do i change the path of where the document is saved?

northernstar
08-19-2007, 12:09 PM
for example i would like to save a document using two macros to the following folders

c:\Mechanical\Certificates\UKAS Certificates

c:\Mechanical\Certificates\In House Certificates

thanks

vksingh24
08-19-2007, 09:06 PM
You can save it like

ActiveDocument.SaveAs "c:\Mechanical\Certificates\UKAS Certificates\test.doc"

ActiveDocument.SaveAs "c:\Mechanical\Certificates\In House Certificates\test.doc"

northernstar
08-19-2007, 10:38 PM
thanks for that but that doesnt bring up the save as dialogue box

i need it so that the user can save the document with a diiferent name each time

any more ideas would be appreciated

thanks

geekgirlau
08-19-2007, 10:44 PM
With Application
.ChangeFileOpenDirectory "c:\Mechanical\Certificates\UKAS Certificates\"
.Dialogs(wdDialogFileSaveAs).Show
End With

northernstar
08-19-2007, 10:49 PM
thanks for that, will try it

think i may have already tried something very similar

will let you know how i get on

thanks again

northernstar
08-19-2007, 11:08 PM
just tried that pieced of code and it doesnt seem to want to change the save as dialogue box, it still just comes up with where it was opened from or where it was last saved

any other ideas would be much appreciated

thanks again

northernstar
08-20-2007, 01:12 PM
cant beleive that this can be such a major stumbling block

please someone help me with this save as problem

thanks!

fumei
08-22-2007, 10:56 AM
Excuse me...that sure looks like shouting to me. That is not nice.

ChangeFileOpenDirectory will only affect the SaveAs location if SaveAs is used directly, NOT as a dialog..ChangeFileOpenDirectory _
"c:\Mechanical\Certificates\UKAS Certificates\"
ActiveDocument.SaveAs Filename:="blah.doc"That will save the active document as:

c:\Mechanical\Certificates\UKAS Certificates\blah.doc

You do not write very good posts.
for example i would like to save a document using two macros to the following foldersTWO macros???? Hmmm. So completely independent macros? Strange. Well, if they are TWO macros, then really one is just using a different path than the other. Strange...but sure...why not.

Further:
either naming it using some text from the document or for the user to enter the file nameWhich?

It would help if you stated precisely your requirements.Const Path1 As String = "c:\Mechanical\Certificates\UKAS Certificates\"
Const Path2 As String = "c:\Mechanical\Certificates\In House Certificates\"

' Note the \ at the end

Dim strName As String
strName = Inputbox("Please enter new name of file.")
ActiveDocument.SaveAs Filename:=Path1 & strName
ActiveDocument.SaveAs Filename:=Path2 & strNamewould take the user entered name and save it twice, once to Path1, then to Path2.

If you want to use text from the document, simply make strName that text.

Again, I know things can get frustrating, but please...do not shout. I for one will ignore it.

If the above does not do what you want, then please state clearly and precisely what you DO want to happen.

fumei
08-22-2007, 10:57 AM
How the heck did that happen?????

northernstar
08-22-2007, 10:37 PM
hi

sorry if people thought i was shouting in the last reply i made that was not the case, i was trying out different fonts and clicked on bold by mistake and forgot to unclick it.....sorry again

thanks for your reply, i will give it a whirl

the reason on the two macros is that depending on what type of document is being used it will be saved to one path or the other, i dont need it to save it to both paths

i hope that clears it up a little and in future i will try to make things clearer

sure the code you have wrote will do the trick

many thanks again

northernstar
08-22-2007, 10:53 PM
that has done the trick

thank you very much

you dont know how pleased i am to have sorted out this problem, really doing my head in

thanks again!!!

northernstar
08-22-2007, 10:59 PM
hi

sorry to be a pain

the code works great, but now i have the problem that if the file name already exists then it just over writes it with the the new file, which of course is not what i need

so just wondering if anyone can point me in the right direction for the code that checks to see if the file name already exists?

i hope this is clear to everyone, any questions please let me know

many thanks

mdmackillop
08-23-2007, 09:41 AM
If len(Dir(Path2 & strName ))>0 then
msgbox "File Exists"
end if

northernstar
08-23-2007, 11:25 AM
thanks for that

that would tell me if the file exists what about over writing it if necessary

really i suppose what i would like is the standard window dialogue box to come up when a file already exists, is this possible

thanks again

fumei
08-23-2007, 12:00 PM
I really think you need to think about your requirements. You do not need a "standard window dialogue". Why?

SaveAs over writes files. Period.

So test - with Malcom's code - on the user input name. If the name exists as a file, then loop back and ask the user again. Give them the choice to either use the name (and overwrite), OR enter a new name.

Why do you want a windows dialog? You know what folder you are saving to. What are your requirements? What, exactly, do you need to know? You need to know whether to use the name (and overwrite) or get another name. So....do that. Use a messagebox (with Yes/No) stating the file exists. Tell the user to press Yes to overwrite, No to input another name. They press Yes...YOU continue the code and do a SaveAs. They press No...give them the file name input again.

This is not difficult, and I am NOT going to post the code for it.

northernstar
08-23-2007, 02:51 PM
thanks for your help and comments

i am very new to writing code in word vba and finding it quite different to Excel (which is what i am used to)

i will try the the looping i am assuming it is r=the same or very similar to an Excel loop

thanks again

northernstar
08-23-2007, 03:56 PM
i have tried doin the loop but it is late and i am tired just wondering if you could point me in the right direction

i can do the msgbox with the yes and no button and the code to save if yes and the code for no

but i am struggling on how to find out if the file name already exists
some clues would be very much appreciated

thanks very much

geekgirlau
08-23-2007, 04:44 PM
Putting together Gerry and Malcolm's code:



Const Path1 As String = "c:\Mechanical\Certificates\UKAS Certificates\"


Const Path2 As String = "c:\Mechanical\Certificates\In House Certificates\"


' Note the \ at the end


Dim strName As String
PromptForName:
strName = InputBox("Please enter new name of file.")


If Len(Dir(Path1 & strName)) > 0 Or Len(Dir(Path2 & strName)) > 0 Then
GoTo PromptForName
Else
ActiveDocument.SaveAs Filename:=Path1 & strName
ActiveDocument.SaveAs Filename:=Path2 & strName
End If

northernstar
08-23-2007, 10:49 PM
thanks for that

but unfortuately it doesnt appear to work

it all works except for the fact it doesnt detect if the file name already exists

dont understand the code

if Len(Dir(Path1 & strName))>0

not sure how this works

also not sure how the code would work if i do want to over write a file that already exists, think i really need a loop of some description

any ideas

thanks again

geekgirlau
08-23-2007, 11:16 PM
Const Path1 As String = "c:\Mechanical\Certificates\UKAS Certificates\"
Const Path2 As String = "c:\Mechanical\Certificates\In House Certificates\"
' Note the \ at the end

Dim strName As String
PromptForName:
strName = InputBox("Please enter new name of file.")

If Len(Dir(Path1 & strName)) > 0 Or Len(Dir(Path2 & strName)) > 0 Then
If vbNo = MsgBox("The file already exists - do you want to overwrite?", _
vbQuestion + vbYesNo + vbDefaultButton2, "Existing File") Then
GoTo PromptForName
Else
ActiveDocument.SaveAs Filename:=Path1 & strName
ActiveDocument.SaveAs Filename:=Path2 & strName
End If

Else
ActiveDocument.SaveAs Filename:=Path1 & strName
ActiveDocument.SaveAs Filename:=Path2 & strName
End If


Dir checks to see whether a file or folder exists, so if the length of Dir is greater than 0, it has found a file matching that path and name.

northernstar
08-24-2007, 07:53 AM
hi

i have now tried the new piece of code and it still doesnt recognise that the file already exists

not sure where to go from here

all the rest of the code works fine but if you use the same file name it just seems to overwrite it

any ideas?

thanks

fumei
08-24-2007, 11:15 AM
Sorry, but it does work exactly as stated. Geekgirlau's code is slightly different from what I did (which is the nature of VBA), but it works the same way. And work it does.

If the given (input) name already exists as a file, it displays the message. If you press Yes, it overwrites the existing file. If you press No, if re-displays the inputbox.

If it is not working for you, then YOU are doing something wrong.

Are you typing the name of the existing file exactly the same?

IS there an existing file?

Again, if it is not working properly for you, then you are doing something wrong. The code works perfectly.

Why not post EXACTLY have you have in your module? Or try this test.

1. Run the code. Enter "yadda.doc".
2. Run the code again. Enter "yadda.doc"

You should get the existing file message. If you don't, then you put the code in wrong.

northernstar
08-24-2007, 02:59 PM
ok i have tried putting in the code again on a different pc and i agree it works to a fashion

if i enter the file name as "test" it saves it ok, then if i try saving another document as "test" then it just overwrites it but if the second time i put in "test.doc" the file already exists message box does trip in

i would like it to work by putting in "test" both times, is this possible?

the document will always be saved in the same place as a .doc file

i know you must think i am a pain but i am very new to this and have come on this site to get help as i really not sure what i am doing in word vba, i get by in excel vba as i know the commands etc for what i need to do

many thanks

northernstar
08-24-2007, 03:01 PM
i am guessing i need somehow just to ignore the .doc in my coding?

mdmackillop
08-25-2007, 02:17 AM
By checking if a file exists using DIR, you are comparing two strings. It follows that they must be identical. Make use of MsgBox in debugging your code to see what your values are. If you need to add/remove ".doc" this is is easily done. Can you post the code as you now have it?

northernstar
08-25-2007, 07:13 AM
please find the code below

Sub test()
Const Path1 As String = "C:\Documents and Settings\Dave\My Documents\David Howell\"


Dim strName As String
PromptForName:
strName = InputBox("Please enter new name of file.")

If Len(Dir(Path1 & strName)) > 0 Then
If vbNo = MsgBox("The file already exists - do you want to overwrite?", _
vbQuestion + vbYesNo + vbDefaultButton2, "Existing File") Then
GoTo PromptForName
Else
ActiveDocument.SaveAs FileName:=Path1 & strName

End If

Else
ActiveDocument.SaveAs FileName:=Path1 & strName

End If

End Sub

thanks

mdmackillop
08-25-2007, 09:03 AM
Sub test()
Const Path1 As String = "C:\Documents and Settings\Dave\My Documents\David Howell\"

Dim strName As String
PromptForName:
strName = InputBox("Please enter new name of file.")

If UCase(Right(strName, 4)) <> ".DOC" Then
strName = strName & ".doc"
End If

If Len(Dir(Path1 & strName)) > 0 Then
If vbNo = MsgBox("The file already exists - do you want to overwrite?", _
vbQuestion + vbYesNo + vbDefaultButton2, "Existing File") Then
GoTo PromptForName
Else
ActiveDocument.SaveAs FileName:=Path1 & strName
End If
Else
ActiveDocument.SaveAs FileName:=Path1 & strName
End If
End Sub

northernstar
08-25-2007, 05:25 PM
thanks for that

i will give this a try very soon and i will let you know how i get on with this

thanks again

fumei
08-26-2007, 10:22 PM
Ummm, that is exactly why. It helps here if you state - precisely, exactly, accurately - what you are doing.

You never mentioned anything, ever, about not wanting to use ".doc".

If you do not explicitly tell Word to do so, it appends .doc to a given name when it saves it.

You enter: "test" and save, Word saves it as "test.doc".

So if you type "test" again....is there a "test"? No, there is not.

Malcom's code should fix it for you, because yes, if you want to do this without using .doc - which you never stated was a requirement - ...you have to deal with the .doc.

Please note Malcolm's code for future use.

Notice in particular this line:If UCase(Right(strName, 4)) <> ".DOC" Then It will help you to understand why he did this.

UCase makes the given string uppercase. It makes a comparison to .DOC. Why? Because what if the file was:

.Doc
.doC
.DOc
.dOC

It happens. His code takes the given string and makes it uppercase (so not matter what the testing string will be .DOC), THEN compares it.

So any file, no matter how the .doc is formatted, will be correctly tested.

northernstar
08-27-2007, 02:38 PM
thanks for that

i will give that go

thanks again