tags:

views:

33

answers:

2

We are using VB6 and Word.Basic object to create documents. Text is inserted at a variety of bookmarks. Our latest requirement wants us to increase the font size for a given insert. We are currently doing this by setting the font size to 12 rather than the 8 that is normally there, however from a maintenance standpoint, I would rather be able to set this to say (currentfontsize + 4)... but so far I have not been able to find any method to get the current font size.

Does anyone have a way to do this?

A: 

Given a Range object, you can check range.Font.Size.

SLaks
+2  A: 

Setting the font size of a bookmark is straight-forward in VBA:

Dim doc As Document

Const MultiSelection As Long = 9999999

Set doc = Application.ActiveDocument

If doc.Bookmarks.Exists("myBookmark") Then
    If doc.Bookmarks("myBookmark").Range.Font.Size = MultiSelection Then
        ' the range of the bookmark consists of runs with different font sizes
        doc.Bookmarks("myBookmark").Range.Font.Size = 12
    Else
        doc.Bookmarks("myBookmark").Range.Font.Size _
            = doc.Bookmarks("myBookmark").Range.Font.Size + 4
    End If
End If

Probably a better option if you want to increase the font size proportionally, is to use Font.Grow which will increase the font size to the next available size, e.g. to get from 8 to 12 you would have to call it 4 times.

If doc.Bookmarks.Exists("myBookmark") Then
    For i = 1 To 4
        doc.Bookmarks("myBookmark").Range.Font.Grow
    Next
End If

If you are not familiar with Word's object model, a trick is to use the Macro Recorder. In you case, start the recorder, open the bookmarks dialog, go to the bookmark and then change the font size. The recorded actions will be saved as a module in your Normal.dot file by default. The action of changing the font size of a bookmark would result in the following recorded macro:

Selection.GoTo What:=wdGoToBookmark, Name:="myBookmark"
With ActiveDocument.Bookmarks
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With
Selection.Font.Size = 12

This code can be the basis for your own function (although it looks a bit different from the above sample, the effect will be the same).

0xA3
I know how to set it, was looking for the size. But based on the other answer... could I modify yours to:doc.Bookmarks("myBookmark").Range.Font.Size = doc.Bookmarks("myBookmark").Range.Font.Size + 4? (Or is it readonly?)
IPX Ares
Yes, that works, too, but only if the current bookmark does not use different font sizes. If it does, you will get an undefined value (9999999) from `range.Font.Size`.
0xA3
perfect thanks!
IPX Ares