Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    vancouver, BC, Br. Columbia
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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 <big>GREATLY</big> appreciated!

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

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

    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
    <code>Set dlgSaveAs = Nothing</code>
    at the end of your code before the <code>End Sub</code>

    StuartR

  3. #3
    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: Hijacked FileSaveAs Saves the Wrong Document (Word 2002 SP 2)

    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

  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: Hijacked FileSaveAs Saves the Wrong Document (Word 2002 SP 2)

    You might want to move the call to SaveAs inside the If loop, or users pressing Cancel might complain. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    <P ID="edit" class=small>(Edited by Andrew77 on 08-Jul-04 05:32. Moved SaveAs inside If ... Then (Thanks, Jefferson))</P>Hi Gwenda,

    How about something like this:

    <pre>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
    </pre>


    Should avoid any problems with other documents activating.

    HTH!

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Just trying to keep you on your toes. (You're welcome). <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  7. #7
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    vancouver, BC, Br. Columbia
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

  8. #8
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    vancouver, BC, Br. Columbia
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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?

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

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

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

  10. #10
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    vancouver, BC, Br. Columbia
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    <img src=/S/cloud9.gif border=0 alt=cloud9 width=25 height=23> 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!

  11. #11
    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: Hijacked FileSaveAs Saves the Wrong Document (Word 2002 SP 2)

    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.

  12. #12
    2 Star Lounger
    Join Date
    Jun 2002
    Location
    vancouver, BC, Br. Columbia
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Thanks Jefferson. Your code is perfect.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hijacked FileSaveAs Saves the Wrong Document (

    <pre>If dial.Display = -1 Then
    sSaveAs = CurDir & "" & dial.Name
    vFormat = dial.Format
    doc.SaveAs FileName:=sSaveAs, fileformat:=vFormat
    End If</pre>


    Thank you!

  14. #14
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

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

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

  15. #15
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    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
    <UL><LI>Replace exiting file;
    <LI>Save changes with a different name
    <LI>Merge changes into existing file[/list] I would like to either:
    <UL><LI>Inhibit the system query from being displayed, and raise my own query with the code; or
    <LI>Disable the third option (Merge changes into existing file), in the query; or
    <LI>Trap the third option if it is selected and invoke appropriate code before the file is saved .[/list] Any guidance will be greatly appreciated.

    <pre>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
    </pre>

    Regards
    Don

Page 1 of 2 12 LastLast

Posting Permissions

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