Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-07-2022, 01:58 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default accessing avery label table in macro

I downloaded an Avery Label Word template. I want to write a macro to fill in multiple labels, based on my choice with the same text.





I understand that the template is a table. How do I access the table in VBA so I can manipulate cell/row/column properties, etc?


Sorry -- I'm not a VBA expert, but this isn't as simple in Excel.
Reply With Quote
  #2  
Old 08-07-2022, 04:21 PM
Guessed's Avatar
Guessed Guessed is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

We don't know what kind of manipulations you require. Working with tables is different to Excel but I wouldn't say it is particularly complex. You can work at the Range, Table, Row, Column or Cell levels so we would need to know what it is you want to do to the table.

You can start by recording a macro of you using the GUI to make those manipulations. Once you have some starting code, you can post it here and someone can tidy it up for you.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 08-07-2022, 05:19 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

I want to copy the text from cell and paste it into other cells, as selected. I tried recording a macro, but when I copied the info from one cell, selected the next cell, and pasted, it just used an offset. And sometimes, the recorded macro didn't even run correctly. We are a nonprofit, and have a yearly raffle. For those who buy 20 or more tickets, we print out labels for them to paste on the tickets for the drawing, so they don't have to fill them out by hand. We use 1 sheet for several customers -- for instance I might have the first 20 going to one and another 25 labels going to another, etc. I would like to copy the data from cell into other selected cells. I could do this by using a loop, or using something like table.Cell(2,2).Range.Text = strTextCopied, but I can't seem to reference the table. I'd just like to try this as a learning experience. (TMI?)
Reply With Quote
  #4  
Old 08-07-2022, 05:19 PM
macropod's Avatar
macropod macropod is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Perhaps you should consider using mailmerge. No vba required.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-07-2022, 05:33 PM
Guessed's Avatar
Guessed Guessed is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Are you printing these pages out and cutting them up to put in to a barrel for the draw? It sounds awfully labour intensive and likely not fully random as there would be variation in paper slip sizes.

Have you considered using an electronic method to quickly store ticket holder names and do the randomised draw - I presume there are smartphone/ipad apps that do exactly this.

If you wanted to stay in Word and manually cut out paper, a macro could be written to ask the user (ticket seller) for how many tickets and repeat this that many times.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 08-07-2022, 05:54 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

The tickets have an area for the purchasers' info. To save the purchasers the hassle of filling them out manually, we give them a sheet of printed labels. They peel them off and stick them on the part of the tickets that go into the barrel. The randomness is the manual drawing of the tickets from the barrel. Using an electronic method would be scary -- if something went wrong, I don't know what we'd do. And we are licensed by our state to do this. We are a small nonprofit -- only volunteers -- no paid employees.
Reply With Quote
  #7  
Old 08-07-2022, 05:56 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

I've used mail marge once or twice -- not sure what the limitations of its capabilities are. More familiar with VBA.
Reply With Quote
  #8  
Old 08-07-2022, 06:23 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

Never mind-- I opened up the VBA editor in Word , and looked up "Tables" in the "Help", "Word Object Model Reference". It told me what I wanted to know.


Basically I can use:


strLabelText = ActiveDocument.Tables(1).Cell(1,1).Range.Text 'Copies the text
ActiveDocument.Tables(1).Cell(2,1).Range.Text= strLabelText 'Pastes the text into the cell below


This requires a lot more coding and cleaning up, but I could not figure out how to access the table. I have something to work with now.


Thanks again.
Reply With Quote
  #9  
Old 08-07-2022, 10:21 PM
Guessed's Avatar
Guessed Guessed is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Try this code. It assumes your cursor is in a cell you have already populated and then asks for how many tickets to add. It makes sure there are enough cells remaining in the table and then duplicates the current cell contents into the required cells.
Code:
Sub AddTickets()
  Dim i As Integer, iCount As Integer, celSrc As Cell, celAdd As Cell
  Dim rngCell As Range, rngToEnd As Range, aTbl As Table
  Set aTbl = Selection.Tables(1)
  Set celSrc = Selection.Cells(1)
  Set rngCell = celSrc.Range
  rngCell.MoveEnd Unit:=wdCharacter, Count:=-1
  Set rngToEnd = rngCell.Duplicate
  rngToEnd.End = aTbl.Range.End
  iCount = InputBox("How many tickets do I need?", "Ticket Copier", 2)
  If iCount > 1 Then
    Do While rngToEnd.Cells.Count < iCount
      aTbl.Rows.Add
      rngToEnd.End = aTbl.Range.End
    Loop
    Set celAdd = celSrc.Next
    For i = 2 To iCount
      celAdd.Range.FormattedText = rngCell
      Set celAdd = celAdd.Next
    Next i
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 08-09-2022, 12:10 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

Thanks --

I tried that, but got a runtime error 91 on the line

celAdd.Range.FormattedText = rngCell

It says: cel.Add.Range.FormattedText: = <Object variable or Width block variable not set>

rngCell did pick up the text from the first cell, so I'm not sure what's going on.

Anyway, my question was solved, and I am using a simpler macro to do what I want it to do. I will probably use an input box, or form for input in addition to my code, after I decide exactly what I want to do. My basic code to populate all the labels:

Sub Copy_Labels()
Dim strLabelText As String
Dim a As Long 'Row
Dim b As Long 'Column

Application.ScreenUpdating = False
strLabelText = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
For b = 1 To 7 Step 2
For a = 1 To 20
ActiveDocument.Tables(1).Cell(a, b).Range.Text = strLabelText
Next a
Next b
Application.ScreenUpdating = True

End Sub

I found out that in columns, there is a column of blank cells between each column of Avery Labels.
Also turned off screen updating, because the macro was going kind of slow.

Thank-you everyone for your input.
Reply With Quote
  #11  
Old 08-09-2022, 12:12 PM
littlepeaks littlepeaks is offline accessing avery label table in macro Windows 10 accessing avery label table in macro Office 2010 32bit
Novice
accessing avery label table in macro
 
Join Date: Jan 2012
Location: Colorado Springs, CO
Posts: 16
littlepeaks is on a distinguished road
Default

I'm sorry -- my forum response did not pick up the code formatting (indents, etc).
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving Avery Label 8376 fsinclaire Word 1 04-30-2022 09:12 AM
accessing avery label table in macro Accessing the format tab with the macro recorder reneemettrie Word VBA 6 01-10-2020 12:03 PM
accessing avery label table in macro Avery Label Templates, editing rows/spacing rainandwind_95448 Word 2 12-13-2015 08:05 AM
accessing avery label table in macro Avery Label Template for Word 2003? Clueless in Seattle Word 2 03-31-2015 06:15 AM
Having trouble using Avery 4014 label to print multiple pages Dickie Mail Merge 1 09-29-2013 07:28 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:41 AM.


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