【问题标题】:VBA: Format all font in documentVBA:格式化文档中的所有字体
【发布时间】:2013-11-13 21:44:22
【问题描述】:

此代码在 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

【问题讨论】:

  • word 文档或 excel 电子表格中的所有文本?
  • word 文档,代码正在从 excel 中运行,这是在询问要打开什么 word 文档后插入的
  • 我认为这将是您的Selection 的问题。你应该使用更直接的方式。
  • 活动文档之类的东西会起作用吗?
  • Selection.WholeStory 特定于 Microsoft Word:msdn.microsoft.com/en-us/library/…

标签: vba excel ms-word


【解决方案1】:

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

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

【讨论】:

  • 很好.. 所以我猜Content 是他需要的吗?
【解决方案2】:

在顶部设置常量:

常量 wdColorAutomatic = -16777216

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

【讨论】:

  • 如果您没有在顶部声明 wdColorAutomatic 将不起作用 :) 请记住您是后期绑定 ;) 您需要在顶部声明 Const wdColorAutomatic = -16777216
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2017-05-06
  • 2014-04-29
相关资源
最近更新 更多