[英]VBA - Change MS Word Font Style Based on Excel Cell Values
我想根據 Excel 單元格值自動更改文檔的字體類型和大小。 例如,如果我在 B3 單元格中輸入“Times New Roman”,在 B4 單元格中輸入 12,那么文檔應該使用這些 styles 格式化。 但是,當我運行我的宏時,它會忽略我的變量,我不知道為什么。 下面是我的代碼:
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
您的例程中有很多不必要的代碼。 下面是簡化版。
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
請注意:
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. 正常樣式與這些文檔默認值匹配對於表 Styles 的正確工作至關重要。
但是,無法使用 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 設置 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
結束子
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.