Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Sep 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Save Macro (Word2000)

    I have a macro in a document called print.doc that opens a text document then saves it as output1.doc. The 1st document then closes but output1.doc remains open. If the user thens opens print.doc again it opens the same .txt document and then saves the document as output2.doc. The problem is that when the user opens this a third time it gives me an error that word can not give a document the same name as another document. I am getting the error after an error occurs for the second time. The error handler doesn't seem to work. I don't know if this makes any sense or not but here is the code:

    Private Sub Document_Open()
    Documents.Open FileName:="Cutput.txt"
    ChangeFileOpenDirectory "C:"

    Dim x As Integer
    RetrySave:

    x = x + 1
    On Error GoTo RetrySave:


    ActiveDocument.SaveAs FileName:="output" & x & ".doc", FileFormat:=wdFormatDocument _
    , LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False

    Windows(1).Activate
    ActiveWindow.Close wdDoNotSaveChanges

    End Sub

    Any help will be appreciated.

    Thanks,
    Eric

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

    Re: Save Macro (Word2000)

    Try putting <font color=blue>Err.Clear</font color=blue> after <font color=blue>RetrySave</font color=blue> and before <font color=blue>x = x + 1</font color=blue>

    Ideally you should check that Err.Number is the particular error that you get returned when the document already exists before you do this.

    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: Save Macro (Word2000)

    I don't completely understand the error handling in VB/VBA, but I know my luck is best when I use Resume to exit my error handlers.

    The following approach might work better for you:
    <pre>Private Sub Document_Open()
    Dim docOriginal As Document
    Set docOriginal = ActiveDocument

    Dim docTextDoc As Document
    Set docTextDoc = Documents.Open(FileName:="Cutput.txt")

    Dim x As Integer
    x = 1

    Do Until Dir("cutput" & CStr(x) & ".doc") = vbNullString
    x = x + 1
    Loop

    ChangeFileOpenDirectory "C:"
    ActiveDocument.SaveAs FileName:="output" & CStr(x) & ".doc", _
    FileFormat:=wdFormatDocument, LockComments:=False, _
    Password:="", AddToRecentFiles:=True, WritePassword:="", _
    ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, _
    SaveAsAOCELetter:=False

    'Windows(1).Activate
    'ActiveWindow.Close wdDoNotSaveChanges

    ' The above lines should be replaced by something more specific like
    ' docOriginal.Close SaveChanges:=wdDoNotSaveChanges 'or
    ' docTextDoc.Close SaveChanges:=wdDoNotSaveChanges
    End Sub</pre>

    The general idea is to pre-check until no matching file name is found, and then make one call to the SaveAs method. Hope this helps.

  4. #4
    New Lounger
    Join Date
    Sep 2002
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Save Macro (Word2000)

    That worked great thank you!

Posting Permissions

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