Option Explicit
Dim WSHShell As Object
Dim oBMs As
Bookmarks
Dim iCount As Long
Dim rkeyWord1 As String
Dim rkeyWord2 As String
Dim rkeyWord3 As String
Dim sCount As String
Dim sLeadingText As String
Dim sTrailingText As String
Dim Response As String
Dim myRng As
Range
Dim i As Long
Const RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
Sub InvoicerSetup()
'Macro by Graham Mayor 6 February 2006
'With input and improvements suggested by Greg Maxey
'http://gregmaxey.mvps.org/
'Revision 17
InvoicerStart:
Set WSHShell = CreateObject("WScript.Shell")
Set oBMs = ActiveDocument.Bookmarks
On Error GoTo setUpError:
If MsgBox("Locate/relocate the Invoice
number" _
& " at the current cursor position?", _
vbYesNo, "Invoice Number Position") = vbYes
Then
'It already exists so delete it
If oBMs.Exists("SeqNum")
Then
oBMs("SeqNum").Range.Delete
End If
'Define and setup empty bookmark
Selection.Collapse wdCollapseStart
Selection.Bookmarks.Add "SeqNum", Selection.Range
Else
'Force setup of bookmark
If Not oBMs.Exists("SeqNum")
Then
MsgBox "Place the insertion point where you want the" _
& " invoice number and start this procedure again.", vbOKOnly,
_
"Insertion Point"
Exit Sub
End If
End If
'Setup/Reset the three starting entries in the
registry
rkeyWord1 = WSHShell.RegRead(RegKey & "InvoiceNo")
rkeyWord2 = WSHShell.RegRead(RegKey & "InvoiceLeadingText")
rkeyWord3 = WSHShell.RegRead(RegKey & "InvoiceTrailingText")
'ask for the new start number, whilst showing the
current next number as default
iCount = InputBox("Set/reset starting
sequence number? The" _
& " next number is currently: " & Val(rkeyWord1), _
"Sequence Number", Val(rkeyWord1))
If iCount <= 1 Then 'if negative start number or
zero is entered
iCount = 1 ' make it 1!
End If
'Write the start number to the registry
WSHShell.regwrite RegKey & "InvoiceNo", iCount
'Ask for the leading text, whilst showing the
existing leading text as default
sLeadingText = InputBox("Add leading text?", "Leading Text",
rkeyWord2)
'Write the chosen leading text to the registry
WSHShell.regwrite RegKey & "InvoiceLeadingText", sLeadingText
'Ask for the trailing text, whilst showing the
existing trailing text as default
sTrailingText = InputBox("Add trailing text?", "Trailing Text",
rkeyWord3)
'Write the chosen trailing text to the registry
WSHShell.regwrite RegKey & "InvoiceTrailingText", sTrailingText
'Setup done - Ensure user has a reminder to save
the template
If MsgBox("Now click 'OK' to save and
close the template" & vbCr & _
"to make the changes available to the documents" & vbCr & _
"or 'Cancel' and re-run setup", vbOKCancel) = vbOK Then
With ActiveDocument
.Save
.Close
End With
End If
Exit Sub
setUpError:
'If rKeyWord1 is empty then assign default values
If rkeyWord1 = ""
Then
WSHShell.regwrite RegKey & "InvoiceNo", 1
WSHShell.regwrite RegKey & "InvoiceLeadingText",
"Invoice No:"
WSHShell.regwrite RegKey & "InvoiceTrailingText", ""
iCount = 1
'and tell the
user what's happening
MsgBox "The registry entries have not yet been
created." _
& vbCr & "Default entries will be applied." & vbCr &
vbCr & _
"These can be changed and the start number reset" _
& vbCr & "from the Invoice Setup toolbar button." _
& vbCr & vbCr & "To access that, close this document
without saving" _
& vbCr & "and open the Invoice.dot template.", _
vbOKOnly, "Default Entries"
Resume
End If
End Sub
Sub AddSeqNumFromRegistry()
Dim BMRange As Range
Set WSHShell = CreateObject("WScript.Shell")
Set oBMs = ActiveDocument.Bookmarks
'Read the
registry entries
iCount = Val(WSHShell.RegRead(RegKey & "InvoiceNo"))
sLeadingText = WSHShell.RegRead(RegKey & "InvoiceLeadingText")
sTrailingText = WSHShell.RegRead(RegKey & "InvoiceTrailingText")
sCount = Format(iCount, "00000") 'Set the number
format to 5 digits
If sTrailingText <> ""
Then
'If there is
trailing text space it from the number
sTrailingText = " " & sTrailingText
End If
'Identify current Bookmark range and insert text
On Error GoTo AddNoError:
Set BMRange = oBMs("SeqNum").Range
BMRange.Text = sLeadingText & " " & sCount _
& sTrailingText 'Re-insert the bookmark
oBMs.Add "SeqNum", BMRange
ActiveDocument.Fields.Update
'Increment the number in the registry
WSHShell.regwrite RegKey & "InvoiceNo",
iCount + 1
Exit Sub
AddNoError:
If Err.Number = 5941 Then 'Bookmark is missing
MsgBox "The bookmark has been deleted. You must run" _
& " the Setup routine to redefine the number
position.", _
vbOKOnly, "Missing Bookmark!"
Exit Sub
End If
End Sub
Sub AutoNew()
On Error GoTo AutoNewError: 'No registry entries
:(
'Turn off the toolbar as this is only used when
editing the template
With CommandBars("Invoice Tools")
.Controls("Invoice Setup").Visible = False
.Controls("Insert Next Number").Visible =
False
.Controls("Inactive").Visible = True
.Visible = False
End With
ActiveDocument.AttachedTemplate.Save
AddSeqNumFromRegistry
Exit Sub
AutoNewError:
'Pop up a message to let the user know what's
happening
MsgBox "The registry entries have not yet been created." _
& vbCr & "Default entries will be applied." & vbCr & vbCr & _
"These can be changed and the start number reset" _
& vbCr & "from the Invoice Setup toolbar button." _
& vbCr & vbCr & "To access that, close this document without saving" _
& vbCr & "and open the Invoice.dot template.", _
vbOKOnly, "Default Entries"
'Write default entries to registry
WSHShell.regwrite RegKey & "InvoiceNo", 1
WSHShell.regwrite RegKey & "InvoiceLeadingText", "Invoice No:"
WSHShell.regwrite RegKey & "InvoiceTrailingText", ""
Resume 'and re-run the macro
End Sub
Sub NoAction() 'User has tried to operate the
macros from a document
MsgBox "The Invoice Tools are only available in the open template." &
vbCr & vbCr & _
"If this message shows and you already have the template open, close it"
& vbCr & _
"and then re-open it again", vbOKOnly, "Invoice Tools"
End Sub
Sub AutoOpen()
Dim oStr As String
oStr = LCase(ActiveDocument.Name)
If InStr(oStr, ".dot") <> 0
Then
If Not CommandBars("Invoice Tools").Visible
Then
CommandBars("Invoice Tools").Visible =
True
End If
With CommandBars("Invoice Tools")
.Controls("Invoice Setup").Visible =
True
.Controls("Insert Next Number").Visible =
True
.Controls("Inactive").Visible = False
End With
ActiveDocument.Save
End If
End Sub
Sub AutoClose() 'Recycles number if document
unsaved.
If ActiveDocument.Name Like "Document#*"
Then
If MsgBox("This invoice has not been saved. Do you want
to save" _
& " before closing?", vbYesNo, "Unsaved Invoice") =
vbYes Then
Application.Dialogs(wdDialogFileSaveAs).Show
Else
If MsgBox("The current number will be recycled.", _
vbOKCancel, "Recycle") = vbOK
Then
WSHShell.regwrite RegKey & "InvoiceNo", iCount
End If
ActiveDocument.Saved = True
ActiveDocument.AttachedTemplate.Saved = True
End If
End If
End Sub