VBA/Word 添加项目符号、编号等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23456699/
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
VBA/Word add bullets, numbering etc
提问by Mustapha George
I have been playing with VBA code that permits automatic creation of word documents. In the example below, I write a Word paragraph 6 times. In order to do certain formatting (bullets, numbering, putting text in tables etc...) it appears necessary to make a second pass and apply formatting after text is created. Can this be done in a single pass like VBA allows us to do with bold or italics?
我一直在使用允许自动创建 word 文档的 VBA 代码。在下面的例子中,我写了 6 次 Word 段落。为了进行某些格式设置(项目符号、编号、将文本放入表格等...),似乎有必要在创建文本后进行第二遍并应用格式。这可以像 VBA 允许我们使用粗体或斜体那样一次性完成吗?
Example: - toggle numbering - write line
示例: - 切换编号 - 写行
Sub Sample()
Dim oWordApp As Object, oWordDoc As Object
Dim i As Integer
'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = True
Set oWordDoc = oWordApp.Documents.Add
With oWordDoc
For i = 0 To 5
.Content.InsertAfter ("Paragraph " & i)
.Content.InsertParagraphAfter
Next
' Yields execution so that the operating system can process other events.
DoEvents
' why does this have to be done after creating text?
.Paragraphs(1).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
.Paragraphs(2).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
End With
Set oWordApp = Nothing
Set oWordDoc = Nothing
End Sub
example from Excel VBA for creating numbered list in Word
采纳答案by Kazimierz Jawor
You can obviously do it. There are few possibilities but I believe the below solution give you a clue of how to use Document.Range(start,end)
property.
你显然可以做到。可能性很少,但我相信以下解决方案为您提供了如何使用Document.Range(start,end)
财产的线索。
With oWordDoc
For i = 0 To 5
.Content.InsertAfter ("Paragraph " & i)
.Content.InsertParagraphAfter
Next
' Yields execution so that the operating system can process other events.
DoEvents
'apply numbering for 6 added paragraphs
.Range(.Paragraphs(1).Range.Start, _
.Paragraphs(6).Range.End) _
.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
'... the rest of your code here
回答by user1274820
I had some trouble adding bullets, so here is the code that worked for me.
我在添加项目符号时遇到了一些麻烦,所以这里是对我有用的代码。
Note that this requires adding a reference to Microsoft Word 15.0 Object Library
请注意,这需要添加对 Microsoft Word 15.0 Object Library
Sub Generate()
Dim objWord
Dim objDoc
Dim temp3 As Word.ListTemplate
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection
'Change your text here:
objSelection.TypeText ("This is my text in Word Document using Excel")
objSelection.TypeParagraph
objSelection.TypeText ("Here is more text")
'--------------------------
Set temp3 = objWord.ListGalleries(wdNumberGallery).ListTemplates(1)
With temp3.ListLevels(1)
.Font.Name = "Symbol"
.Font.Size = 11
.NumberFormat = ChrW(61623)
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = objWord.CentimetersToPoints(0.63)
.Alignment = wdListLevelAlignLeft
.TextPosition = objWord.CentimetersToPoints(1.27)
.TabPosition = wdUndefined
.StartAt = 1
End With
'Apply formatting to our range
objDoc.Range.ListFormat.ApplyListTemplate ListTemplate:=temp3
End Sub
回答by Mustapha George
This will do it...
这将做到...
Dim wdApp As Word.Application
Set wdApp = New Word.Application
With wdApp
.Visible = True
.Activate
.Documents.Add
' turn on bullets
.ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
.Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=.ListGalleries(wdBulletGallery).ListTemplates(1), _
continuepreviouslist:=False, applyto:=wdListApplyToWholeList, defaultlistbehavior:=wdWord9ListBehavior
With .Selection
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Font.Bold = False
.Font.Name = "Century Gothic"
.Font.Size = 12
.TypeText ("some details")
.TypeParagraph
.TypeText ("some details")
.TypeParagraph
End With
' turn off bullets
.Selection.Range.ListFormat.RemoveNumbers wdBulletGallery
With .Selection
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.TypeText ("some details")
.TypeParagraph
.TypeText ("some details")
.TypeParagraph
End With
' turn on outline numbers
.ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = ""
.Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=.ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
continuepreviouslist:=False, applyto:=wdListApplyToWholeList, defaultlistbehavior:=wdWord9ListBehavior
With .Selection
.ParagraphFormat.Alignment = wdAlignParagraphLeft
.TypeText ("some details")
.TypeParagraph
.TypeText ("some details")
End With
End With