Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-31-2014, 10:49 AM
elh52 elh52 is offline Macro to loop through all cells in a table produced by a mail merge Mac OS X Macro to loop through all cells in a table produced by a mail merge Office for Mac 2011
Novice
Macro to loop through all cells in a table produced by a mail merge
 
Join Date: Aug 2014
Posts: 3
elh52 is on a distinguished road
Question Macro to loop through all cells in a table produced by a mail merge

Hi all,

I'm very new indeed to macros so this may be a very simple question - if so, I'm sorry! I've carried out a mail merge on some data to make labels, and I now want to turn the first line of each label into a barcode. I've got a macro that will make the barcode, but instead of looping through the cells in the table it runs on the first cell over and over again. Can anyone help to diagnose what I've done wrong? The code is:

Sub BarcodeLabels()



Dim oRow As Row
Dim oCell As Cell
Dim oTbl As Table

For Each oTbl In ActiveDocument.Tables
For Each oRow In oTbl.Rows
For Each oCell In oRow.Cells

Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:="*"
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:="* "
Selection.Expand wdLine
Selection.Font.Name = "Free 3 of 9"
Selection.Font.Size = 14
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.Font.Name = "Arial"
Selection.EndKey Unit:=wdLine
Next oCell
Next oRow
Next oTbl

End Sub


Thank you for any help you can give!
Reply With Quote
  #2  
Old 08-31-2014, 11:50 AM
elh52 elh52 is offline Macro to loop through all cells in a table produced by a mail merge Mac OS X Macro to loop through all cells in a table produced by a mail merge Office for Mac 2011
Novice
Macro to loop through all cells in a table produced by a mail merge
 
Join Date: Aug 2014
Posts: 3
elh52 is on a distinguished road
Default

I've now got to this:





Sub BarcodeLabels()

Dim C As Cell
Dim objTable As Table

For Each objTable In ActiveDocument.Tables
For Each C In ActiveDocument.Tables(1).Range.Cells

Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:="*"
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:="* "
Selection.Expand wdLine
Selection.Font.Name = "Free 3 of 9"
Selection.Font.Size = 14
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.Font.Name = "Arial"
Selection.EndKey Unit:=wdLine
Selection.Move Unit:=wdCell, Count:=1
Next C

Next objTable

End Sub

Which runs through the first page of labels quite happily, and then loops in the last cell in the table without moving on to the next page (which I presume is a separate table). Any ideas? I'd be very grateful for any help!
Reply With Quote
  #3  
Old 08-31-2014, 02:37 PM
macropod's Avatar
macropod macropod is offline Macro to loop through all cells in a table produced by a mail merge Windows 7 64bit Macro to loop through all cells in a table produced by a mail merge Office 2010 32bit
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

Since you're using mailmerge, why aren't you applying the barcoding at that time, rather than afterwards? It's a trivial exercise to do it that way and requires no macros.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 08-31-2014, 10:39 PM
elh52 elh52 is offline Macro to loop through all cells in a table produced by a mail merge Mac OS X Macro to loop through all cells in a table produced by a mail merge Office for Mac 2011
Novice
Macro to loop through all cells in a table produced by a mail merge
 
Join Date: Aug 2014
Posts: 3
elh52 is on a distinguished road
Default

Hi macropod, thanks for the reply.

I did try that initially, but I'm no expert with mail merge either so it may be that I didn't do it right! The problem I had was that if I told the mail merge to use the barcode font for that line, the end of line formatting character in Word gets included in the barcode font as well and the barcode won't scan. That's why I have three spaces in Arial added in after the last asterisk in the macro below, as this means the formatting character is in Arial as well and is invisible. I'm sure it's not the most elegant solution but it works! I tried to tell the mail merge to add those three Arial spaces after the asterisk but every time I copied the format across to the other cells in the mail merge they would disappear or turn into barcode font, meaning again that it wouldn't scan. If you're able to explain how I could make scannable barcode with a mail merge that would be brilliant! I don't really mind how I arrive at the solution as long as I get there
Reply With Quote
  #5  
Old 08-31-2014, 10:50 PM
macropod's Avatar
macropod macropod is offline Macro to loop through all cells in a table produced by a mail merge Windows 7 64bit Macro to loop through all cells in a table produced by a mail merge Office 2010 32bit
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

From what I can tell from your macro code, the start/stop asterisks are not included in the data source. In that case, you need a field coded as:
{QUOTE "*{MERGEFIELD barcode}*" \* Charformat}
or:
{QUOTE "*«barcode»*" \* Charformat}
where 'barcode' is you mailmerge field's name and with just the 'Q' in 'QUOTE' formatted in the 3of9 barcode font & point size that you require.

Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues. Likewise, you can't type or copy & paste the chevrons (i.e. '« »') - they're part of the actual mergefields, which you can insert from the mailmerge toolbar.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to loop through all cells in a table produced by a mail merge Mail Merge on Optional Row(s) of a Table Chris Wong Mail Merge 3 08-21-2019 12:55 PM
Macro to loop through all cells in a table produced by a mail merge Mail Merge Into Dynamic Table Jag618 Mail Merge 1 03-04-2013 11:26 PM
Macro to loop through all cells in a table produced by a mail merge mail merge with table nadja Mail Merge 5 03-06-2012 05:41 PM
Macro to loop through all cells in a table produced by a mail merge Macro to Merge Cells Row By Row in a Table KD999 Word VBA 4 02-20-2012 08:51 PM
Macro to loop through all cells in a table produced by a mail merge Word Doc Macro (mail Merge) ajolson1964 Word VBA 1 05-10-2011 10:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:47 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