Results 1 to 13 of 13
  1. #1
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Overwrite files setting? (Word 2000)

    What setting do I need to alter in order to have Word question before overwriting a duplicate file?

    I type multiple reports on a daily basis and have to save the current day's worth of work into a separate folder so that Word does not overwrite a possible existing file with the same name. My files are automatically named according to public information entered into the header macro, and I have no way of knowing in advance if there are existing files with the same name. I know that the overwrite setting can be altered because I have done it accidentally in the past, but don't know exactly how. Also, how reliable is the overwrite setting once it is activated?

    Can anyone help with this?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Overwrite files setting? (Word 2000)

    What command or method are you using for saving? If you are using ActiveDocument.SaveAs, you will have to check for the existence of the file in advance. You can use Dir for this:

    if Dir(myPath) <> vbNullstring then
    'Deal with existing file somehow
    End If

    There are fancier ways, but probably this one will serve most purposes.

  3. #3
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwrite files setting? (Word 2000)

    My current code to save the files are:

    ' This paragraph automatically saves the report with the predefined variables assigned to the report header
    ChangeFileOpenDirectory "D:REPORTS"
    ActiveDocument.SaveAs FileName:=vIPatient & "-" & vID & "-" & _
    "MB" & "-" & Format$(Now, "mm-dd-yyyy") & ".doc", FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
    :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False

    In the case of a duplicate file, I would like to add (2) to the end of the second file being saved. In order to add your code and get it to work with the (2), would I write it like this?:

    ChangeFileOpenDirectory "D:REPORTS"
    "if Dir D:REPORTS" <> vbNullstring then
    add (2)
    End If"

    ActiveDocument.SaveAs FileName:=vIPatient & "-" & vID & "-" & _
    "MB" & "-" & Format$(Now, "mm-dd-yyyy") & ".doc", FileFormat:=wdFormatDocument, _

    Somehow I feel as if I am doing something incorrectly here?

    Thank you for your help!

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Overwrite files setting? (Word 2000)

    Here's how it could work:
    <code>
    ' Set up test data (since I don't have real data)
    Const vIPatient As String = "PATIENT"
    Const vID As String = "ID"
    ' Set up variables to check file name
    Dim strName As String, intCount As Integer
    intCount = 1
    ' Create basic file name, then add (2), (3), etc., as needed to avoid overwrite
    strName = vIPatient & "-" & vID & "-" & "MB" & "-" & Format$(Now, "mm-dd-yyyy")
    While Dir("D:REPORTS" & strName & ".doc") <> vbNullString
    intCount = intCount + 1
    If intCount = 2 Then
    strName = strName & "(" & CStr(intCount) & ")"
    Else
    strName = Left(strName, InStrRev(strName, "(") - 1) & "(" & CStr(intCount) & ")"
    End If
    Wend
    ' Change directory for save, and do it
    ChangeFileOpenDirectory "D:REPORTS"
    ActiveDocument.SaveAs FileName:=strName & ".doc", _
    FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, _
    EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, _
    SaveFormsData:=False, SaveAsAOCELetter:=False
    </code>
    Hope this helps.

  5. #5
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwrite files setting? (Word 2000)

    It's been a long day and I'm not sure my mind is grasping all of this. I'm still pretty new at writing code in Word and wished I still had WordPerfect DOS!

    If I understand you correctly, then all I would need to add to my current code is the CONST (code below is from my report header):

    Selection.TypeText Text:="PATIENT: "
    CONST vIPatient = InputBox("PATIENT'S FIRST & LAST NAME: ")
    Selection.TypeText vIPatient

    After adding the CONST to the above code, I would then dim in (declarations) strName As String, intCount As Integer

    And then change my previously posted code to read?:

    While Dir("D:REPORTS" & strName & ".doc") <> vbNullString
    intCount = intCount + 1
    If intCount = 2 Then
    strName = strName & "(" & CStr(intCount) & ")"
    Else
    strName = Left(strName, InStrRev(strName, "(") - 1) & "(" &
    CStr(intCount) & ")"
    End If
    Wend
    ' Change directory for save, and do it
    ChangeFileOpenDirectory "D:REPORTS"
    ActiveDocument.SaveAs FileName:=strName & ".doc", _
    FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, _
    EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, _
    SaveFormsData:=False, SaveAsAOCELetter:=False

    Did I understand this correctly? (Is the Wend part of the code a typo, or should it really be there?)

    Thank you for your help!

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Overwrite files setting? (Word 2000)

    The line

    CONST vIPatient = InputBox("PATIENT'S FIRST & LAST NAME: ")

    is not valid, for you must assign a specific value to a constant in design time, but InputBox assigns a value in runtime. So you must use a variable instead of a constant:

    Dim vIPatient As String
    vIPatient = InputBox("PATIENT'S FIRST & LAST NAME: ")

    Don't forget the lines

    intCount = 1
    ' Create basic file name, then add (2), (3), etc., as needed to avoid overwrite
    strName = vIPatient & "-" & vID & "-" & "MB" & "-" & Format$(Now, "mm-dd-yyyy")

    Without them, the code becomes meaningless.

    Wend indicates the end of the code block that started with While. The lines between While and Wend will be repeated as long as the condition after While is met. You can also use the slightly more modern

    Do While Dir("D:REPORTS" & strName & ".doc") <> vbNullString
    ...
    ...
    Loop

    You have omitted the continuation character _ after the first line of

    strName = Left(strName, InStrRev(strName, "(") - 1) & "(" &
    CStr(intCount) & ")"

    It should be

    strName = Left(strName, InStrRev(strName, "(") - 1) & "(" & _
    CStr(intCount) & ")"

  7. #7
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwrite files setting? (Word 2000)

    Thank you Hans for explaining the Wend and pointing out the missing _.

    I already have vIPatient As String in the (declarations) box, which should make it a global variable? Where and how would I type vIPatient to make it constant (const)? I think that part of the communication problem here is that you guys know all of the terminology for writing code in Word, but it is still pretty much Greek to me. Could you explain it to me as if I were a five-year-old? (Seriously, no sarcasm meant.)

    Thank you again for your help!

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Overwrite files setting? (Word 2000)

    A constant is used to store a value that you (as programmer) already know when you are writing the code, and that won't have to be changed when the code is running.

    As an example, you might decide in advance that all documents are going to be stored in D:Reports. So you can define a constant
    <pre>Const strPath = "D:Reports"
    </pre>

    If, however, you want the user to be able to specify the path, you cannot define it as a constant. It must be a variable instead:
    <pre>Dim strPath As String
    strPath = InputBox("Please enter a path.")
    </pre>

    (There are better ways to prompt for a path, this is only meant as a simple illustration of the general idea)

    You define a constant or variable at the top of a module, before all Subs and Functions, if you need to share its value between Subs and Functions. If you're going to use a constant or variable in only one Sub or Function, you should define it in that Sub or Function.

    If I understand you correctly, you want the user to provide the name of the patient when the code is executed. In terms of Visual Basic, this means that the patient is a variable, not a constant. If you are going to use the name of the patient only in one procedure (Sub), you must define vIPatient within that procedure:
    <pre>Sub DoSomething()
    Dim vIPatient As String
    ...
    vIPatient = InputBox(...)
    ...
    End Sub
    </pre>

    If you need to use the same patient name in other procedures or functions without prompting the user to enter it again, you must define vIPatient at the top of the module, in the Declarations section. If you only need it in procedures or functions within the same module, you make it a private variable. It will be known to all procedures and functions within the module where it is defined, but procedures and functions in other modules won't know it.
    <pre>Private vIPatient As String

    Sub DoSomething()
    ...
    vIPatient = InputBox(...)
    ...
    End Sub

    Sub DoSomethingElse()
    ...
    MsgBox vIPatient
    ...
    End Sub
    </pre>

    If you need the patient name in procedures or functions in other modules as well, define it as a public variable:
    <pre>Public vIPatient As String
    </pre>

    You can refer to this variable throughout all modules. But, of course, it will be empty until you assign it a value.

  9. #9
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwrite files setting? (Word 2000)

    Thank you Hans! I believe that I now have a better idea of constants versus variables. I do use my variables again in other functions, so the global variable would be correct. From what I understand, I should define the path as a constant at the top in (declarations) since I want it to be shared by multiple functions. After that, I should only have to define the path: as a constant?

    I will give my theory a try and play with the macro when I've had more sleep. I'll let you know if it works, as well as if it doesn't.

    Thanks again for your very simple definition!

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Overwrite files setting? (Word 2000)

    I'm really, really sorry for the confusion! The line "Set up test data" was my only clue to you that you could ignore those two lines, because you already have that data, while I (or anyone else testing the code) needed to create that data in order to end up with a valid file name. So, removing those few lines that were for testing only, my code basically replaces your very simple ActiveDocument.SaveAs line. Does that make sense?

  11. #11
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwrite files setting? (Word 2000)

    Thank you jscher! I think that I was easily confused that night and hopefully have a better idea now. I did realize that the first part of your post was an example, but the constant threw me. Basically, I should be able to cut and paste the code below for it to work, without making anything constant (replacing my portion of code)? I'm in the middle of my work day, so I will give this a try over the weekend. Please let me know if I still am not understanding this correctly.

    While Dir("D:REPORTS" & strName & ".doc") <> vbNullString
    intCount = intCount + 1
    If intCount = 2 Then
    strName = strName & "(" & CStr(intCount) & ")"
    Else
    strName = Left(strName, InStrRev(strName, "(") - 1) & "(" &_
    CStr(intCount) & ")"
    End If
    Wend
    ' Change directory for save, and do it
    ChangeFileOpenDirectory "D:REPORTS"
    ActiveDocument.SaveAs FileName:=strName & ".doc", _
    FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, _
    EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, _
    SaveFormsData:=False, SaveAsAOCELetter:=False

    Thank you again!

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Overwrite files setting? (Word 2000)

    I suggest starting from this comment line (and keeping the comments for future reference):

    ' Set up variables to check file name

    Since you only need the two variables for this one little function, it will be easier to understand in the future if you put all these pieces right there. Also, it is very important to set intCount to 1 before entering the While loop. Otherwise, it should crash! (Feel free to test this...)

  13. #13
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Overwrite files setting? (Word 2000)

    It worked!! Thank you so much for all of your help! What a relief to be able to get rid of many folders!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •