Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Word VBA SaveAs bug (2000 SR1)

    I'm actually doing this from Access, but the same problem comes up with Word VBA so I've posted it here.
    I want to create the document programmatically, but then give the user the opportunity to specify the save as name and location. This code works fine in two cases: Cancel is trapped correctly and if they specify a new name it works. But if they specify an existing name (to overwrite it), the dialog box doesn't release control - it stays 'stuck' in the .Display method.

    Is this a Word bug?

    <pre>Sub testWordSaveAsDialog()
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim vFileSaveName As Variant

    Set wd = New Word.Application
    Set doc = wd.Documents.Add

    With doc
    Dim myDialog As Dialog
    Set myDialog = wd.Dialogs(wdDialogFileSaveAs)

    If myDialog.Display = 0 Then
    'Cancel
    MsgBox "File save cancelled"
    .Close SaveChanges:=wdDoNotSaveChanges
    Else
    'Save
    vFileSaveName = myDialog.Name
    .SaveAs vFileSaveName
    MsgBox "File saved as: " & vFileSaveName
    .Close
    End If
    End With
    wd.Quit
    Set wd = Nothing

    End Sub
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word VBA SaveAs bug (2000 SR1)

    I doubt it's a bug.

    You really ought to trap for 3 return values from the .display method:

    -2 = close
    -1 = ok (in this case Save)
    0 = cancel

    Any value greater than 1 means some other button on the dialog was clicked.

    Here's a clip from a FileSaveAs I use. You may find the structure helpful:

    <pre>Select Case .Display
    Case -2 'Close
    Exit Sub
    Case -1 'ok
    vFileName = .Name
    If InStr(vFileName, " ") <> 0 Then 'Filename has spaces
    vFileName = Mid$(.Name, 2, Len(.Name) - 2)
    End If
    Call WriteProtectOff
    'Update all fields before save
    Call UpdateFieldsKP
    Call UpdateFootNotes
    Call ConvertDateToSaveDate
    'Call PlaceHolderDrop 'Jiggles the screen which is irritating.
    'Set the current user as the owner as this is NOT the original document
    Call DocVarAssign("DocOwner", oReg.RGet("Personal", "FullName"))
    If oReg.RGet("Personal", "Delegates") = "" Then
    Call DocVarAssign("DocDelegates", oReg.RGet("Personal", "FullName"))
    Else
    Call DocVarAssign("DocDelegates", oReg.RGet("Personal", "Delegates"))
    End If

    ActiveDocument.AttachedTemplate.Saved = True
    .Execute
    .Update
    Call UpdateFieldsKP 'Make the changes from Date To SaveDate visible
    .Execute
    Exit Do
    'End If
    Case 0 'Cancel
    Exit Do
    End Select
    </pre>

    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  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: Word VBA SaveAs bug (2000 SR1)

    > if they specify an existing name (to overwrite it), the dialog box doesn't release control - it stays
    > 'stuck' in the .Display method

    Does the user get the messagebox (Yes/No/Cancel) to confirm that he or she wishes to overwrite the existing file? If you run the .Display method from inside Word, you typically get that message and Word proceeds accordingly.

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Re: Word VBA SaveAs bug (2000 SR1)

    I agree, I really should check the other return values from .Display. But my problem is that it doesn't get past the Display. I've tried this on my machine at home (Word 2000, no SR), with the same problem.
    Here's a more cut-down version of the problem. It doesn't actually do a save. But if I select a filename that already exists, then the MsgBox doesn't get displayed. (Caution: just to be on the safe side, create a blank doc manually through Word, and use that as the test save-over file)
    <pre>Sub testWordSaveAsDialog2()
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim myDialog As Dialog

    Set wd = New Word.Application
    Set doc = wd.Documents.Add
    Set myDialog = wd.Dialogs(wdDialogFileSaveAs)
    myDialog.Display
    MsgBox "Reached here"
    wd.Quit
    Set wd = Nothing
    End Sub
    </pre>


  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Re: Word VBA SaveAs bug (2000 SR1)

    No, they don't get any message box. It seems 'stuck' in the dialog box - the dialog box doesn't release control.

    In my (limited) understanding, .Display doesn't actually do the save. It just displays the dialog box and then I have to call the .SaveAs method of the document to save it. But if I choose a filename that already exists, it gets 'stuck' in the .Display - it doesn't reach the step after the .Display
    I tried using the .Show method to display the dialog box and do the save, but had the same (lack of) success.

  6. #6
    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: Word VBA SaveAs bug (2000 SR1)

    I couldn't get it to work either, until I changed the method used to create the wd object. I have no explanation for why the following works but the other method doesn't. Absolutely no clue.
    <pre>Sub testWordSaveAsDialog2()
    Dim wd As Object 'was Word.Application
    Dim doc As Word.Document
    Dim myDialog As Dialog

    'Set wd = New Word.Application
    Set wd = CreateObject("Word.Application")
    With wd
    .Visible = True
    .DisplayAlerts = wdAlertsAll
    Set doc = .Documents.Add
    doc.Range.InsertAfter "Testing"
    With .Dialogs(wdDialogFileSaveAs)
    If .Display = -1 Then
    MsgBox "You want to save!"
    '.Execute
    End If
    End With
    End With
    doc.Close savechanges:=wdDoNotSaveChanges
    Set doc = Nothing
    wd.Quit
    Set wd = Nothing
    End Sub</pre>

    By the way, you carry out the user's choice in a Word dialog with the .Execute method. Some of the stuff I added probably is unnecessary, but seemed like good hygiene during the testing process...

  7. #7
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Re: Word VBA SaveAs bug (2000 SR1) - Thanks

    Excellent - thank you.
    I also don't know why this works, but it does!
    Peter

Posting Permissions

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