Re: Replaces files

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jay Freedman (jay.freedman_at_verizon.net)
Date: 08/16/04


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


Relevant Pages

  • Re: Beginners Linux Questions
    ... when I've typed in a line and would like to erase it ... > prompt) it will close down that terminal. ... > except that it inserts into the input the matching filename found. ... > more than one filename matches the given characters, ...
    (alt.os.linux)
  • Re: File Transfer
    ... although, of course, it will prompt for user name and password, if it ... filename as the source. ... would like to tranfer file/data from the device to a PC on network. ... copy command it works fine. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: SMS Collection question
    ... was having a hard time getting it to work right. ... AAROn ... > FILENAME to the filename you are missing: ... >> yet one of the executables is missing. ...
    (microsoft.public.sms.admin)
  • RE: TransferSpreadsheet saving file
    ... Yes, you are correct, I don't need to prompt for the file name, but I do need ... I tried using .xls in the filename but that didn't work. ... and cannot find anything that is in capitals. ...
    (microsoft.public.access.externaldata)
  • Re: script advice
    ... Basically I'm inputting a filename, ... of files with the sander program to generate an rst file used to ... Semicolon seems to be missing at t.pl line 1. ...
    (comp.lang.perl.misc)