Excel 2016 VBA - Creating labels and transferring that to a Word document with a button? [closed]









up vote
0
down vote

favorite












edit
The purpose of this project was to use 1 application to list off a single customer and anywhere from 1-100 serial numbers with barcodes. The data that is in the spreadsheet would then populate a word document (via command button) populate the appropriate sections and print.



I hope this makes things a little more clear.



I am trying to figure out a way to create an excel spreadsheet that contains all of the information and with a command button send the data to word and populate all of the designated spots.



I will include some photos for reference, I did not include all of the serial number/ barcode code because it is the exact same code repeated multiple times.



Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Add("This is my file name", , False)

With wDoc
.Application.Selection.Find.Text = "<<Customer>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A2")

.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Assembly>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B2")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<PO>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Quantity>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E6")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F6")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E7")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F7")
.Application.Selection.EndOf



.SaveAs2 Filename:=("file name goes here"), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With

End Sub


This is the word document that I want to transfer all the data to. It is a template.



This is the excel data that I want to transfer










share|improve this question















closed as unclear what you're asking by Mathieu Guindon, Jaba, Billal Begueradj, greg-449, Shiladitya Nov 10 at 11:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Any reason you can't use Word's mail merge functionality?
    – Cindy Meister
    Nov 10 at 6:33










  • I tried using mailmerger, however the instructions were to populate 1 application and automate the printing process to another application. It got really messy when using mail merger with the macros and vb. They also wanted there to be a header and some basic information on the customer.
    – Tony Arashiro
    Nov 11 at 5:20














up vote
0
down vote

favorite












edit
The purpose of this project was to use 1 application to list off a single customer and anywhere from 1-100 serial numbers with barcodes. The data that is in the spreadsheet would then populate a word document (via command button) populate the appropriate sections and print.



I hope this makes things a little more clear.



I am trying to figure out a way to create an excel spreadsheet that contains all of the information and with a command button send the data to word and populate all of the designated spots.



I will include some photos for reference, I did not include all of the serial number/ barcode code because it is the exact same code repeated multiple times.



Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Add("This is my file name", , False)

With wDoc
.Application.Selection.Find.Text = "<<Customer>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A2")

.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Assembly>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B2")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<PO>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Quantity>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E6")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F6")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E7")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F7")
.Application.Selection.EndOf



.SaveAs2 Filename:=("file name goes here"), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With

End Sub


This is the word document that I want to transfer all the data to. It is a template.



This is the excel data that I want to transfer










share|improve this question















closed as unclear what you're asking by Mathieu Guindon, Jaba, Billal Begueradj, greg-449, Shiladitya Nov 10 at 11:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Any reason you can't use Word's mail merge functionality?
    – Cindy Meister
    Nov 10 at 6:33










  • I tried using mailmerger, however the instructions were to populate 1 application and automate the printing process to another application. It got really messy when using mail merger with the macros and vb. They also wanted there to be a header and some basic information on the customer.
    – Tony Arashiro
    Nov 11 at 5:20












up vote
0
down vote

favorite









up vote
0
down vote

favorite











edit
The purpose of this project was to use 1 application to list off a single customer and anywhere from 1-100 serial numbers with barcodes. The data that is in the spreadsheet would then populate a word document (via command button) populate the appropriate sections and print.



I hope this makes things a little more clear.



I am trying to figure out a way to create an excel spreadsheet that contains all of the information and with a command button send the data to word and populate all of the designated spots.



I will include some photos for reference, I did not include all of the serial number/ barcode code because it is the exact same code repeated multiple times.



Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Add("This is my file name", , False)

With wDoc
.Application.Selection.Find.Text = "<<Customer>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A2")

.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Assembly>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B2")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<PO>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Quantity>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E6")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F6")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E7")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F7")
.Application.Selection.EndOf



.SaveAs2 Filename:=("file name goes here"), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With

End Sub


This is the word document that I want to transfer all the data to. It is a template.



This is the excel data that I want to transfer










share|improve this question















edit
The purpose of this project was to use 1 application to list off a single customer and anywhere from 1-100 serial numbers with barcodes. The data that is in the spreadsheet would then populate a word document (via command button) populate the appropriate sections and print.



I hope this makes things a little more clear.



I am trying to figure out a way to create an excel spreadsheet that contains all of the information and with a command button send the data to word and populate all of the designated spots.



I will include some photos for reference, I did not include all of the serial number/ barcode code because it is the exact same code repeated multiple times.



Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Add("This is my file name", , False)

With wDoc
.Application.Selection.Find.Text = "<<Customer>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A2")

.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Assembly>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B2")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<PO>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Quantity>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E6")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F6")
.Application.Selection.EndOf


.Application.Selection.Find.Text = "<<SerialNumber>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("E7")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Barcode>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("F7")
.Application.Selection.EndOf



.SaveAs2 Filename:=("file name goes here"), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With

End Sub


This is the word document that I want to transfer all the data to. It is a template.



This is the excel data that I want to transfer







excel vba ms-word






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 5:22

























asked Nov 9 at 20:44









Tony Arashiro

11




11




closed as unclear what you're asking by Mathieu Guindon, Jaba, Billal Begueradj, greg-449, Shiladitya Nov 10 at 11:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Mathieu Guindon, Jaba, Billal Begueradj, greg-449, Shiladitya Nov 10 at 11:13


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • Any reason you can't use Word's mail merge functionality?
    – Cindy Meister
    Nov 10 at 6:33










  • I tried using mailmerger, however the instructions were to populate 1 application and automate the printing process to another application. It got really messy when using mail merger with the macros and vb. They also wanted there to be a header and some basic information on the customer.
    – Tony Arashiro
    Nov 11 at 5:20
















  • Any reason you can't use Word's mail merge functionality?
    – Cindy Meister
    Nov 10 at 6:33










  • I tried using mailmerger, however the instructions were to populate 1 application and automate the printing process to another application. It got really messy when using mail merger with the macros and vb. They also wanted there to be a header and some basic information on the customer.
    – Tony Arashiro
    Nov 11 at 5:20















Any reason you can't use Word's mail merge functionality?
– Cindy Meister
Nov 10 at 6:33




Any reason you can't use Word's mail merge functionality?
– Cindy Meister
Nov 10 at 6:33












I tried using mailmerger, however the instructions were to populate 1 application and automate the printing process to another application. It got really messy when using mail merger with the macros and vb. They also wanted there to be a header and some basic information on the customer.
– Tony Arashiro
Nov 11 at 5:20




I tried using mailmerger, however the instructions were to populate 1 application and automate the printing process to another application. It got really messy when using mail merger with the macros and vb. They also wanted there to be a header and some basic information on the customer.
– Tony Arashiro
Nov 11 at 5:20












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Edited to add an option to replace all matching token instances, or only one.
For repeated tokens, this allows you to replace one at a time, using different values.



It's not clear what your barcodes are (font? graphic?) but you can likely follow the same approach as for the serial numbers.



NOTE - it's not clear to me that the order of replacements in the serialnumber-Barcode pairs will always be in sync: you will need to test to verify.



Sub PerformReplacements()

Dim wApp As Word.Application
Dim wDoc As Word.Document, c As Range

Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Open("C:UserstwilliamsDesktoptmp.docx", , False)

ReplaceToken wDoc, "<<Customer>>", Range("A2").Value
ReplaceToken wDoc, "<<Assembly>>", Range("B2").Value

'serialnumbers
For Each c In Range("E2:E10").Cells
If c.Value <> "" Then
ReplaceToken wDoc, "<<SerialNumber>>", c.Value, False '<< one replacement only
End If
Next c

wDoc.SaveAs2 Filename:="C:UserstwilliamsDesktoptmp2.docx", _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False

End Sub

'given a document and a token to replace, replace only one or all of the
' token instances with "newText"
Sub ReplaceToken(doc As Word.Document, token As String, newText As String, _
Optional replaceAll As Boolean = True)

doc.Application.Options.DefaultHighlightColorIndex = wdNoHighlight
With doc.Range.Find
.Text = token
.replacement.Text = newText
.replacement.ClearFormatting
.replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=IIf(replaceAll, wdReplaceAll, wdReplaceOne) '<<<<<edit
End With

End Sub


Note - it would be easier to manage this process if your worksheet also had the token names: you could then loop over the range and perform the replacements without having to hard-code the tokens into your VBA.






share|improve this answer






















  • I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
    – Tony Arashiro
    Nov 9 at 21:31










  • The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
    – Tony Arashiro
    Nov 9 at 21:39










  • Every line where? In Excel or in Word ?
    – Tim Williams
    Nov 9 at 21:52










  • In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
    – Tony Arashiro
    Nov 9 at 21:59










  • So you worked it out?
    – Tim Williams
    Nov 9 at 23:19

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Edited to add an option to replace all matching token instances, or only one.
For repeated tokens, this allows you to replace one at a time, using different values.



It's not clear what your barcodes are (font? graphic?) but you can likely follow the same approach as for the serial numbers.



NOTE - it's not clear to me that the order of replacements in the serialnumber-Barcode pairs will always be in sync: you will need to test to verify.



Sub PerformReplacements()

Dim wApp As Word.Application
Dim wDoc As Word.Document, c As Range

Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Open("C:UserstwilliamsDesktoptmp.docx", , False)

ReplaceToken wDoc, "<<Customer>>", Range("A2").Value
ReplaceToken wDoc, "<<Assembly>>", Range("B2").Value

'serialnumbers
For Each c In Range("E2:E10").Cells
If c.Value <> "" Then
ReplaceToken wDoc, "<<SerialNumber>>", c.Value, False '<< one replacement only
End If
Next c

wDoc.SaveAs2 Filename:="C:UserstwilliamsDesktoptmp2.docx", _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False

End Sub

'given a document and a token to replace, replace only one or all of the
' token instances with "newText"
Sub ReplaceToken(doc As Word.Document, token As String, newText As String, _
Optional replaceAll As Boolean = True)

doc.Application.Options.DefaultHighlightColorIndex = wdNoHighlight
With doc.Range.Find
.Text = token
.replacement.Text = newText
.replacement.ClearFormatting
.replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=IIf(replaceAll, wdReplaceAll, wdReplaceOne) '<<<<<edit
End With

End Sub


Note - it would be easier to manage this process if your worksheet also had the token names: you could then loop over the range and perform the replacements without having to hard-code the tokens into your VBA.






share|improve this answer






















  • I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
    – Tony Arashiro
    Nov 9 at 21:31










  • The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
    – Tony Arashiro
    Nov 9 at 21:39










  • Every line where? In Excel or in Word ?
    – Tim Williams
    Nov 9 at 21:52










  • In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
    – Tony Arashiro
    Nov 9 at 21:59










  • So you worked it out?
    – Tim Williams
    Nov 9 at 23:19














up vote
1
down vote













Edited to add an option to replace all matching token instances, or only one.
For repeated tokens, this allows you to replace one at a time, using different values.



It's not clear what your barcodes are (font? graphic?) but you can likely follow the same approach as for the serial numbers.



NOTE - it's not clear to me that the order of replacements in the serialnumber-Barcode pairs will always be in sync: you will need to test to verify.



Sub PerformReplacements()

Dim wApp As Word.Application
Dim wDoc As Word.Document, c As Range

Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Open("C:UserstwilliamsDesktoptmp.docx", , False)

ReplaceToken wDoc, "<<Customer>>", Range("A2").Value
ReplaceToken wDoc, "<<Assembly>>", Range("B2").Value

'serialnumbers
For Each c In Range("E2:E10").Cells
If c.Value <> "" Then
ReplaceToken wDoc, "<<SerialNumber>>", c.Value, False '<< one replacement only
End If
Next c

wDoc.SaveAs2 Filename:="C:UserstwilliamsDesktoptmp2.docx", _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False

End Sub

'given a document and a token to replace, replace only one or all of the
' token instances with "newText"
Sub ReplaceToken(doc As Word.Document, token As String, newText As String, _
Optional replaceAll As Boolean = True)

doc.Application.Options.DefaultHighlightColorIndex = wdNoHighlight
With doc.Range.Find
.Text = token
.replacement.Text = newText
.replacement.ClearFormatting
.replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=IIf(replaceAll, wdReplaceAll, wdReplaceOne) '<<<<<edit
End With

End Sub


Note - it would be easier to manage this process if your worksheet also had the token names: you could then loop over the range and perform the replacements without having to hard-code the tokens into your VBA.






share|improve this answer






















  • I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
    – Tony Arashiro
    Nov 9 at 21:31










  • The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
    – Tony Arashiro
    Nov 9 at 21:39










  • Every line where? In Excel or in Word ?
    – Tim Williams
    Nov 9 at 21:52










  • In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
    – Tony Arashiro
    Nov 9 at 21:59










  • So you worked it out?
    – Tim Williams
    Nov 9 at 23:19












up vote
1
down vote










up vote
1
down vote









Edited to add an option to replace all matching token instances, or only one.
For repeated tokens, this allows you to replace one at a time, using different values.



It's not clear what your barcodes are (font? graphic?) but you can likely follow the same approach as for the serial numbers.



NOTE - it's not clear to me that the order of replacements in the serialnumber-Barcode pairs will always be in sync: you will need to test to verify.



Sub PerformReplacements()

Dim wApp As Word.Application
Dim wDoc As Word.Document, c As Range

Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Open("C:UserstwilliamsDesktoptmp.docx", , False)

ReplaceToken wDoc, "<<Customer>>", Range("A2").Value
ReplaceToken wDoc, "<<Assembly>>", Range("B2").Value

'serialnumbers
For Each c In Range("E2:E10").Cells
If c.Value <> "" Then
ReplaceToken wDoc, "<<SerialNumber>>", c.Value, False '<< one replacement only
End If
Next c

wDoc.SaveAs2 Filename:="C:UserstwilliamsDesktoptmp2.docx", _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False

End Sub

'given a document and a token to replace, replace only one or all of the
' token instances with "newText"
Sub ReplaceToken(doc As Word.Document, token As String, newText As String, _
Optional replaceAll As Boolean = True)

doc.Application.Options.DefaultHighlightColorIndex = wdNoHighlight
With doc.Range.Find
.Text = token
.replacement.Text = newText
.replacement.ClearFormatting
.replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=IIf(replaceAll, wdReplaceAll, wdReplaceOne) '<<<<<edit
End With

End Sub


Note - it would be easier to manage this process if your worksheet also had the token names: you could then loop over the range and perform the replacements without having to hard-code the tokens into your VBA.






share|improve this answer














Edited to add an option to replace all matching token instances, or only one.
For repeated tokens, this allows you to replace one at a time, using different values.



It's not clear what your barcodes are (font? graphic?) but you can likely follow the same approach as for the serial numbers.



NOTE - it's not clear to me that the order of replacements in the serialnumber-Barcode pairs will always be in sync: you will need to test to verify.



Sub PerformReplacements()

Dim wApp As Word.Application
Dim wDoc As Word.Document, c As Range

Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Open("C:UserstwilliamsDesktoptmp.docx", , False)

ReplaceToken wDoc, "<<Customer>>", Range("A2").Value
ReplaceToken wDoc, "<<Assembly>>", Range("B2").Value

'serialnumbers
For Each c In Range("E2:E10").Cells
If c.Value <> "" Then
ReplaceToken wDoc, "<<SerialNumber>>", c.Value, False '<< one replacement only
End If
Next c

wDoc.SaveAs2 Filename:="C:UserstwilliamsDesktoptmp2.docx", _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False

End Sub

'given a document and a token to replace, replace only one or all of the
' token instances with "newText"
Sub ReplaceToken(doc As Word.Document, token As String, newText As String, _
Optional replaceAll As Boolean = True)

doc.Application.Options.DefaultHighlightColorIndex = wdNoHighlight
With doc.Range.Find
.Text = token
.replacement.Text = newText
.replacement.ClearFormatting
.replacement.Font.Italic = False
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=IIf(replaceAll, wdReplaceAll, wdReplaceOne) '<<<<<edit
End With

End Sub


Note - it would be easier to manage this process if your worksheet also had the token names: you could then loop over the range and perform the replacements without having to hard-code the tokens into your VBA.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 23:45

























answered Nov 9 at 21:09









Tim Williams

83.8k96481




83.8k96481











  • I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
    – Tony Arashiro
    Nov 9 at 21:31










  • The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
    – Tony Arashiro
    Nov 9 at 21:39










  • Every line where? In Excel or in Word ?
    – Tim Williams
    Nov 9 at 21:52










  • In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
    – Tony Arashiro
    Nov 9 at 21:59










  • So you worked it out?
    – Tim Williams
    Nov 9 at 23:19
















  • I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
    – Tony Arashiro
    Nov 9 at 21:31










  • The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
    – Tony Arashiro
    Nov 9 at 21:39










  • Every line where? In Excel or in Word ?
    – Tim Williams
    Nov 9 at 21:52










  • In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
    – Tony Arashiro
    Nov 9 at 21:59










  • So you worked it out?
    – Tim Williams
    Nov 9 at 23:19















I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
– Tony Arashiro
Nov 9 at 21:31




I am sorry about that, the problem is in the word image when the data is transferred over it does not transfer all the data, just in the first column of the table. And I will try this code out, I appreciate it.
– Tony Arashiro
Nov 9 at 21:31












The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
– Tony Arashiro
Nov 9 at 21:39




The other issue that I am having is that the data for <<serialnumbers>> is different for every line along with the <<Barcode>> so what you provided me fixed one problem, however it is only copying the first line and populating everything with that line
– Tony Arashiro
Nov 9 at 21:39












Every line where? In Excel or in Word ?
– Tim Williams
Nov 9 at 21:52




Every line where? In Excel or in Word ?
– Tim Williams
Nov 9 at 21:52












In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
– Tony Arashiro
Nov 9 at 21:59




In excel. ReplaceToken wDoc, "<<SerialNumber6>>", Range("E6").Value ReplaceToken wDoc, "<<Barcode6>>", Range("F6").Value ReplaceToken wDoc, "<<SerialNumber7>>", Range("E7").Value ReplaceToken wDoc, "<<Barcode7>>", Range("F7").Value ReplaceToken wDoc, "<<SerialNumber8>>", Range("E8").Value ReplaceToken wDoc, "<<Barcode8>>", Range("F8").Value ReplaceToken wDoc, "<<SerialNumber9>>", Range("E9").Value ReplaceToken wDoc, "<<Barcode9>>", Range("F9").Value
– Tony Arashiro
Nov 9 at 21:59












So you worked it out?
– Tim Williams
Nov 9 at 23:19




So you worked it out?
– Tim Williams
Nov 9 at 23:19



Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20