PDA

View Full Version : [SOLVED:] Problems with Macro to insert barcode



askrates
06-08-2014, 11:47 PM
Hi Everyone,

I hope some kind soul can help. I am working on a macro in word to do two simple things:

1) Create an increasing serial number (at a bookmarked location) on a word document that increases by 1 for every copy printed
2) Prints the serial number as a barcode (at another bookmarked location). The number has to be deleted before moving on to the next page, or the paper will fill up with barcodes!

The first part works, but the second part doesn't. I cannot get it to print anything.

Any advice welcome

Public Sub CoverSheetNumber()
Dim Message As String, Title As String, Default As String, NumCopies As Long
Dim Rng1 As Range
Dim StrCode As String


' Set prompt.
Message = "Enter number of sheets"
' Set title.
Title = "Print"
' Set default.
Default = "1"


' Display message, title, and default value.
NumCopies = Val(InputBox(Message, Title, Default))
SerialNumber = 1


If SerialNumber = "" Then
SerialNumber = 1
End If


'Set ranges (bookmarked places in document)


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


Set Rng2 = ActiveDocument.Bookmarks("B2").Range
Counter = 0


'Insert values and print


While Counter < NumCopies
Rng1.Delete
Rng2.Delete
Rng1.Text = SerialNumber
Rng2.Text = "*2014-" & SerialNumber & "*"
Rng2.Collapse wdCollapseStart
Rng2.Font.Name = "3 of 9 Barcode"
Rng2.Font.Size = 28
ActiveDocument.PrintOut
SerialNumber = SerialNumber + 1
Counter = Counter + 1


Wend


'Save the bookmark ready for the next use.
With ActiveDocument.Bookmarks
.Add Name:="B1", Range:=Rng1
.Add Name:="B2", Range:=Rng2
End With


End Sub

macropod
06-09-2014, 05:35 PM
Your approach is over-engineered! For the macro, all you need is:

Sub CoverSheetNumber()
Application.ScreenUpdating = False
Dim lCount As Long, i As Long, j As Long
On Error GoTo Done
lCount = InputBox("How many copies to print?")
If lCount < 1 Then GoTo Done
For i = 1 To lCount
With ActiveDocument
.CustomDocumentProperties("Serial#").Value = _
.CustomDocumentProperties("Serial#").Value + 1
.Fields.Update
.PrintOut
End With
Next
Done:
Application.ScreenUpdating = True
End Sub
To use it:
1. Add a custom document property named 'Serial#' to the document, with a 0 value.
2. Where you want the number to appear, insert a DOCPROPERTY field, referencing the 'Serial#' property.
2. Where you want the barcode to appear, insert a DOCPROPERTY field, referencing the 'Serial#' property and:
• Format the 'D' of the DOCPROPERTY field code with the barcode font; and
• add a \* Charformat switch to the field.

The easiest way to create the DOCPROPERTY fields for this is to press Ctrl-F9 in the document to create a pair of field braces (i.e. { }) and type 'DOCPROPERTY Serial#' between the, so you end up with {DOCPROPERTY Serial#} and {DOCPROPERTY Serial# \* Charformat}, respectively.

With this approach, if you save the updated document after printing and later decide to print more copies, the new set will continue numbering from the last used #.

PS: When posting code, please use the code tags. They're indicated by the # button on the posting screen.

askrates
06-09-2014, 07:04 PM
Hi Paul,

Thank you so much for your help. I am a terrible coder, so that is why my initial try was so over-engineered.
As for your suggestion, I really like the idea of using a custom document property - much better than bookmarks.

One problem though. When I run the code, the value at the {DOCPROPERTY Serial#} is added twice, e.g. "11" rather than "1". I am not sure why this is.

Thanks again for your help!
Jan

macropod
06-09-2014, 08:04 PM
There are only two reasons I can think of for that:
1. You have two DOCPROPERTY fields at that location; or
2. The custom document property has that value.

gmaxey
06-09-2014, 08:59 PM
Providing you and your users will be using Word 2007 or higher, I personally prefer content controls. Here is a modification of Paul's code:


Sub CoverSheetNumber()
Application.ScreenUpdating = False
Dim lCount As Long, i As Long
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("Serial Number").Item(1)
If oCC.ShowingPlaceholderText Then oCC.Range.Text = 0
On Error GoTo Done
lCount = InputBox("How many copies to print?")
If lCount < 1 Then GoTo Done
For i = 1 To lCount
oCC.Range.Text = Val(oCC.Range.Text) + 1
ActiveDocument.PrintOut
Next
Done:
Application.ScreenUpdating = True
End Sub

macropod
06-09-2014, 09:05 PM
Hi Greg,

Output needs to go to two destinations - one in plain text, the other as a barcode...

askrates
06-09-2014, 11:06 PM
Hi Greg and Paul,

Thanks for your help. I got it working. There were indeed two DOCPROPERTY fields at the destination.

Thanks again!!
Jan

gmaxey
06-10-2014, 05:04 AM
Paul,
Yes I should read the question closer :-P. Well since it is solved there is no point updating the code. Thanks.