Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save Macro-Word 2000

Status
Not open for further replies.

luceze

Programmer
Apr 26, 2001
842
US
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:="C:\output.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
 
Hi

Your problem,as I see it from your code, has to do with the 'scope' or 'lifetime' of the variable x. X is created each time the procedure starts up and is destroyed when it ends. So x is reset to 1 each time you load PRINT.DOC. My suggested solution will guarantee an uniquely named file (i.e. OUTPUT1.DOC, OUTPUT2.DOC,...,etc.) every time you open PRINT.DOC, even sometime next month!

First you will need all your current files (I'll assume you want everything done in the root directory of drive C:) plus also a plain text file, that I've called FILENUMBER.TXT, initially containing just one character - 0 (zero), also residing in C:\.

Here's the code in the Open() event procedure of PRINT.DOC :

Private Sub Document_Open()
Dim x As Integer

ChangeFileOpenDirectory "C:\"
Documents.Open FileName:="output.txt"


Open "FileNumber.txt" For Input As #1
Input #1, x
Close #1

x = x + 1

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


Open "FileNumber.txt" For Output As #1
Write #1, x
Close #1

Windows(1).Activate
ActiveWindow.Close wdDoNotSaveChanges

End Sub

This works by saving the current 'file number' (x) to a small text file, reading it again next time PRINT.DOC is opened and incrementing it by one to create the next file number in sequence, which is resaved.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top