none
Creating a copy of Document and it does not close RRS feed

  • Question

  • I have code that creates a copy of ThisDocument, saves the document to the name and path in the SaveAs dialog and then closes the saved copy. It works except that the saved copy gets stuck and I have to force quit Word to close it. Here is my code:

    Sub CreateReport()
        Dim tbl As Table
        Dim ans
        Dim myCopy As Document
        
        Set tbl = ThisDocument.Sections.First.Headers(wdHeaderFooterPrimary).Range.Tables(1)
        
        ans = MsgBox("Do you want to save this report?", vbYesNo)
        
        If ans <> 7 Then
        
            Set myCopy = Documents.Add(ThisDocument.FullName)
            With Dialogs(wdDialogFileSaveAs)
                .Name = Left(tbl.Cell(1, 2).Range.Text, Len(tbl.Cell(1, 2).Range.Text) - 2) & " Payment Schedule.docx"
                .Format = Word.WdSaveFormat.wdFormatXMLDocument
                .Show
            End With
            
            Application.DisplayAlerts = wdAlertsNone
                myCopy.Close
            Application.DisplayAlerts = wdAlertsAll
            
            MsgBox "Document has been created", vbOKOnly
        End If
    End Sub

    Sunday, March 1, 2020 7:23 PM

Answers

  • I was able to solve the problem by taking out the dialog to get name and path. I just do a straight up SaveAs2 and hard code the file name using one of the user entries in the document. 

    Down side is that user now has to go to their documents folder to rename the file and/or move it to where they want it. But, I had a deadline of tomorrow morning so...had to have something that works. 

    I'll continue to make improvements to the template after I get this first release out. 

    Thanks to those who've chimed in on my questions as I build this thing.

    tod

    • Marked as answer by todtown Monday, March 2, 2020 1:54 AM
    Monday, March 2, 2020 1:54 AM

All replies

  • Before trying to replicate your scenario, what happens if you comment out 

    Application.DisplayAlerts = wdAlertsNone

    (or rather, if you ensure that 

    Application.DisplayAlerts = wdAlertsAll

    before the myCopy.Close

    ?

    (Just because you suppress alerts doesn't mean that Word isn't waiting for a response from you and "getting stuck" on account of that)


    Peter Jamieson

    Sunday, March 1, 2020 7:42 PM
  • I should also mention that my code works on a PC. This problem is when I try to use it on a Mac. Sorry I left that out.

    I put the displayalerts to try to fix the problem. 

    The code displays the dialog box with the suggested file name and I click Yes and the file is created in the path, but it stays open and I can not close it without Force Quit.


    • Edited by todtown Sunday, March 1, 2020 7:55 PM
    Sunday, March 1, 2020 7:54 PM
  • I had a quick look on Mac (here, Mac OS 10.15.3, word 16.34)

    but so far I didn't have the same experience. The one thing I noticed is that . the value of .Name in Dialogs(wdDialogFileSaveAs) is not the same as the name assigned to it by the expression you use and the one I see in the relevant textbox in the dialog, so perhaps the bycopy.Close is not receiving the .Name that you expect.

    Will have a further look tomorrow if that hasn't shed any light.


    Peter Jamieson


    Sunday, March 1, 2020 10:06 PM
  • I was able to solve the problem by taking out the dialog to get name and path. I just do a straight up SaveAs2 and hard code the file name using one of the user entries in the document. 

    Down side is that user now has to go to their documents folder to rename the file and/or move it to where they want it. But, I had a deadline of tomorrow morning so...had to have something that works. 

    I'll continue to make improvements to the template after I get this first release out. 

    Thanks to those who've chimed in on my questions as I build this thing.

    tod

    • Marked as answer by todtown Monday, March 2, 2020 1:54 AM
    Monday, March 2, 2020 1:54 AM