Как сделать текст жирным, используя Visual Basic в Excel для создания документа Word? - PullRequest
0 голосов
/ 21 июня 2011

Я видел это , но у меня это не работает; Я не понимаю, где изменить insertafter на typetext. Что я должен изменить в следующем, чтобы часть текста была выделена жирным шрифтом?

Sub CreateNewWordDoc()
    Dim wrdDoc As Word.Document
    Dim wrdApp As Word.Application
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Add
    With wrdDoc
        .Content.InsertAfter "not bold "
        .Content.Font.Bold = True
        .Content.InsertAfter "should be bold"
        .Content.Font.Bold = False
        .Content.InsertAfter " again not bold, followed by newline"
        .Content.InsertParagraphAfter
        .Content.Font.Bold = True
        .Content.InsertAfter "bold again"
        .Content.Font.Bold = False
        .Content.InsertAfter " and again not bold"
        .Content.InsertParagraphAfter
        .SaveAs ("testword.doc")
        .Close
    End With
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

1 Ответ

0 голосов
/ 22 июня 2011

Когда вы пишете .Content.Font.Bold = True и .Content.Font.Bold = False, вы переключаете целое Content вперед и назад между жирным и не жирным шрифтом.Последнее утверждение такого рода - .Content.Font.Bold = False, поэтому, конечно, ничто не заканчивается жирным шрифтом.

Вот способ сделать то, что вы хотите.Это действительно плохо и быстро написано, но это должно поставить вас на правильный путь.

    .Content.InsertAfter "not bold "
    .Content.InsertAfter "should be bold"
    .Content.InsertAfter " again not bold, followed by newline"
    .Content.InsertParagraphAfter
    .Content.InsertParagraphAfter
    .Content.InsertParagraphAfter
    .Content.InsertAfter "bold again"
    .Content.InsertAfter " and again not bold"
    .Content.InsertParagraphAfter

    .Words(3).Font.Bold = True
    .Words(4).Font.Bold = True
    .Words(5).Font.Bold = True
    .Words(16).Font.Bold = True
    .Words(17).Font.Bold = True
...