How to bold only a portion of Word paragraph in Visual Studio (VB) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Creating multiple Word paragraphs with Document.Paragraphs.Add()How do you display code snippets in MS Word preserving format and syntax highlighting?C# Word Interop, Pasting Clipboard into ParagraphHow do I write bold text to a Word document programmatically without bolding the entire document?How to get just a portion of the Word.Range.TextIs there a way to get the page number of the start of a paragraph with Applescript in Mac Word 2011?Macro to save word file using text string in last paragraphRe-defne a Paragraph Range to Include a Subset of Words in the ParagraphMicrosoft Word VBA moving through paragraphs and highlighting specific positions in each paragraphWord Interop - process headers and footers only onceAspose.Words - Format a single word in a paragraph
A letter with no particular backstory
How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?
Dyck paths with extra diagonals from valleys (Laser construction)
An adverb for when you're not exaggerating
Is there any word for a place full of confusion?
Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?
How much damage would a cupful of neutron star matter do to the Earth?
Can a new player join a group only when a new campaign starts?
What order were files/directories output in dir?
What does it mean that physics no longer uses mechanical models to describe phenomena?
Why weren't discrete x86 CPUs ever used in game hardware?
How do I find out the mythology and history of my Fortress?
What does Turing mean by this statement?
macOS: Name for app shortcut screen found by pinching with thumb and three fingers
What would you call this weird metallic apparatus that allows you to lift people?
How do living politicians protect their readily obtainable signatures from misuse?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?
preposition before coffee
How to identify unknown coordinate type and convert to lat/lon?
Why do early math courses focus on the cross sections of a cone and not on other 3D objects?
Do wooden building fires get hotter than 600°C?
One-one communication
Deconstruction is ambiguous
How to bold only a portion of Word paragraph in Visual Studio (VB)
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Creating multiple Word paragraphs with Document.Paragraphs.Add()How do you display code snippets in MS Word preserving format and syntax highlighting?C# Word Interop, Pasting Clipboard into ParagraphHow do I write bold text to a Word document programmatically without bolding the entire document?How to get just a portion of the Word.Range.TextIs there a way to get the page number of the start of a paragraph with Applescript in Mac Word 2011?Macro to save word file using text string in last paragraphRe-defne a Paragraph Range to Include a Subset of Words in the ParagraphMicrosoft Word VBA moving through paragraphs and highlighting specific positions in each paragraphWord Interop - process headers and footers only onceAspose.Words - Format a single word in a paragraph
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to export a Word document from a Visual Basic program. Different parts of the document will need different formatting.
I have several paragraphs, and I need to bold only portions of each of those paragraphs. I am trying to set the range within each paragraph that needs to be bolded, but no matter what I do, it only seems to want to to bold the entire paragraph.
I want to do something like this:
Dim Para1 As Word.Paragraph
Para1 = WordDoc.Content.Paragraphs.Add
Para1.Range.Start = 1
Para1.Range.End = 14
Para1.Range.Font.Bold = True
Para1.Range.Text = "Job number is: " + myJobID
... so that it bolds from the 'J' to the ':' (in Para1.Range.Text) but does not bold the myJobID (which is a variable I'm getting from the user). However, no matter what I do, it bolds the entire paragraph, including the myJobID.
I've also tried creating a Range variable that sets a range based on the entire document, but the problem with that is, the lengths of several variables I'm outputting on the Word document are going to be varying sizes, and thus there's no way to know where the start of the next section I want to bold will start at. So basically, I have to work within the Paragraph object rather than iterating through all of the characters in the entire document.
Hope that made sense. Any ideas?
vb.net ms-word bold
add a comment |
I am trying to export a Word document from a Visual Basic program. Different parts of the document will need different formatting.
I have several paragraphs, and I need to bold only portions of each of those paragraphs. I am trying to set the range within each paragraph that needs to be bolded, but no matter what I do, it only seems to want to to bold the entire paragraph.
I want to do something like this:
Dim Para1 As Word.Paragraph
Para1 = WordDoc.Content.Paragraphs.Add
Para1.Range.Start = 1
Para1.Range.End = 14
Para1.Range.Font.Bold = True
Para1.Range.Text = "Job number is: " + myJobID
... so that it bolds from the 'J' to the ':' (in Para1.Range.Text) but does not bold the myJobID (which is a variable I'm getting from the user). However, no matter what I do, it bolds the entire paragraph, including the myJobID.
I've also tried creating a Range variable that sets a range based on the entire document, but the problem with that is, the lengths of several variables I'm outputting on the Word document are going to be varying sizes, and thus there's no way to know where the start of the next section I want to bold will start at. So basically, I have to work within the Paragraph object rather than iterating through all of the characters in the entire document.
Hope that made sense. Any ideas?
vb.net ms-word bold
You set the bold property on an empty paragraph, then added the text. Do it the other way around.e.g add the paragraph, add the text, set the sub range then finally apply formatting to the sub range.
– Freeflow
Mar 9 at 7:07
add a comment |
I am trying to export a Word document from a Visual Basic program. Different parts of the document will need different formatting.
I have several paragraphs, and I need to bold only portions of each of those paragraphs. I am trying to set the range within each paragraph that needs to be bolded, but no matter what I do, it only seems to want to to bold the entire paragraph.
I want to do something like this:
Dim Para1 As Word.Paragraph
Para1 = WordDoc.Content.Paragraphs.Add
Para1.Range.Start = 1
Para1.Range.End = 14
Para1.Range.Font.Bold = True
Para1.Range.Text = "Job number is: " + myJobID
... so that it bolds from the 'J' to the ':' (in Para1.Range.Text) but does not bold the myJobID (which is a variable I'm getting from the user). However, no matter what I do, it bolds the entire paragraph, including the myJobID.
I've also tried creating a Range variable that sets a range based on the entire document, but the problem with that is, the lengths of several variables I'm outputting on the Word document are going to be varying sizes, and thus there's no way to know where the start of the next section I want to bold will start at. So basically, I have to work within the Paragraph object rather than iterating through all of the characters in the entire document.
Hope that made sense. Any ideas?
vb.net ms-word bold
I am trying to export a Word document from a Visual Basic program. Different parts of the document will need different formatting.
I have several paragraphs, and I need to bold only portions of each of those paragraphs. I am trying to set the range within each paragraph that needs to be bolded, but no matter what I do, it only seems to want to to bold the entire paragraph.
I want to do something like this:
Dim Para1 As Word.Paragraph
Para1 = WordDoc.Content.Paragraphs.Add
Para1.Range.Start = 1
Para1.Range.End = 14
Para1.Range.Font.Bold = True
Para1.Range.Text = "Job number is: " + myJobID
... so that it bolds from the 'J' to the ':' (in Para1.Range.Text) but does not bold the myJobID (which is a variable I'm getting from the user). However, no matter what I do, it bolds the entire paragraph, including the myJobID.
I've also tried creating a Range variable that sets a range based on the entire document, but the problem with that is, the lengths of several variables I'm outputting on the Word document are going to be varying sizes, and thus there's no way to know where the start of the next section I want to bold will start at. So basically, I have to work within the Paragraph object rather than iterating through all of the characters in the entire document.
Hope that made sense. Any ideas?
vb.net ms-word bold
vb.net ms-word bold
edited Mar 9 at 6:22
Cindy Meister
16.1k102537
16.1k102537
asked Mar 8 at 21:55
JohnAJohnA
162
162
You set the bold property on an empty paragraph, then added the text. Do it the other way around.e.g add the paragraph, add the text, set the sub range then finally apply formatting to the sub range.
– Freeflow
Mar 9 at 7:07
add a comment |
You set the bold property on an empty paragraph, then added the text. Do it the other way around.e.g add the paragraph, add the text, set the sub range then finally apply formatting to the sub range.
– Freeflow
Mar 9 at 7:07
You set the bold property on an empty paragraph, then added the text. Do it the other way around.e.g add the paragraph, add the text, set the sub range then finally apply formatting to the sub range.
– Freeflow
Mar 9 at 7:07
You set the bold property on an empty paragraph, then added the text. Do it the other way around.e.g add the paragraph, add the text, set the sub range then finally apply formatting to the sub range.
– Freeflow
Mar 9 at 7:07
add a comment |
2 Answers
2
active
oldest
votes
Create another Range
object that only covers the characters that you want to bold.
The code below is not tested (don't have full VS set up on this machine), but should give you an idea:
Dim para1 As Word.Paragraph
Dim textToBeBolded As Word.Range
para1 = WordDoc.Content.Paragraphs.Add 'ThisDocument.Paragraphs.Add in VBA
para1.Range.Text = "Job number is: " + myJobID
para1.Range.SetRange 1, 14
textToBeBolded = para1.Range
textToBeBolded.SetRange 1, 14
textToBeBolded.Font.Bold = True
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
SetRange
is a method called from aRange
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange
– AJD
Mar 8 at 22:28
|
show 6 more comments
In order to format individual text runs it's necessary to break the text down into individual runs when inserting. Also, it's best to work with an independent Range
object. Between formatting commands the Range
needs to be "collapsed" - think of it like pressing the right (or left) arrow of a selection to make it a blinking cursor. Something along these lines
Dim Para1 As Word.Paragraph
Dim rng as Word.Range
Para1 = WordDoc.Content.Paragraphs.Add
rng = Para1.Range
rng.Text = "Job number is: "
rng.Font.Bold = True
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.Text = myJobID
rng.Font.Bold = False
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
If it's really necessary to insert the full text in one go, then Find/Replace to locate the text that should be formatted differently is one way to format after-the-fact, although less efficient.
Another possibility is to use string manipulation functions, such as Instr
(or Contains
), Left
, Mid
etc. to determine where in a longer string the substring is located. Then Range.Start
and Range.End
can work with those values. But generally it's better to not rely on the start
and end
values since Word can insert non-visible characters that can throw this numbering off.
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
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%2f55071534%2fhow-to-bold-only-a-portion-of-word-paragraph-in-visual-studio-vb%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
Create another Range
object that only covers the characters that you want to bold.
The code below is not tested (don't have full VS set up on this machine), but should give you an idea:
Dim para1 As Word.Paragraph
Dim textToBeBolded As Word.Range
para1 = WordDoc.Content.Paragraphs.Add 'ThisDocument.Paragraphs.Add in VBA
para1.Range.Text = "Job number is: " + myJobID
para1.Range.SetRange 1, 14
textToBeBolded = para1.Range
textToBeBolded.SetRange 1, 14
textToBeBolded.Font.Bold = True
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
SetRange
is a method called from aRange
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange
– AJD
Mar 8 at 22:28
|
show 6 more comments
Create another Range
object that only covers the characters that you want to bold.
The code below is not tested (don't have full VS set up on this machine), but should give you an idea:
Dim para1 As Word.Paragraph
Dim textToBeBolded As Word.Range
para1 = WordDoc.Content.Paragraphs.Add 'ThisDocument.Paragraphs.Add in VBA
para1.Range.Text = "Job number is: " + myJobID
para1.Range.SetRange 1, 14
textToBeBolded = para1.Range
textToBeBolded.SetRange 1, 14
textToBeBolded.Font.Bold = True
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
SetRange
is a method called from aRange
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange
– AJD
Mar 8 at 22:28
|
show 6 more comments
Create another Range
object that only covers the characters that you want to bold.
The code below is not tested (don't have full VS set up on this machine), but should give you an idea:
Dim para1 As Word.Paragraph
Dim textToBeBolded As Word.Range
para1 = WordDoc.Content.Paragraphs.Add 'ThisDocument.Paragraphs.Add in VBA
para1.Range.Text = "Job number is: " + myJobID
para1.Range.SetRange 1, 14
textToBeBolded = para1.Range
textToBeBolded.SetRange 1, 14
textToBeBolded.Font.Bold = True
Create another Range
object that only covers the characters that you want to bold.
The code below is not tested (don't have full VS set up on this machine), but should give you an idea:
Dim para1 As Word.Paragraph
Dim textToBeBolded As Word.Range
para1 = WordDoc.Content.Paragraphs.Add 'ThisDocument.Paragraphs.Add in VBA
para1.Range.Text = "Job number is: " + myJobID
para1.Range.SetRange 1, 14
textToBeBolded = para1.Range
textToBeBolded.SetRange 1, 14
textToBeBolded.Font.Bold = True
edited Mar 8 at 22:42
answered Mar 8 at 22:16
AJDAJD
1,5482514
1,5482514
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
SetRange
is a method called from aRange
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange
– AJD
Mar 8 at 22:28
|
show 6 more comments
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
SetRange
is a method called from aRange
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange
– AJD
Mar 8 at 22:28
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
If I do that it throws an error message. I can't do: textToBeBolded = Para1.Range(1,14)
– JohnA
Mar 8 at 22:21
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
And of course I declared it in a new Range: Dim textToBeBolded As Word.Range
– JohnA
Mar 8 at 22:25
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
I don't have SetRange either. Is there another library I should be referencing besides the Word object library?
– JohnA
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
What error message? As noted, I could not test the code itself - but the concept of creating a separate sub-range and then applying the font modifiers to that sub-range is the important concept here.
– AJD
Mar 8 at 22:27
SetRange
is a method called from a Range
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange– AJD
Mar 8 at 22:28
SetRange
is a method called from a Range
object : docs.microsoft.com/en-us/office/vba/api/word.range.setrange– AJD
Mar 8 at 22:28
|
show 6 more comments
In order to format individual text runs it's necessary to break the text down into individual runs when inserting. Also, it's best to work with an independent Range
object. Between formatting commands the Range
needs to be "collapsed" - think of it like pressing the right (or left) arrow of a selection to make it a blinking cursor. Something along these lines
Dim Para1 As Word.Paragraph
Dim rng as Word.Range
Para1 = WordDoc.Content.Paragraphs.Add
rng = Para1.Range
rng.Text = "Job number is: "
rng.Font.Bold = True
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.Text = myJobID
rng.Font.Bold = False
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
If it's really necessary to insert the full text in one go, then Find/Replace to locate the text that should be formatted differently is one way to format after-the-fact, although less efficient.
Another possibility is to use string manipulation functions, such as Instr
(or Contains
), Left
, Mid
etc. to determine where in a longer string the substring is located. Then Range.Start
and Range.End
can work with those values. But generally it's better to not rely on the start
and end
values since Word can insert non-visible characters that can throw this numbering off.
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
add a comment |
In order to format individual text runs it's necessary to break the text down into individual runs when inserting. Also, it's best to work with an independent Range
object. Between formatting commands the Range
needs to be "collapsed" - think of it like pressing the right (or left) arrow of a selection to make it a blinking cursor. Something along these lines
Dim Para1 As Word.Paragraph
Dim rng as Word.Range
Para1 = WordDoc.Content.Paragraphs.Add
rng = Para1.Range
rng.Text = "Job number is: "
rng.Font.Bold = True
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.Text = myJobID
rng.Font.Bold = False
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
If it's really necessary to insert the full text in one go, then Find/Replace to locate the text that should be formatted differently is one way to format after-the-fact, although less efficient.
Another possibility is to use string manipulation functions, such as Instr
(or Contains
), Left
, Mid
etc. to determine where in a longer string the substring is located. Then Range.Start
and Range.End
can work with those values. But generally it's better to not rely on the start
and end
values since Word can insert non-visible characters that can throw this numbering off.
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
add a comment |
In order to format individual text runs it's necessary to break the text down into individual runs when inserting. Also, it's best to work with an independent Range
object. Between formatting commands the Range
needs to be "collapsed" - think of it like pressing the right (or left) arrow of a selection to make it a blinking cursor. Something along these lines
Dim Para1 As Word.Paragraph
Dim rng as Word.Range
Para1 = WordDoc.Content.Paragraphs.Add
rng = Para1.Range
rng.Text = "Job number is: "
rng.Font.Bold = True
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.Text = myJobID
rng.Font.Bold = False
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
If it's really necessary to insert the full text in one go, then Find/Replace to locate the text that should be formatted differently is one way to format after-the-fact, although less efficient.
Another possibility is to use string manipulation functions, such as Instr
(or Contains
), Left
, Mid
etc. to determine where in a longer string the substring is located. Then Range.Start
and Range.End
can work with those values. But generally it's better to not rely on the start
and end
values since Word can insert non-visible characters that can throw this numbering off.
In order to format individual text runs it's necessary to break the text down into individual runs when inserting. Also, it's best to work with an independent Range
object. Between formatting commands the Range
needs to be "collapsed" - think of it like pressing the right (or left) arrow of a selection to make it a blinking cursor. Something along these lines
Dim Para1 As Word.Paragraph
Dim rng as Word.Range
Para1 = WordDoc.Content.Paragraphs.Add
rng = Para1.Range
rng.Text = "Job number is: "
rng.Font.Bold = True
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
rng.Text = myJobID
rng.Font.Bold = False
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
If it's really necessary to insert the full text in one go, then Find/Replace to locate the text that should be formatted differently is one way to format after-the-fact, although less efficient.
Another possibility is to use string manipulation functions, such as Instr
(or Contains
), Left
, Mid
etc. to determine where in a longer string the substring is located. Then Range.Start
and Range.End
can work with those values. But generally it's better to not rely on the start
and end
values since Word can insert non-visible characters that can throw this numbering off.
edited Mar 11 at 18:52
answered Mar 9 at 6:32
Cindy MeisterCindy Meister
16.1k102537
16.1k102537
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
add a comment |
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Nevermind, I had a typo elsewhere.I think this is working, but I have to play around with some other stuff.
– JohnA
Mar 11 at 13:27
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Copied from comment by @JohnA posted to wrong Answer: Thanks for the reply. Unfortunately, when I try this, nothing shows up at all - the entire line does not even print. I think you misplaced some quotation marks because if I try your code literally, it prints out three entire lines of code literally on the Word doc (basically, lines 5-8 on your code). So I put a quotation mark after "Job ID is ". Also, you needed parentheses surrounding Word.WdCollapseDirection.wdCollapseEnd.
– Cindy Meister
Mar 11 at 18:50
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
Thanks for the corrections @JohnA - I was typing on a mobile device, off the top of my head... Sorry for the confusion! I've made the changes in the code in the Answer.
– Cindy Meister
Mar 11 at 18:52
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%2f55071534%2fhow-to-bold-only-a-portion-of-word-paragraph-in-visual-studio-vb%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
You set the bold property on an empty paragraph, then added the text. Do it the other way around.e.g add the paragraph, add the text, set the sub range then finally apply formatting to the sub range.
– Freeflow
Mar 9 at 7:07