Excel VBA:在向MS-Word添加文本时设置字体样式和大小 [英] Excel VBA: setting font style and size while adding text to MS-Word

查看:731
本文介绍了Excel VBA:在向MS-Word添加文本时设置字体样式和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Excel VBA创建一个Word文档,并添加各种字体和大小的文本。这是我的代码:

pre $ $ $ $ c $ Sub $ CreateNewWordDoc
Dim wrdDoc As Word.Document
Dim wrdApp As Word.Application
Set wrdApp = CreateObject(Word.Application)
Set wrdDoc = wrdApp.Documents.Add

Dim charStart As Long
Dim charEnd As Long

用wrdDoc
For i = 1 to 3
charStart = wrdApp.Selection.Start
.Content.InsertAfter(some text)
charEnd = wrdApp.Selection.End
如果i = 1那么
'将文本范围(charStart,charEnd)设置为eg Arial,8pt
else
如果i = 2那么
'将文本范围(charStart,charEnd)设置为例如Calibri,10pt
else
'将文本范围(charStart,charEnd)设置为Verdana,12pt
End If
End If
Next i
.Content.InsertParagraphAfter
.SaveAs(testword.docx)
.Close'close文档
End With
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

如何在上面的if-else语句中即时定义字体样式和大小?

解决方案

会这样符合法案吗? > Sub CreateNewWordDoc()
Dim doc As Word.Document
Dim toAdd As String
Dim lengthAdded As Long
Dim selStart As Long

Set doc = ActiveDocument
toAdd =Hello World'要添加什么?
lengthAdded = Len(toAdd)'稍后!
selStart = Selection.Start'在哪里添加文本?

doc.Range(selStart).InsertAfter(toAdd)
用doc.Range(selStart,selStart + lengthAdded)
这里是字体发生的地方
。 Font.Name =Arial
.Font.Size = 15
End With

End Sub

请注意,我摆脱了大部分与问题无关的代码。希望你能从我的代码外推给你!


I want to create a word document using Excel VBA, and add text with various font styles and sizes. Here is my code:

Sub CreateNewWordDoc()
    Dim wrdDoc As Word.Document
    Dim wrdApp As Word.Application
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Add

    Dim charStart As Long
    Dim charEnd As Long

    With wrdDoc
        For i = 1 To 3
            charStart = wrdApp.Selection.Start
            .Content.InsertAfter (" some text")
            charEnd = wrdApp.Selection.End
            If i = 1 Then
                'set the text range (charStart,charEnd) to e.g. Arial, 8pt
            Else
                If i = 2 Then
                    'set the text range (charStart,charEnd) to e.g. Calibri, 10pt
                Else
                    'set the text range (charStart,charEnd) to e.g. Verdana, 12pt
                End If
            End If
        Next i
        .Content.InsertParagraphAfter
        .SaveAs ("testword.docx")
        .Close ' close the document
    End With
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

How can I define font style and size on-the-fly in the if-else statement above?

解决方案

Would something like this fit the bill?

Sub CreateNewWordDoc()
  Dim doc As Word.Document
  Dim toAdd As String
  Dim lengthAdded As Long
  Dim selStart As Long

  Set doc = ActiveDocument
  toAdd = "Hello World" ' What to add?
  lengthAdded = Len(toAdd) ' For later!
  selStart = Selection.Start ' Where to add the text?

  doc.Range(selStart).InsertAfter (toAdd)
  With doc.Range(selStart, selStart + lengthAdded)
    ' Here's where the font stuff happens
    .Font.Name = "Arial"
    .Font.Size = 15
  End With

End Sub

Note that I've got rid of most of the code which isn't directly pertinent to the question. Hopefully you can extrapolate from my code to yours!

这篇关于Excel VBA:在向MS-Word添加文本时设置字体样式和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆