简体   繁体   English

VBA - 根据 Excel 单元格值更改 MS Word 字体样式

[英]VBA - Change MS Word Font Style Based on Excel Cell Values

I want to automatically change a document's font type and size based on Excel cell values.我想根据 Excel 单元格值自动更改文档的字体类型和大小。 For example, if I input “Times New Roman” in cell B3 and 12 in cell B4, then the document should be formatted with these styles.例如,如果我在 B3 单元格中输入“Times New Roman”,在 B4 单元格中输入 12,那么文档应该使用这些 styles 格式化。 However, when I run my macro, it ignores my variables and I can't figure out why.但是,当我运行我的宏时,它会忽略我的变量,我不知道为什么。 Below is my code:下面是我的代码:

Sub FormatWholeAsDefaultFont()
    Dim mySpreadsheet As Excel.Workbook
    Dim strFont As String, strRange As String
    Dim sngFontSize As Single, sngTopMargin As Single, _
        sngBottomMargin As Single, sngLeftMargin As Single, _
        sngRightMargin As Single
    Set mySpreadsheet = _
           GetObject("C:\Files\Data\Excel\Document Timesheet.xlsm")
    
    strFont = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B2").Value
    sngFontSize = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B3").Value
    sngLeftMargin = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B5").Value
    sngRightMargin = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B6").Value
    sngTopMargin = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B7").Value
    sngBottomMargin = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B8").Value
    strRange = mySpreadsheet.Application.Workbooks("Document Timesheet.xlsm") _
            .Sheets("Document Agreement").Range("B9").Value
    'strFont = CStr(strFont)
    Selection.WholeStory
    With ActiveDocument.Styles(wdStyleNormal).Font
        .Name = strFont
        .Size = sngFontSize
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 11
        .Animation = wdAnimationNone
        .Ligatures = wdLigaturesNone
        .NumberSpacing = wdNumberSpacingDefault
        .NumberForm = wdNumberFormDefault
        .StylisticSet = wdStylisticSetDefault
        .ContextualAlternates = 0
    End With
End Sub

You have a lot of unnecessary code in your routine.您的例程中有很多不必要的代码。 Below is a simplified version.下面是简化版。

Sub FormatWholeAsDefaultFont()
    Dim mySpreadsheet As Excel.Workbook
    Dim strFont As String, strRange As String
    Dim sngFontSize As Single, sngTopMargin As Single, _
        sngBottomMargin As Single, sngLeftMargin As Single, _
        sngRightMargin As Single
    Set mySpreadsheet = _
        GetObject("C:\Files\Data\Excel\Document Timesheet.xlsm")
    
    With mySpreadsheet.Sheets("Document Agreement")
        strFont = .Range("B2").Value
        sngFontSize = .Range("B3").Value
        sngLeftMargin = .Range("B5").Value
        sngRightMargin = .Range("B6").Value
        sngTopMargin = .Range("B7").Value
        sngBottomMargin = .Range("B8").Value
        strRange = .Range("B9").Value
    End With
    
    With ActiveDocument.Styles(wdStyleNormal).Font
        .Name = strFont
        .Size = sngFontSize
    End With
End Sub

Please note:请注意:

Since Word 2007 it has been necessary to set the document's default font in the Set Defaults tab of the Manage Styles dialog, accessed from a button at the bottom of the Styles pane or by typing Dialogs(wdDialogStyleManagement).Show into the Immediate Window. Since Word 2007 it has been necessary to set the document's default font in the Set Defaults tab of the Manage Styles dialog, accessed from a button at the bottom of the Styles pane or by typing Dialogs(wdDialogStyleManagement).Show into the Immediate Window. It is vital for the correct working of Table Styles that the Normal style match these document defaults.正常样式与这些文档默认值匹配对于表 Styles 的正确工作至关重要。

However, it is not possible to set the document defaults using VBA.但是,无法使用 VBA 设置文档默认值。

Sub FormatWholeAsDefaultFont() Dim mySpreadsheet As Excel.Workbook Dim strFont As String, strRange As String Dim sngFontSize As Single, sngTopMargin As Single, _ sngBottomMargin As Single, sngLeftMargin As Single, _ sngRightMargin As Single Set mySpreadsheet = _ GetObject("C:\Files\Data\Excel\Document Timesheet.xlsm") Sub FormatWholeAsDefaultFont() Dim mySpreadsheet As Excel.Workbook Dim strFont As String, strRange As String Dim sngFontSize As Single, sngTopMargin As Single, _ sngBottomMargin As Single, sngLeftMargin As Single, _ sngRightMargin As Single 设置 mySpreadsheet(文件\数据\Excel\Document Timesheet.xlsm")

With mySpreadsheet.Sheets("Document Agreement")
    strFont = .Range("B2").Value
    sngFontSize = .Range("B3").Value
    sngLeftMargin = .Range("B5").Value
    sngRightMargin = .Range("B6").Value
    sngTopMargin = .Range("B7").Value
    sngBottomMargin = .Range("B8").Value
    strRange = .Range("B9").Value
End With

Selection.WholeStory
Selection.Font.Name = strFont
Selection.Font.Size = sngFontSize

With ActiveDocument.Styles(wdStyleNormal).Font
    .Name = strFont
    .Size = sngFontSize
End With

End Sub结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM