Using Excel- VBA to populate a table in Word and modify the color of a particular word in each cell The Next CEO of Stack OverflowHow can I replace a Microsoft Word character style within a range/selection in VBA?Populate table in Word Template with VBA?Excel Listobject Table Insert Rows Causes Cell Formula ErrorWord VBA: Error “The requested member of the collection does not exist” for a table cell that really does existVBA Word Changing table row and column size inGraph portion of Excel table in Word with a macroWrite and Style In LoopWord VBA - Applying alternate font color via styles does not work with text that has direct formatting (of font color) appliedExcel VBA - Populate Avery TemplatesCopying tables from excel to word VBA
Why am I allowed to create multiple unique pointers from a single object?
What is the result of assigning to std::vector<T>::begin()?
Is there a way to save my career from absolute disaster?
Return the Closest Prime Number
What's the best way to handle refactoring a big file?
Sending manuscript to multiple publishers
Why do remote companies require working in the US?
If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?
sp_blitzCache results Memory grants
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?
Are there any unintended negative consequences to allowing PCs to gain multiple levels at once in a short milestone-XP game?
How powerful is the invisibility granted by the Gloom Stalker ranger's Umbral Sight feature?
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Anatomically Correct Strange Women In Ponds Distributing Swords
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
If the heap is initialized for security, then why is the stack uninitialized?
If a black hole is created from light, can this black hole then move at speed of light?
How did people program for Consoles with multiple CPUs?
Why didn't Khan get resurrected in the Genesis Explosion?
Inappropriate reference requests from Journal reviewers
Which kind of appliances can one connect to electric sockets located in a airplane's toilet?
What benefits would be gained by using human laborers instead of drones in deep sea mining?
What was the first Unix version to run on a microcomputer?
Interfacing a button to MCU (and PC) with 50m long cable
Using Excel- VBA to populate a table in Word and modify the color of a particular word in each cell
The Next CEO of Stack OverflowHow can I replace a Microsoft Word character style within a range/selection in VBA?Populate table in Word Template with VBA?Excel Listobject Table Insert Rows Causes Cell Formula ErrorWord VBA: Error “The requested member of the collection does not exist” for a table cell that really does existVBA Word Changing table row and column size inGraph portion of Excel table in Word with a macroWrite and Style In LoopWord VBA - Applying alternate font color via styles does not work with text that has direct formatting (of font color) appliedExcel VBA - Populate Avery TemplatesCopying tables from excel to word VBA
I have an array that has stored 6 pieces of information, one of which is storage conditions. There are four different storage conditions (-80C, RT, 2-8, -20C). I am iterating through the array and then populating an Avery Label Template, which is a table. I can populate the label template just fine, but I want to change the font/color of the storage condition word. I am having trouble isolating that particular element of the cell.
Here is what i have:
'populating cell with array containing information
.Cell(r, c).Range.Text = arrEl(x, 0) & " "& arrEl(x, 1) & " "& arrEl(x, 2)
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
'Changing font and style of each cell
With .Cell(r, c).Range
.Font.Name = "Times New Roman"
.Font.Size = 8
Debug.Print .Paragraphs(2).Range.Words.Last
'I've tried .Words.Count to tell he how many words
'I've tried .Words(1)
End With
I am using Debug.Print to locate and isolate the storage condition (i.e. arrEl(x, 5) once it is in the cell in Word. I am encountering two problems:
- .Words.Last is displaying the end-of-cell character ¤ even though the last word would hopefully read one of four storage conditions (-80C, RT, 2-8, -20C).
- When I place the storage condition as the first word in the second paragraph I get incomplete portion of the storage condition. For example, only the "-" when I would like to have "-80" or only the "2" when I would like to have "2-8".
Any suggestions?
excel vba ms-word word-vba
add a comment |
I have an array that has stored 6 pieces of information, one of which is storage conditions. There are four different storage conditions (-80C, RT, 2-8, -20C). I am iterating through the array and then populating an Avery Label Template, which is a table. I can populate the label template just fine, but I want to change the font/color of the storage condition word. I am having trouble isolating that particular element of the cell.
Here is what i have:
'populating cell with array containing information
.Cell(r, c).Range.Text = arrEl(x, 0) & " "& arrEl(x, 1) & " "& arrEl(x, 2)
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
'Changing font and style of each cell
With .Cell(r, c).Range
.Font.Name = "Times New Roman"
.Font.Size = 8
Debug.Print .Paragraphs(2).Range.Words.Last
'I've tried .Words.Count to tell he how many words
'I've tried .Words(1)
End With
I am using Debug.Print to locate and isolate the storage condition (i.e. arrEl(x, 5) once it is in the cell in Word. I am encountering two problems:
- .Words.Last is displaying the end-of-cell character ¤ even though the last word would hopefully read one of four storage conditions (-80C, RT, 2-8, -20C).
- When I place the storage condition as the first word in the second paragraph I get incomplete portion of the storage condition. For example, only the "-" when I would like to have "-80" or only the "2" when I would like to have "2-8".
Any suggestions?
excel vba ms-word word-vba
Is your goal to color code each storage condition on the printed labels? If so could you use Word's Find/Replace once for each storage condition? E.g., Find "-80C" and Replace with [BLUE] "-80C". Find "RT" and Replace with [GREEN] "RT".
– xidgel
Mar 21 at 17:39
add a comment |
I have an array that has stored 6 pieces of information, one of which is storage conditions. There are four different storage conditions (-80C, RT, 2-8, -20C). I am iterating through the array and then populating an Avery Label Template, which is a table. I can populate the label template just fine, but I want to change the font/color of the storage condition word. I am having trouble isolating that particular element of the cell.
Here is what i have:
'populating cell with array containing information
.Cell(r, c).Range.Text = arrEl(x, 0) & " "& arrEl(x, 1) & " "& arrEl(x, 2)
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
'Changing font and style of each cell
With .Cell(r, c).Range
.Font.Name = "Times New Roman"
.Font.Size = 8
Debug.Print .Paragraphs(2).Range.Words.Last
'I've tried .Words.Count to tell he how many words
'I've tried .Words(1)
End With
I am using Debug.Print to locate and isolate the storage condition (i.e. arrEl(x, 5) once it is in the cell in Word. I am encountering two problems:
- .Words.Last is displaying the end-of-cell character ¤ even though the last word would hopefully read one of four storage conditions (-80C, RT, 2-8, -20C).
- When I place the storage condition as the first word in the second paragraph I get incomplete portion of the storage condition. For example, only the "-" when I would like to have "-80" or only the "2" when I would like to have "2-8".
Any suggestions?
excel vba ms-word word-vba
I have an array that has stored 6 pieces of information, one of which is storage conditions. There are four different storage conditions (-80C, RT, 2-8, -20C). I am iterating through the array and then populating an Avery Label Template, which is a table. I can populate the label template just fine, but I want to change the font/color of the storage condition word. I am having trouble isolating that particular element of the cell.
Here is what i have:
'populating cell with array containing information
.Cell(r, c).Range.Text = arrEl(x, 0) & " "& arrEl(x, 1) & " "& arrEl(x, 2)
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
'Changing font and style of each cell
With .Cell(r, c).Range
.Font.Name = "Times New Roman"
.Font.Size = 8
Debug.Print .Paragraphs(2).Range.Words.Last
'I've tried .Words.Count to tell he how many words
'I've tried .Words(1)
End With
I am using Debug.Print to locate and isolate the storage condition (i.e. arrEl(x, 5) once it is in the cell in Word. I am encountering two problems:
- .Words.Last is displaying the end-of-cell character ¤ even though the last word would hopefully read one of four storage conditions (-80C, RT, 2-8, -20C).
- When I place the storage condition as the first word in the second paragraph I get incomplete portion of the storage condition. For example, only the "-" when I would like to have "-80" or only the "2" when I would like to have "2-8".
Any suggestions?
excel vba ms-word word-vba
excel vba ms-word word-vba
edited Mar 21 at 17:07
Astro
asked Mar 21 at 15:41
AstroAstro
64
64
Is your goal to color code each storage condition on the printed labels? If so could you use Word's Find/Replace once for each storage condition? E.g., Find "-80C" and Replace with [BLUE] "-80C". Find "RT" and Replace with [GREEN] "RT".
– xidgel
Mar 21 at 17:39
add a comment |
Is your goal to color code each storage condition on the printed labels? If so could you use Word's Find/Replace once for each storage condition? E.g., Find "-80C" and Replace with [BLUE] "-80C". Find "RT" and Replace with [GREEN] "RT".
– xidgel
Mar 21 at 17:39
Is your goal to color code each storage condition on the printed labels? If so could you use Word's Find/Replace once for each storage condition? E.g., Find "-80C" and Replace with [BLUE] "-80C". Find "RT" and Replace with [GREEN] "RT".
– xidgel
Mar 21 at 17:39
Is your goal to color code each storage condition on the printed labels? If so could you use Word's Find/Replace once for each storage condition? E.g., Find "-80C" and Replace with [BLUE] "-80C". Find "RT" and Replace with [GREEN] "RT".
– xidgel
Mar 21 at 17:39
add a comment |
2 Answers
2
active
oldest
votes
With Word VBA the following identifies the text in a table's cell:
ActiveDocument.Tables(1).Cell(1,1).Range.Text
You can change the font characteristics of a word in a cell with:
ActiveDocument.Tables(1).Cell(1,1).Range.Words(2).Font.ColorIndex = 3
or use the following for the entire text:
ActiveDocument.Tables(1).Cell(1,1).Range.Font.ColorIndex = 3
If coding this from Excel then check that you are referencing Word, and its table(s), as appropriate.
You can modify, if needed, the text in a cell with
ActiveDocument.Tables(1).Cell(1,1).Range.Text = "foo bar"
You can get the last word with
ActiveDocument.Tables(1).Cell(1,1).Range.Words( _
ActiveDocument.Tables(1).Cell(1,1).Range.Words.Count - 1)
although this can be simplified with intermediate references. Count - 1
is ignoring the ending cell marker (or whatever it is called).
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
You can store the entire Text in a variable and then manipulate this variable. You can useSplit
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.
– Andy G
Mar 22 at 8:48
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
add a comment |
You say that the storage condition is in 'arrEl(x, 5)'
If this is so you need to change this line
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
to
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text & arrEl(x, 3) & " "+ arrEl(x, 4) & " "
Set my_range = .cell(r,c).range
my_range.collapse direction:=wdcollapseend
.Cell(r,c).range.text = .Cell(r,c).range.text & arrEl(x, 5)
my_range.end = .Cell(r,c).range.end
At this point my_range should include only the text from arrEl(x,5) so you can now apply the font formatting you desire to my_range.
You will of course need to insert a Dim my_range as Word.range in your code
Edit
To ensure we don't include any end of cell or paragraph markers in the range we can use the .MoveEndUntil method. In the range examples provided the set of characters that denote the end of the rage are "CT8". Thus we can do
my_range.MoveEndUntil cset:="CT8", count:=wdbackwards
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284170%2fusing-excel-vba-to-populate-a-table-in-word-and-modify-the-color-of-a-particula%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With Word VBA the following identifies the text in a table's cell:
ActiveDocument.Tables(1).Cell(1,1).Range.Text
You can change the font characteristics of a word in a cell with:
ActiveDocument.Tables(1).Cell(1,1).Range.Words(2).Font.ColorIndex = 3
or use the following for the entire text:
ActiveDocument.Tables(1).Cell(1,1).Range.Font.ColorIndex = 3
If coding this from Excel then check that you are referencing Word, and its table(s), as appropriate.
You can modify, if needed, the text in a cell with
ActiveDocument.Tables(1).Cell(1,1).Range.Text = "foo bar"
You can get the last word with
ActiveDocument.Tables(1).Cell(1,1).Range.Words( _
ActiveDocument.Tables(1).Cell(1,1).Range.Words.Count - 1)
although this can be simplified with intermediate references. Count - 1
is ignoring the ending cell marker (or whatever it is called).
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
You can store the entire Text in a variable and then manipulate this variable. You can useSplit
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.
– Andy G
Mar 22 at 8:48
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
add a comment |
With Word VBA the following identifies the text in a table's cell:
ActiveDocument.Tables(1).Cell(1,1).Range.Text
You can change the font characteristics of a word in a cell with:
ActiveDocument.Tables(1).Cell(1,1).Range.Words(2).Font.ColorIndex = 3
or use the following for the entire text:
ActiveDocument.Tables(1).Cell(1,1).Range.Font.ColorIndex = 3
If coding this from Excel then check that you are referencing Word, and its table(s), as appropriate.
You can modify, if needed, the text in a cell with
ActiveDocument.Tables(1).Cell(1,1).Range.Text = "foo bar"
You can get the last word with
ActiveDocument.Tables(1).Cell(1,1).Range.Words( _
ActiveDocument.Tables(1).Cell(1,1).Range.Words.Count - 1)
although this can be simplified with intermediate references. Count - 1
is ignoring the ending cell marker (or whatever it is called).
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
You can store the entire Text in a variable and then manipulate this variable. You can useSplit
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.
– Andy G
Mar 22 at 8:48
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
add a comment |
With Word VBA the following identifies the text in a table's cell:
ActiveDocument.Tables(1).Cell(1,1).Range.Text
You can change the font characteristics of a word in a cell with:
ActiveDocument.Tables(1).Cell(1,1).Range.Words(2).Font.ColorIndex = 3
or use the following for the entire text:
ActiveDocument.Tables(1).Cell(1,1).Range.Font.ColorIndex = 3
If coding this from Excel then check that you are referencing Word, and its table(s), as appropriate.
You can modify, if needed, the text in a cell with
ActiveDocument.Tables(1).Cell(1,1).Range.Text = "foo bar"
You can get the last word with
ActiveDocument.Tables(1).Cell(1,1).Range.Words( _
ActiveDocument.Tables(1).Cell(1,1).Range.Words.Count - 1)
although this can be simplified with intermediate references. Count - 1
is ignoring the ending cell marker (or whatever it is called).
With Word VBA the following identifies the text in a table's cell:
ActiveDocument.Tables(1).Cell(1,1).Range.Text
You can change the font characteristics of a word in a cell with:
ActiveDocument.Tables(1).Cell(1,1).Range.Words(2).Font.ColorIndex = 3
or use the following for the entire text:
ActiveDocument.Tables(1).Cell(1,1).Range.Font.ColorIndex = 3
If coding this from Excel then check that you are referencing Word, and its table(s), as appropriate.
You can modify, if needed, the text in a cell with
ActiveDocument.Tables(1).Cell(1,1).Range.Text = "foo bar"
You can get the last word with
ActiveDocument.Tables(1).Cell(1,1).Range.Words( _
ActiveDocument.Tables(1).Cell(1,1).Range.Words.Count - 1)
although this can be simplified with intermediate references. Count - 1
is ignoring the ending cell marker (or whatever it is called).
edited Mar 22 at 8:52
answered Mar 21 at 15:50
Andy GAndy G
17.2k53757
17.2k53757
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
You can store the entire Text in a variable and then manipulate this variable. You can useSplit
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.
– Andy G
Mar 22 at 8:48
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
add a comment |
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
You can store the entire Text in a variable and then manipulate this variable. You can useSplit
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.
– Andy G
Mar 22 at 8:48
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
Thank you Andy. The last suggestion definitely captured more of what I needed. However, in certain "words" it only captured a portion. For instance, 2-8 only displays 8, and -80 only displays 80. Is there a way to capture the entire expression (2-8, or -80). Can the 'delimiter' be a space? Or perhaps a different method instead of range.word?
– Astro
Mar 21 at 20:43
You can store the entire Text in a variable and then manipulate this variable. You can use
Split
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.– Andy G
Mar 22 at 8:48
You can store the entire Text in a variable and then manipulate this variable. You can use
Split
to split it. If you need to format part of the text which doesn't qualify as a "word" then you'll need to try and locate the specific Range of Characters that you want to format.– Andy G
Mar 22 at 8:48
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
Just omit the phrase .Words(2) in my code to format the entire text of a cell.
– Andy G
Mar 22 at 8:53
add a comment |
You say that the storage condition is in 'arrEl(x, 5)'
If this is so you need to change this line
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
to
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text & arrEl(x, 3) & " "+ arrEl(x, 4) & " "
Set my_range = .cell(r,c).range
my_range.collapse direction:=wdcollapseend
.Cell(r,c).range.text = .Cell(r,c).range.text & arrEl(x, 5)
my_range.end = .Cell(r,c).range.end
At this point my_range should include only the text from arrEl(x,5) so you can now apply the font formatting you desire to my_range.
You will of course need to insert a Dim my_range as Word.range in your code
Edit
To ensure we don't include any end of cell or paragraph markers in the range we can use the .MoveEndUntil method. In the range examples provided the set of characters that denote the end of the rage are "CT8". Thus we can do
my_range.MoveEndUntil cset:="CT8", count:=wdbackwards
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
add a comment |
You say that the storage condition is in 'arrEl(x, 5)'
If this is so you need to change this line
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
to
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text & arrEl(x, 3) & " "+ arrEl(x, 4) & " "
Set my_range = .cell(r,c).range
my_range.collapse direction:=wdcollapseend
.Cell(r,c).range.text = .Cell(r,c).range.text & arrEl(x, 5)
my_range.end = .Cell(r,c).range.end
At this point my_range should include only the text from arrEl(x,5) so you can now apply the font formatting you desire to my_range.
You will of course need to insert a Dim my_range as Word.range in your code
Edit
To ensure we don't include any end of cell or paragraph markers in the range we can use the .MoveEndUntil method. In the range examples provided the set of characters that denote the end of the rage are "CT8". Thus we can do
my_range.MoveEndUntil cset:="CT8", count:=wdbackwards
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
add a comment |
You say that the storage condition is in 'arrEl(x, 5)'
If this is so you need to change this line
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
to
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text & arrEl(x, 3) & " "+ arrEl(x, 4) & " "
Set my_range = .cell(r,c).range
my_range.collapse direction:=wdcollapseend
.Cell(r,c).range.text = .Cell(r,c).range.text & arrEl(x, 5)
my_range.end = .Cell(r,c).range.end
At this point my_range should include only the text from arrEl(x,5) so you can now apply the font formatting you desire to my_range.
You will of course need to insert a Dim my_range as Word.range in your code
Edit
To ensure we don't include any end of cell or paragraph markers in the range we can use the .MoveEndUntil method. In the range examples provided the set of characters that denote the end of the rage are "CT8". Thus we can do
my_range.MoveEndUntil cset:="CT8", count:=wdbackwards
You say that the storage condition is in 'arrEl(x, 5)'
If this is so you need to change this line
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text + arrEl(x, 3) + " "+ arrEl(x, 4) + " "+ arrEl(x, 5)
to
.Cell(r, c).Range.Text =.Cell(r,c).Range.Text & arrEl(x, 3) & " "+ arrEl(x, 4) & " "
Set my_range = .cell(r,c).range
my_range.collapse direction:=wdcollapseend
.Cell(r,c).range.text = .Cell(r,c).range.text & arrEl(x, 5)
my_range.end = .Cell(r,c).range.end
At this point my_range should include only the text from arrEl(x,5) so you can now apply the font formatting you desire to my_range.
You will of course need to insert a Dim my_range as Word.range in your code
Edit
To ensure we don't include any end of cell or paragraph markers in the range we can use the .MoveEndUntil method. In the range examples provided the set of characters that denote the end of the rage are "CT8". Thus we can do
my_range.MoveEndUntil cset:="CT8", count:=wdbackwards
edited Mar 22 at 8:54
answered Mar 21 at 17:04
FreeflowFreeflow
593129
593129
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
add a comment |
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
I think this is the way to go. However, there is still one problem. when i try Debug.Print my_range.Words(1) it prints out the end-of-cell symbol that kinda looks like this: [] . Also, my_range.Words Count displays 1. Why is it picking up that symbol?
– Astro
Mar 21 at 20:26
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
@Astro I edited my answer to add information of the MoveEndUntil range method.
– Freeflow
Mar 22 at 8:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284170%2fusing-excel-vba-to-populate-a-table-in-word-and-modify-the-color-of-a-particula%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is your goal to color code each storage condition on the printed labels? If so could you use Word's Find/Replace once for each storage condition? E.g., Find "-80C" and Replace with [BLUE] "-80C". Find "RT" and Replace with [GREEN] "RT".
– xidgel
Mar 21 at 17:39