Wednesday, October 9, 2013

Sequentially numbering multiple copies of single document using a macro

Create a bookmark named SerialNumber in the document where you want the Serial Number to appear. It can be in the header or footer if that is where you want the number.

Then create a macro containing the following commands to print the document.

It will ask for the number of copies that you want to make and sequentially number each copy.

The first time this macro runs, the first copy will be numbered 1 and when it finishes running, it will store in aSettings.Txt file the number that is one more that the number on the last copy.

The next time the macro is run, it will start numbering the copies from that number. If when you first start, you want the numbers to start at some number other than 1, run the macro, entering 1 as the number of copies and then open Settings.Txt file and replace the number in the file with the number that you want as the first in the series.

At any time thereafter, if you want the series to start at a particular number, you can open that file and replace the number in it with the number that you want to be the first in the series.



Dim Message As String, Title As String, Default As String, NumCopies As Long
Dim Rng1 As Range

' Set prompt.
Message = "Enter the number of copies that you want to print"
' Set title.
Title = "Print"
' Set default.
Default = "1"

' Display message, title, and default value.
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = System.PrivateProfileString("C:\Settings.Txt", _
"MacroSettings", "SerialNumber")

If SerialNumber = "" Then
    SerialNumber = 1
End If

Set Rng1 = ActiveDocument.Bookmarks("SerialNumber").Range
Counter = 0

While Counter < NumCopies
    Rng1.Delete
    Rng1.Text = SerialNumber
    ActiveDocument.PrintOut
    SerialNumber = SerialNumber + 1
    Counter = Counter + 1
Wend

'Save the next number back to the Settings.txt file ready for the next use.
System.PrivateProfileString("C:\Settings.txt", "MacroSettings", _
        "SerialNumber") = SerialNumber

'Recreate the bookmark ready for the next use.
With ActiveDocument.Bookmarks
    .Add Name:="SerialNumber", Range:=Rng1
End With

ActiveDocument.Save
If you want the Serial Number to appear in a particular format, e.g. 001, 002, etc, replace the line
Rng1.Text = SerialNumber
with
Rng1.Text = Format(SerialNumber, "00#")

Tuesday, October 8, 2013

To run a macro at schedule time

If you want to keep your workbook open and run code at a specified time, later on, then try this..
Insert a new code module to your project -or- use one that you already have in your project.
Create a scheduler subroutine as shown below:

Sub Scheduler()

'-- RUNS SUB(S) (OR FUNCTIONS) AT TIME SCHEDULED.

    Application.OnTime TimeValue("11:46:40"), "TheScheduledSub"

End Sub

Create a subroutine (or function) that you want to run. Here is one for this example:

Sub TheScheduledSub()

    Debug.Print "TheScheduledSub() has run at " & Time

End Sub
Scheduler() will execute TheScheduledSub() at the exact time specified inside Scheduler(), defined by TimeValue. You can read more about TimeValue here.

You could probably pass the time value you want the Scheduler() sub to execute your sub(s) or function(s) in as a parameter in the sub.
You could probably run a sub 5 hours from now, lets say. I.E. Application.OnTime Now + TimeValue("05:00:00"), "TheScheduledSub"