• Word VBA SaveAs bug (2000 SR1)

    Author
    Topic
    #376725

    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?

    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
    
    Viewing 1 reply thread
    Author
    Replies
    • #618151

      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:

      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
      
      • #618399

        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)

        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
        
    • #618224

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

      • #618400

        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.

        • #618418

          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.

          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

          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…

          • #618989

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

    Viewing 1 reply thread
    Reply To: Word VBA SaveAs bug (2000 SR1)

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

    Your information: