• Hijacked FileSaveAs Saves the Wrong Document (Word 2002 SP 2)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Hijacked FileSaveAs Saves the Wrong Document (Word 2002 SP 2)

    Author
    Topic
    #407073

    Hi Loungers,
    I have hijacked FileSaveAs with the following code (I’ve stripped out a lot of the stuff I was doing whilst trying to hone in on the problem):

    Sub FileSaveAs()
    Dim dlgSaveAs As FileDialog
    Set dlgSaveAs = Application.FileDialog( FileDialogType:=msoFileDialogSaveAs)

    With dlgSaveAs
    If .Show = -1 Then ‘user clicked OK
    .Execute
    End If
    End With
    End Sub

    If there are two or more documents open, Word will sometimes switch to a different open document just before displaying the msoFileDialogSaveAs, which causes the user to save the wrong file.

    Any insights GREATLY appreciated!

    Viewing 5 reply threads
    Author
    Replies
    • #848631

      I haven’t seen the exact problem you describe, but something very similar happens if you load a userform whilst one document is active and fail to unload it again. Next time you Show that userform word switches back to the document that was active the previous time.

      Try including the line
      Set dlgSaveAs = Nothing
      at the end of your code before the End Sub

      StuartR

      • #849029

        Thanks Stuart, but no joy – it still switches active documents just before the save. I am going to follow Jefferson’s advice and use Word.Dialogs(wdDialogFileSaveAs).Show instead.

      • #849030

        Thanks Stuart, but no joy – it still switches active documents just before the save. I am going to follow Jefferson’s advice and use Word.Dialogs(wdDialogFileSaveAs).Show instead.

    • #848632

      I haven’t seen the exact problem you describe, but something very similar happens if you load a userform whilst one document is active and fail to unload it again. Next time you Show that userform word switches back to the document that was active the previous time.

      Try including the line
      Set dlgSaveAs = Nothing
      at the end of your code before the End Sub

      StuartR

    • #848641

      The .FileDialog object is new in Word 2002, and I don’t know why it would be better or worse to use than the old .Dialogs collection. Being somewhat wedded to old (and reliable) ways, I’d simply use the following code and see if the problem goes away:

      Sub FileSaveAs()
      Word.Dialogs(wdDialogFileSaveAs).Show
      End Sub

      Note that the .Show method of a Word dialog performs both .Display and, if relevant, .Execute (different than the FileDialog object). If you need to take some action in the midst of that process, use code more like this:

      Sub FileSaveAs()
      with Word.Dialogs(wdDialogFileSaveAs)
      ‘Your code here
      If .Display = -1 Then
      ‘Your code here
      .Execute
      ‘Your code here
      End If
      End With
      End Sub

      • #849110

        Hi Jefferson – I’ve switched from FileDialog to Dialog and the problem of the slippery ActiveDocument problem has gone away. … to be replaced by another: Using FileDialog I was able to test whether the user was trying to save in the root of C: using the SelectedItems property and if so, I was just exiting with a “try again” message. Do you know any way to test the path the user has selected in the Word.Dialogs(wdDialogFileSaveAs) and then Exit Sub before the .Execute?

        • #849114

          You can check Options.DefaultFilePath(wdCurrentFolderPath). If it equals “c:”, display a message box and get out.

          • #849119

            cloud9 WOW! It says in Help about DefaultFilePath Property, “The new setting takes effect immediately” but who would ever have thought that it would take effect between a .Show and a .Execute?? Thank you!

        • #849115

          You can check Options.DefaultFilePath(wdCurrentFolderPath). If it equals “c:”, display a message box and get out.

        • #849139

          Here’s a way to re-display the dialog until they get out of the root of C::

          Sub FileSaveAs()
          ‘ Sample hijacked File>SaveAs
          Dim strTemp As String
          Do
          With Word.Dialogs(wdDialogFileSaveAs)
          ‘ Try to intelligently pre-set the file name
          If strTemp vbNullString Then
          .Name = strTemp
          End If
          If .Display = -1 Then
          ‘Only execute outside the root of c:
          If Options.DefaultFilePath(wdCurrentFolderPath) “c:” Then
          ‘ Save file and escape the loop
          .Execute
          Exit Do
          Else
          ‘ Store the user’s file name but demand a different path
          strTemp = .Name
          MsgBox “Please choose a different path!”, vbCritical
          End If
          Else
          ‘User canceled
          Exit Sub
          End If
          End With
          Loop
          ‘More post-save stuff
          End Sub

          Hope this helps.

        • #849140

          Here’s a way to re-display the dialog until they get out of the root of C::

          Sub FileSaveAs()
          ‘ Sample hijacked File>SaveAs
          Dim strTemp As String
          Do
          With Word.Dialogs(wdDialogFileSaveAs)
          ‘ Try to intelligently pre-set the file name
          If strTemp vbNullString Then
          .Name = strTemp
          End If
          If .Display = -1 Then
          ‘Only execute outside the root of c:
          If Options.DefaultFilePath(wdCurrentFolderPath) “c:” Then
          ‘ Save file and escape the loop
          .Execute
          Exit Do
          Else
          ‘ Store the user’s file name but demand a different path
          strTemp = .Name
          MsgBox “Please choose a different path!”, vbCritical
          End If
          Else
          ‘User canceled
          Exit Sub
          End If
          End With
          Loop
          ‘More post-save stuff
          End Sub

          Hope this helps.

          • #849156

            Thanks Jefferson. Your code is perfect.

          • #849157

            Thanks Jefferson. Your code is perfect.

          • #1049007

            Jefferson
            Thank you for once again reminding me how much easier it is to understand code that is well commented.

          • #1066569

            Hi Jefferson
            I have taken your code and modified it as shown below. But I am in need of a little guidance.

            In the event the user selects a folder containing a file with the same name, a query is raised by the system asking whether the user wants to

            • Replace exiting file;
            • Save changes with a different name
            • Merge changes into existing file
              [/list] I would like to either:

              • Inhibit the system query from being displayed, and raise my own query with the code; or
              • Disable the third option (Merge changes into existing file), in the query; or
              • Trap the third option if it is selected and invoke appropriate code before the file is saved .
                [/list] Any guidance will be greatly appreciated.

                Option Explicit
                ' Creator_
                Const myTitle = "Claim Computer Ownership"
                Public Sub Main()
                Dim SaveLoc As String
                Dim UID As String
                Dim OrigPath As String
                Dim msg As String
                
                    OrigPath = ThisDocument.Path
                    ChangeFileOpenDirectory Environ("userprofile") & "My Documents"
                    
                    Do
                        msg = "Enter the ""Payroll Number"" of the person who will be" _
                            & " claiming ownership of their computer."
                        UID = LCase(InputBox(msg, myTitle))
                        With Dialogs(wdDialogFileSaveAs)
                            .Name = UID & "_Ownership_Claim.doc"
                            If .Display = -1 Then
                                If Options.DefaultFilePath(wdCurrentFolderPath)  OrigPath _
                                    And .Name = UID & "_Ownership_Claim.doc" Then
                                    ' Save the file and escape the loop
                                    Application.DisplayAlerts = wdAlertsNone
                                    .Execute
                                    Application.DisplayAlerts = wdAlertsAll
                                    Exit Do
                                Else
                                    msg = "Do not revise the filename. Leave it as" & vbCrLf
                                    msg = msg & UID & "_Ownership_Claim.doc" & vbCrLf & vbCrLf
                                    msg = msg & UID & "_Ownership_Claim.doc" & " must be saved " _
                                            & vbCrLf
                                    msg = msg & "in a folder that is accessible by the intended user's " _
                                            & vbCrLf
                                    msg = msg & "machine; and must NOT be saved in:" & vbCrLf
                                    msg = msg & OrigPath
                                    
                                    MsgBox msg, vbExclamation, myTitle
                                    
                                End If
                                
                            Else
                                ' User cancelled
                                MsgBox "User cancelled", vbCritical
                                Stop
                            End If
                        End With
                    Loop
                
                End Sub
                
            • #1066571

              The query is displayed by Word when the user clicks the Save button in the Save As dialog, I don’t think you can intercept it.

              You could use the FileDialog object with the msoFileDialogSaveAs option instead, this displays the standard prompt whether you want to overwrite the file or not; the merge option isn’t offered. Howver, this prompt cannot be suppressed by setting DisplayAlerts to False.

            • #1066583

              Thank you Hans
              Once again you’ve provided the guidance needed. I will use the FolderPicker option combined with a Dir statement to create my own query.

              Thanks again

          • #1066581

            Hi Jefferson
            Further to my post, I was able to solve the problem with the following If Then Else construct wrapped around the Exit Do statement.
            Just in case someone else has a similar problem. Mind you, the original file on disk is modified.

                                If MacroContainer.Name = .Name Then
                                    Exit Do
                                Else
                                    Documents(.Name).Close wdDoNotSaveChanges
                                    msg = "Merging the files is not an acceptable option." & vbCrLf
                                    msg = msg & "We will try again."
                                    MsgBox msg, vbExclamation, myTitle
                                End If
            
      • #849111

        Hi Jefferson – I’ve switched from FileDialog to Dialog and the problem of the slippery ActiveDocument problem has gone away. … to be replaced by another: Using FileDialog I was able to test whether the user was trying to save in the root of C: using the SelectedItems property and if so, I was just exiting with a “try again” message. Do you know any way to test the path the user has selected in the Word.Dialogs(wdDialogFileSaveAs) and then Exit Sub before the .Execute?

    • #848642

      The .FileDialog object is new in Word 2002, and I don’t know why it would be better or worse to use than the old .Dialogs collection. Being somewhat wedded to old (and reliable) ways, I’d simply use the following code and see if the problem goes away:

      Sub FileSaveAs()
      Word.Dialogs(wdDialogFileSaveAs).Show
      End Sub

      Note that the .Show method of a Word dialog performs both .Display and, if relevant, .Execute (different than the FileDialog object). If you need to take some action in the midst of that process, use code more like this:

      Sub FileSaveAs()
      with Word.Dialogs(wdDialogFileSaveAs)
      ‘Your code here
      If .Display = -1 Then
      ‘Your code here
      .Execute
      ‘Your code here
      End If
      End With
      End Sub

    • #848633

      (Edited by Andrew77 on 08-Jul-04 05:32. Moved SaveAs inside If … Then (Thanks, Jefferson))

      Hi Gwenda,

      How about something like this:

      Sub FileSaveAs()
      Dim doc As Document
      Dim sSaveAs As String
      Dim vFormat As Variant
      
      Dim dial As Dialog
      Set doc = ActiveDocument
      
      Set dial = Dialogs(wdDialogFileSaveAs)
      
      If dial.Display = -1 Then
          sSaveAs = CurDir & "" & dial.Name
          vFormat = dial.Format
         doc.SaveAs FileName:=sSaveAs, fileformat:=vFormat
      End If
      
      
      End Sub
      

      Should avoid any problems with other documents activating.

      HTH!

      • #848643

        You might want to move the call to SaveAs inside the If loop, or users pressing Cancel might complain. smile

      • #848644

        You might want to move the call to SaveAs inside the If loop, or users pressing Cancel might complain. smile

      • #925379
        If dial.Display = -1 Then
            sSaveAs = CurDir & "" & dial.Name
            vFormat = dial.Format
           doc.SaveAs FileName:=sSaveAs, fileformat:=vFormat
        End If

        Thank you!

    • #848634

      (Edited by Andrew77 on 08-Jul-04 05:32. Moved SaveAs inside If … Then (Thanks, Jefferson))

      Hi Gwenda,

      How about something like this:

      Sub FileSaveAs()
      Dim doc As Document
      Dim sSaveAs As String
      Dim vFormat As Variant
      
      Dim dial As Dialog
      Set doc = ActiveDocument
      
      Set dial = Dialogs(wdDialogFileSaveAs)
      
      If dial.Display = -1 Then
          sSaveAs = CurDir & "" & dial.Name
          vFormat = dial.Format
         doc.SaveAs FileName:=sSaveAs, fileformat:=vFormat
      End If
      
      
      End Sub
      

      Should avoid any problems with other documents activating.

      HTH!

    Viewing 5 reply threads
    Reply To: Hijacked FileSaveAs Saves the Wrong Document (Word 2002 SP 2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: