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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19965004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:26:11  来源:igfitidea点击:

VBA: Format all font in document

excelvbaexcel-vbams-word

提问by user2965077

This code worked when used in MS Word, now trying to run from excel. I want to format all the text in the document with the same font type and the same font size.

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

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

This one also did not work:

这个也不起作用:

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

回答by Siddharth Rout

I guess this is in continuation to your last question. Try this. I have not deleted certain declarations from the previous code.

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

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

回答by user2965077

Set constant at top:

在顶部设置常量:

Const wdColorAutomatic = -16777216

常量 wdColorAutomatic = -16777216

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