Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-16-2019, 02:31 PM
limdul9992 limdul9992 is offline Having Word print sequential serial numbers in a text field Windows 10 Having Word print sequential serial numbers in a text field Office 2010
Novice
Having Word print sequential serial numbers in a text field
 
Join Date: Jul 2019
Posts: 5
limdul9992 is on a distinguished road
Default Having Word print sequential serial numbers in a text field

Hi Everyone,



So I am have a code that I got for Excel to print the serial numbers sequentially but I am wondering what I would have to change in the code to work with a text field called Serialnumber to do the same as it does for Excel. Here is the code. I am not very good with VBA so that is why I am coming here for help.

ub PrintJobs()
Dim i As Long, startnum As Long, lastnum As Long

startnum = Application.InputBox("Enter the first job number to be printed", "Print Job Number", 1, , , , , 1)
lastnum = Application.InputBox("Enter the last job number to be printed", "Print Job Number", 1, , , , , 1)

For i = startnum To lastnum
Range("D7").Value = i
ActiveWindow.SelectedSheets.PrintOut
Next

End Sub
Reply With Quote
  #2  
Old 08-16-2019, 08:23 PM
gmayor's Avatar
gmayor gmayor is offline Having Word print sequential serial numbers in a text field Windows 10 Having Word print sequential serial numbers in a text field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Insert a text content control where you want the number to be displayed and title it Startnumber then:

Code:
Sub PrintJobs()
Dim i As Long, startnum As Long, lastnum As Long

    startnum = InputBox("Enter the first job number to be printed", "Print Job Number", 1)
    lastnum = InputBox("Enter the last job number to be printed", "Print Job Number", 1)

    For i = startnum To lastnum
        ActiveDocument.SelectContentControlsByTitle("Serialnumber").Item(1).Range.Text = i
        ActiveDocument.PrintOut
    Next

End Sub
See also Auto number documents
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-20-2019, 06:23 AM
limdul9992 limdul9992 is offline Having Word print sequential serial numbers in a text field Windows 10 Having Word print sequential serial numbers in a text field Office 2010
Novice
Having Word print sequential serial numbers in a text field
 
Join Date: Jul 2019
Posts: 5
limdul9992 is on a distinguished road
Default

Hi Gmayor,

So I am trying the code and when I run it I get an error that states:

Run-time error 5941
The requested member of the collection does not exist.

I have put in a legacy text field and named it with Serialnumber and then what you said to name it as Startnumber and neither of them work and I get the same message. Is there another content control that I should be using? I am using word 2010 if that makes any difference.
Reply With Quote
  #4  
Old 08-20-2019, 06:30 AM
limdul9992 limdul9992 is offline Having Word print sequential serial numbers in a text field Windows 10 Having Word print sequential serial numbers in a text field Office 2010
Novice
Having Word print sequential serial numbers in a text field
 
Join Date: Jul 2019
Posts: 5
limdul9992 is on a distinguished road
Default

Well ignore my last post. I found which content control it needed to be. So now it works except for one thing. My serial numbers that I use will sometimes start with a 0 because I use the month and year as my first 4 numbers. However when I enter in say 0819xxxx it does not print the 0.Is there a way to make it so it will print the zero as well?

Also is there a way that I could create a command button on the word doc that would have this code in it to print of the serial numbers but to hide the command button when printing?

Last edited by limdul9992; 08-20-2019 at 09:05 AM. Reason: Added question about a command button.
Reply With Quote
  #5  
Old 08-20-2019, 08:23 PM
gmayor's Avatar
gmayor gmayor is offline Having Word print sequential serial numbers in a text field Windows 10 Having Word print sequential serial numbers in a text field Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following will add the data and the number with a leading zero

Code:
Sub PrintJobs()
Dim i As Long, startnum As Long, lastnum As Long
Dim orng As Range
    Set orng = ActiveDocument.SelectContentControlsByTitle("Serialnumber").Item(1).Range
    startnum = InputBox("Enter the first job number to be printed", "Print Job Number", 1)
    lastnum = InputBox("Enter the last job number to be printed", "Print Job Number", 1)
    For i = startnum To lastnum
        orng.Text = Format(Date, "mmyy") & Format(i, "00")
        ActiveDocument.PrintOut
    Next
    Set orng = Nothing
End Sub
Put the button on the QAT (Quick Access Toolbar) or on the ribbon Customize the Office Ribbon (It doesn't take rocket science)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com

Last edited by gmayor; 08-21-2019 at 08:19 PM.
Reply With Quote
  #6  
Old 08-21-2019, 06:23 AM
limdul9992 limdul9992 is offline Having Word print sequential serial numbers in a text field Windows 10 Having Word print sequential serial numbers in a text field Office 2010
Novice
Having Word print sequential serial numbers in a text field
 
Join Date: Jul 2019
Posts: 5
limdul9992 is on a distinguished road
Default Solved and works great

Thank you very much Gmayor. That works perfectly. Thank you very much for your help.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having Word print sequential serial numbers in a text field Looking for help with creating sequential serial number on one page 4ener Word 9 02-24-2019 07:50 AM
Sequential numbering in Word - Unique numbers for table rows krips Word 1 03-21-2018 05:06 AM
Having Word print sequential serial numbers in a text field Creating alphanumeric incremental serial numbers srinivasaru Excel Programming 8 04-03-2017 12:53 PM
Having Word print sequential serial numbers in a text field How to have sequential numbers appear in word header woodyear99 Word 1 07-18-2016 08:28 PM
Having Word print sequential serial numbers in a text field Increase serial number for every print atomtm Word 1 06-15-2012 05:39 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:06 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft