formatting text in a Word Doc

M

Mark

Hi,

I'm trying to format text in a Word Document using VB.Net. All the help
files tell me how to select a paragraph, sentence, etc and format that, but
what I need is to format a specific phrase in the document as I am building
it. The phrase may exist more than once in the document, but I need to 'hone
in' only on the phrase I am interested in. How is this done?
 
P

Peter Huang [MSFT]

Hi

I think you may try to use the Find/Replace function to format the special
words.
Here is some VBA code snippet, but the Automation code is similar.
[VBA]
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "SearchText"
.Replacement.Text = "SearchText"
.Replacement.Font.Color = wdColorBrown
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

[NOTE:]
The Text and Replacement.Text is same so the content will not change.
And we use the Replacement's property to format the found text.
e.g. .Replacement.Font.Color


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark

This does not answer my question. I do not want to bold every instance of the
phrase that matches the one I wish to bold. Let me be more specific.

I have a long document, pre-written and saved. I wish to open it
programmatically, and append text to it, with the text generated being a
function of some independant variable. As I write the clause, certain words
need to be bold.....for instance.....I might write the following...."undue
care and attention results in accidents." I may need the words 'care and
attention' to be bolded.....I want to set them to bold as I write them. In
other words, 'undue' is not bold, 'care and attention' is bold, and 'results
in accidents' is not bold.


I would like to do this without searching, but on the fly, as I write to the
document. Is this possible? All examples I find on the subject refer to
selecting the first 7 characters of a document, or the first or second
sentence of a document (or paragraph), etc; none of these scenarios provide
an indication as to how to do what I need, let alone if even possible.
 
P

Peter Huang [MSFT]

Hi

I think it is hard to do that Word did not know when to specify the word to
be Bold,e.g. when you type "c", how did word know you will type "are".
Also if you want bold, you may just press Ctrl+B when you need to format.
e.g. typing..... Ctrl+B typing........Ctrl+B typing......, so it will be
as the style below.
normal...Bold....normal

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cindy Meister

Hi Mark

You don't show us any kind of sample code you're using, so it's difficult to
be specific...

You do need to learn how to work with ranges in Word. Given the phrasing you
show us here, this is how I would go about it ("pseudo code", so there may be
syntax errors)

Dim rng as Word.Range = doc.Range
rng.Collapse wdCollapseEnd
rng.Text = "undue "
rng.Collapse wdCollapseEnd
rng.Text = "care and attention"
rng.Bold = True
rng.Collapse wdCollapseEnd
rng.Text = "results in accidents"
rng.Text.Bold = False

-- Cindy
 
M

Mark

Peter,

I think you have failed to read my original question. I do not expect Word
to 'know' anything. (it is software, not wetware). And your suggestion to use
Ctrl + B seems to indicate that you think I am using Word application via the
application GUI....which I thought I made clear I am not (at least I thought
I was clear on this, if not, my apologies).


Regards,
Mark St Denis
 
M

Mark

Hi Cindy,

Thanks for your reply. I agree that I need to learn about ranges.
Unfortunately, I was unable to make anything of what you provided. Not sure
what your intent was with the 'wdCollapseEnd', as it does not appear to be
any kind of object or enum value in my environment (VS.Net 2003, Word 2003).
But, doing a google search on the word did eventually lead me to some info
regarding Word automation in Visual Foxpro, which indicated that it is a
declared variable.

However, I have been completely unable to get the range object to do
anything remotely close to what I want....as per one of the FoxPro examples,
I found that the Selection object works.

You mention that I didn't provide sample code. That is because I don't
really have any. I'm not sure how that would help anyway, as what I had
didn't do any of what I want it to do.

I find the Word Object Model very difficult to grasp, and Microsoft's help
files on it are not much help at all. They are really more descriptive, and
the examples are too simplistic to really illustrate how the objects work.

Would anyone know of some good material (with GOOD code examples) covering
the subject? Anything good in the way of books seem to be no longer
available.
 
P

Peter Huang [MSFT]

Hi

Thanks for your quickly reply!
I still did not understand your scenario very much.
I assume you are automation Word to insert text and you want to bold some
words in the inserted text.
If so I think you may try using the range as Cindy's suggestion.

For word's OM reference, you may reference to the Word's VBA help file.
<Program Files>\Microsoft Office\OFFICE11\1033\VBAWD10.CHM

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cindy Meister

Hi Mark

VS 2003 covers a lot of ground. If you were using VB.NET the sample code
should work, more or less. So perhaps if you tell us which language you're
using the code example could be more specific? And it would help if you
provide the basic approach you're using to link to the Word application and
the document, so that the example can use that, making it more obvious to you
how things are approached.

I think the best source, currently, for automation code in .NET languages is
probably MSDN. But no, there's not a whole lot out there - yet. You'll need
to learn how to abstract the VB documentation to your language, if you need
to delve into the Office COM object models to any extent.

There is one really good book out there, from Microsoft Press, about
automating Office 2003 through .NET. Can't recall the exact title or author.
Charles White, maybe?

-- Cindy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top