Solved

Setting range to 'bold' fails (MS Word automation, vb.net)

Posted on 2003-11-16
5
984 Views
Last Modified: 2012-08-14
I'm writing a document generator with vb.net and Word automation. I've found some info on the net,  and I can succesfully modify bookmark values to create a fresh document, like so:

Dim oWordDoc As Word.Document = oWordApp.Documents.Open(fileName, missing, oReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)

Dim oBookmark As Object = myBookmarkName

oWordDoc.Bookmarks.Item(oBookmark).Range.Text = myText

However, it doesn't work when I try to change the formatting of this bookmark, to bold, italic or underlined, like so:

oWordDoc.Bookmarks.Item(oBookmark).Range.Font.Bold = True

I've tried replacing 'True' with '1', but it doesn't add any formatting changes to the open word document.

I've also tried grabbing the range first, but that doesn't work either:

Dim oRng As Word.Range = oWordDoc.Bookmarks.Item(oBookmark).Range
oRng.Font.Bold = True

The range object also has Bold, Italic, and Underline properties, but I haven't had any succes with those either. Oh, and I've tried setting the text value before and after the formatting change, but that doesn't seem to be the problem either.
0
Comment
Question by:Rapido
  • 3
  • 2
5 Comments
 

Author Comment

by:Rapido
Comment Utility
I found the answer! This is how it should be done, with a selection:

Dim oRng As Word.Range
                Dim oSel As Word.Selection

                oRng = oWordDoc.Bookmarks.Item(oBookmark).Range
                oSel = oWordApp.Selection()
                oSel.Start = oRng.Start
                oSel.End = oRng.End

                If (drDocVar("IsBold") = True) Then
                    oSel.Font.Bold = True
                End If

                If (drDocVar("IsItalic") = True) Then
                    oSel.Font.Italic = True
                End If

                If (drDocVar("IsUnderlined") = True) Then
                    oSel.Font.Underline = True
                End If

                oSel.TypeText(myText)
0
 
LVL 7

Accepted Solution

by:
wsteegmans earned 500 total points
Comment Utility
Try something like this:

Dim oWordDoc As Word.Document = oWordApp.Documents.Open(fileName, missing, oReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)

oWordApp.Selection.GoTo (wdGoToBookmark, missing, missing, oBookmark.Name)
oWordApp.Selection.Font.Bold = True
oWordApp.Selection.TypeText (myText)

You can also create a Style with these characteristics:
- Style Type: Character
- Font: Bold

Before typing your stuff, you choose this Style, like this:

oWordApp.Selection.Style = oWordApp.ActiveDocument.Styles("StyleBold")
oWordApp.Selection.TypeText (myText)
0
 
LVL 7

Expert Comment

by:wsteegmans
Comment Utility
You found it already yourselves ... :-)
0
 

Author Comment

by:Rapido
Comment Utility
Thanks anyhow. I'd be happy to give you the points so this question gets archived, I haven't been able to find an answer anywhere else....
0
 
LVL 7

Expert Comment

by:wsteegmans
Comment Utility
Thanks! But your solution is the most beautiful way!

Some additional info about Office Programming in VS .Net. With the new Office (2003), you can now also write managed code in your .Net programs to control Word or Excel ... Visual Studio .NET can help you incorporate Microsoft Office Word 2003 and Microsoft Office Excel 2003 into your custom solutions by providing tools and classes to make automation easier. You can also use Visual Studio .NET as your development environment for other Office applications.

Read more:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrconmanagedcodeofficeobjectmodels.asp
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to setup 18 78
countX 22 69
allStar challenge 1 41
stringclean challenge 26 54
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now