#1
|
|||
|
|||
macro to change name of files in a folder in order
Hello all, II have a macro that whenever I run it, it save the file in PDF format with the date and a number, the files are invoices, I wrote a macro and it works nice except one thing. When I run the macro and it save the file for example witht the format invoice-130001-07/08/2014, (the PDF starts from 130000) and the 9th generated file that is the last generated file of the day has the name invoice-130009-07/08/2014 but but when I save it in the next day, logically it must be invoice-130010-08/08/2014 but it starts again from 130001 for the number and macro generates the name invoice-130001-08/08/2014 I want to know if there is anyway to fix it?
The code is here and the file is attached: Code:
Sub quote13000() ' Dim exdate As Date Dim o As Object Dim odoc As Document Dim nm, nm1 As String Dim i As Integer Dim PathAndFileName As String, n As Long Set odoc = ActiveDocument n = 3000 PathAndFileName = "C:\Users\Tony\Documents\Quotes\Quote 1" If Dir(PathAndFileName & n & " " & Format(Now, "mm-dd-yy") & ".pdf") = "" Then nm = PathAndFileName & n & " " & Format(Now, "mm-dd-yy") & ".pdf" With ActiveDocument .SaveAs FileName:=nm, _ Fileformat:=wdFormatPDF '.Close End With Else n = 3000 Do While Dir(PathAndFileName & n & " " & Format(Now, "mm-dd-yy") & ".pdf") <> "" n = n + 1 Loop nm = PathAndFileName & n & " " & Format(Now, "mm-dd-yy") & ".pdf" With ActiveDocument .SaveAs FileName:=nm, _ Fileformat:=wdFormatPDF ' .Close End With End If End Sub Last edited by macropod; 07-08-2014 at 03:13 PM. Reason: Added code tags & formatting |
#2
|
||||
|
||||
Unless you store and retrieve the last-used number, the only way the macro could pick up where it left off is to test the numbers of all the files produced in previous iterations, to see what the highest number is. For some approaches to that, see the links in this post: https://www.msofficeforums.com/word/...mber-word.html
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I used the procedure mentioned in the following link and combined it with my code:
https://www.msofficeforums.com/word/...iple-docs.html But it seems that it is not working. I think the idea of saving the last number is good but where can I save it? The document property seems does not working in my file? I put it in the header to be able to remove the colour and make it sightless, attached is the file. BTW thanks for putting the code in appropriate tags since I could not see it at the first. I think I should try to find the answer to question What other ways can I save the last generated number? |
#4
|
||||
|
||||
Without wanting to place too fine a point on it, there is no DOCPROPERTY field referencing InvNum in your document. All you have is a '{DOCPROPERTY InvNum}' text string. That is not a field.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
How can I put a field to it? May I ask you tell a reference for me so I learn how to do it?
|
#6
|
||||
|
||||
Delete the {} from your '{DOCPROPERTY InvNum}', then select 'DOCPROPERTY InvNum' and press Ctrl-F9. Press F9 again to update the field.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
save as pdf in order |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I change the page number order? | officeboy09 | Excel | 4 | 10-30-2013 08:11 PM |
Word Macro - change date in footer for all files in a folder | patidallas22 | Word VBA | 2 | 03-09-2012 08:14 AM |
O 2007 folders & files lose alphabetical order in | John Seed | Outlook | 0 | 02-28-2012 05:56 PM |
2010 Office -- Favorites folder not staying in order. | Emerogork | Outlook | 9 | 08-13-2011 07:00 AM |
Change order of names to LAST / FIRST ? | Jack R | Outlook | 1 | 07-05-2010 08:50 PM |