PDA

View Full Version : Solved: bold bookmarks and no_bold



white_flag
05-09-2011, 02:41 AM
Hello

I have the following code that will start from excel and will end in word :

this code will create an set of bookmarks in word bmk and tmk. What I do not know it is to set the fontsize and so one for apart bookmarks:
bmk to be bold size 11 and
tmk to be no_bold size 10


Sub CreateNewWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
With wrdDoc
For i = 1 To 10
.Bookmarks.Add "bmk" & i, .Characters.Last
.Range.InsertAfter vbCrLf
.Bookmarks("bmk" & i).Range.Font.Name = "Trebuchet MS"
.Bookmarks("bmk" & i).Range.Font.Size = 11
.Bookmarks("bmk" & i).Range.Font.Bold = True
.Bookmarks.Add "tmk" & i, .Characters.Last
.Range.InsertAfter vbCrLf
.Bookmarks("tmk" & i).Range.Font.Name = "Trebuchet MS"
.Bookmarks("tmk" & i).Range.Font.Size = 10
.Bookmarks("tmk" & i).Range.Font.Bold = False
Next i
.Bookmarks("bmk1").Range.Text = "title"
.Bookmarks("tmk1").Range.Text = "content"
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

white_flag
05-09-2011, 03:33 AM
I add those line:

.Bookmarks("bmk" & i).Range.Select
.Bookmarks("tmk" & i).Range.Select


but nothing



Sub CreateNewWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
With wrdDoc
For i = 1 To 10
.Bookmarks.Add "bmk" & i, .Characters.Last
.Range.InsertAfter vbCrLf
.Bookmarks("bmk" & i).Range.Select
.Bookmarks("bmk" & i).Range.Font.Name = "Trebuchet MS"
.Bookmarks("bmk" & i).Range.Font.Size = 11
.Bookmarks("bmk" & i).Range.Font.Bold = True
.Bookmarks.Add "tmk" & i, .Characters.Last
.Range.InsertAfter vbCrLf
.Bookmarks("tmk" & i).Range.Select
.Bookmarks("tmk" & i).Range.Font.Name = "Trebuchet MS"
.Bookmarks("tmk" & i).Range.Font.Size = 10
.Bookmarks("tmk" & i).Range.Font.Bold = False
Next i
.Bookmarks("bmk1").Range.Text = "title"
.Bookmarks("tmk1").Range.Text = "content"
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

gmaxey
05-09-2011, 05:03 AM
Your method of adding text to the bookmark is simply putting the text after the bookmark so the text isn't getting the font attributes of the bookmark range. To put text "in" a bookmark you can use something like this:

Option Explicit
Sub CreateNewWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Dim pBMName As String
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
With wrdDoc
For i = 1 To 10
pBMName = "bmk" & i
.Bookmarks.Add pBMName, .Characters.Last
.Range.InsertAfter vbCrLf
AddTextToBookmark wrdDoc, pBMName, "title", "Trebuchet MS", 11, True
pBMName = "tmk" & i
.Bookmarks.Add pBMName, .Characters.Last
.Range.InsertAfter vbCrLf
AddTextToBookmark wrdDoc, pBMName, "content", "Trebuchet MS", 10, False
Next i
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
Sub AddTextToBookmark(ByRef oDoc As Document, pName As String, pText As String, pFont As String, lngSize As Long, bBold As Boolean)
Dim myRange As Range
Dim oBM As Bookmark
With oDoc
Set myRange = .Bookmarks(pName).Range
myRange.Text = pText
Set oBM = .Bookmarks.Add(Name:=pName, Range:=myRange)
With oBM.Range
.Font.Name = pFont
.Font.Size = lngSize
.Font.Bold = bBold
End With
End With
End Sub

white_flag
05-09-2011, 05:21 AM
Greg, thank you for your answer. But it is not going.

gmaxey
05-09-2011, 06:09 PM
It is not going ... what? What exactly are you trying to do. Your loop is creating 10 bookmarks but your code is attempting to put text in only two of them. What is the end result supposed to look like?

white_flag
05-10-2011, 12:12 AM
the scoop is :
1. create bookmarks
2. go to bookmark
3. write the text
4. make the text from bookmark with diffrent properties (font, size, color ...)

I am number 4 :), it is not going

gmaxey
05-10-2011, 04:21 AM
You will have to change the order of operations. When you attempt to write text "in" a bookmark you delete the bookmark. You have to:

1. Define a range
2. Define text for the range
3. Bookmark the defined range
4. Apply attributes to the bookmark

Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oRng As Word.Range
Dim oBM As Bookmark
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseEnd
oRng.Text = "Test"
Set oBM = ActiveDocument.Bookmarks.Add("TestBM", oRng)
With oBM.Range.Font
.Name = "Arial"
.Size = 20
.Bold = True
End With
End Sub

white_flag
05-10-2011, 05:53 AM
this make sens .. so, Greg, thank you