VBA:格式化文档中的所有字体

时间:2013-11-13 21:44:22

标签: excel vba excel-vba ms-word

此代码在MS Word中使用时有效,现在尝试从excel运行。我想用相同的字体类型和相同的字体大小格式化文档中的所有文本。

'Format document with universal font type and size
    Selection.WholeStory
    Selection.Font.Name = "Calibri"
    Selection.Font.Size = 11
    End With

这个也没用:

ActiveDocument.Range.Font.Color = wdColorAutomatic
ActiveDocument.Range.Font.Name = "Calibri"
ActiveDocument.Range.Font.Size = 11

2 个答案:

答案 0 :(得分:4)

我想这是你最后一个问题的延续。试试这个。我没有从前面的代码中删除某些声明。

Const wdFindContinue = 1

Sub FnFindAndFormat()
    Dim FileToOpen
    Dim objWord As Object, objDoc As Object, Rng As Object
    Dim MyAr() As String, strToFind As String
    Dim i As Long

    '~~> This holds your search words
    strToFind = "deal,contract,sign,award"

    '~~> Create an array of text to be found
    MyAr = Split(strToFind, ",")

    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Word Files *.docx (*.docx),")

    If FileToOpen = False Then Exit Sub

    Set objWord = CreateObject("Word.Application")
    '~~> Open the relevant word document
    Set objDoc = objWord.Documents.Open(FileToOpen)

    objWord.Visible = True

    Set Rng = objDoc.Content

    With Rng
        .Font.Name = "Calibri"
        .Font.Size = 11
    End With
End Sub

答案 1 :(得分:0)

将常数设置为顶部:

Const wdColorAutomatic = -16777216

objDoc.Range.Font.Color = wdColorAutomatic
        objDoc.Range.Font.Name = "Calibri"
        objDoc.Range.Font.Size = 11