Re: Replaces files
From: Jay Freedman (jay.freedman_at_verizon.net)
Date: 08/16/04
- Next message: Peter Hewett: "Re: Stop macro running"
- Previous message: JB: "Re: Stop macro running"
- In reply to: Richstein: "Re: Replaces files"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 16 Aug 2004 10:45:41 -0400
I'm afraid you're missing the entire point of the exercise. Think in words
first, then in code:
- Ask the user for a name to use.
- Test whether a file already exists with that name.
- If it exists, ask whether to replace. Otherwise, just save the file with
that name.
The "ask for a name" part corresponds to the InputBox statement. This should
come first.
The "test whether a file exists" part corresponds to the If
Len(Dir(fileName)) > 0 statement. The "ask whether to replace" part
corresponds to a MsgBox statement that you should have in the Then part of
the If statement (which you don't do), while the "just save the file" part
corresponds to an Else part of the If statement (which is also missing from
your macro).
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Richstein wrote: > I tried the first suggestion, it still does not prompt me > if the file exists. What am I missing? Thanks for the > help! > > Dim fileName As String > If Len(Dir(fileName)) > 0 Then > > fileName = InputBox("Enter File Name") > > ActiveDocument.SaveAs "F:\class\" & fileName > > End If > > > >> -----Original Message----- >> Two approaches you can take. Either test whether the file exists: >> >> if len(dir(fileName)) > 0 then >> .. file exists >> end if >> >> Or, since you're prompting for the filename anyway, use the >> CommonDialog control instead of the inputbox (which is a much better >> way to get a filename anyway). Add a form, and put a CommonDialog >> control on it. You don't need to display the form. IN your macro, >> use code like >> >> With new MyForm.CommonDialog1 >> .InitDir = .... 'Folder to save in >> .Flags = cdlOFNOverwritePrompt (+ cdlOFNNoChangeDir if >> users MUST save in this folder) >> >> etc ... Check Help for the full set of arguments >> >> End with >> >> >> >> "Richstein" <anonymous@discussions.microsoft.com> wrote in message >> news:65f301c4834d$decdb480$a301280a@phx.gbl... >>> I have the following code written to save files in a >>> certain location. However, if the file already exists, no >>> prompt appears asking if I want to replace. Is there a >>> way to do this? Thank you. >>> >>> Sub Saveit() >>> Dim fileName As String >>> >>> fileName = InputBox("Enter File Name") >>> >>> ActiveDocument.SaveAs "F:\class\" & fileName >>> >>> >>> >>> End Sub >>> >> >> >> .
- Next message: Peter Hewett: "Re: Stop macro running"
- Previous message: JB: "Re: Stop macro running"
- In reply to: Richstein: "Re: Replaces files"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|