VBA/Word添加项目符号、编号等
我一直在玩VBA代码,它允许自动创建word文档。在下面的例子中,我把一个单词的段落写了6遍。为了进行某些格式化(项目符号、编号、将文本放入表格等),似乎有必要在创建文本后进行第二次传递并应用格式化。这可以像VBA允许我们使用粗体或斜体那样一次完成吗 例如: -切换编号 -写行VBA/Word添加项目符号、编号等,vba,ms-word,Vba,Ms Word,我一直在玩VBA代码,它允许自动创建word文档。在下面的例子中,我把一个单词的段落写了6遍。为了进行某些格式化(项目符号、编号、将文本放入表格等),似乎有必要在创建文本后进行第二次传递并应用格式化。这可以像VBA允许我们使用粗体或斜体那样一次完成吗 例如: -切换编号 -写行 Sub Sample() Dim oWordApp As Object, oWordDoc As Object Dim i As Integer '~~> Establish an Wo
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
子样本()
将oWordApp作为对象,将oWordDoc作为对象
作为整数的Dim i
“~~>建立Word应用程序对象
出错时继续下一步
Set oWordApp=GetObject(,“Word.Application”)
如果错误号为0,则
设置oWordApp=CreateObject(“Word.Application”)
如果结束
呃,明白了
错误转到0
oWordApp.Visible=True
设置oWordDoc=oWordApp.Documents.Add
与oWordDoc
对于i=0到5
.Content.InsertAfter(“段落”&i)
.Content.InsertParagraphAfter
下一个
'生成执行,以便操作系统可以处理其他事件。
多芬特
'为什么必须在创建文本后执行此操作?
.段落(1).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=_
ListGalleries(wdNumberGallery)。ListTemplates(1),ContinuePreviousList:=_
False,ApplyTo:=wdListApplyToWholeList,DefaultListBehavior:=_
wdWord10ListBehavior
.段落(2).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=_
ListGalleries(wdNumberGallery)。ListTemplates(1),ContinuePreviousList:=_
False,ApplyTo:=wdListApplyToWholeList,DefaultListBehavior:=_
wdWord10ListBehavior
以
设置oWordApp=Nothing
设置oWordDoc=Nothing
端接头
你显然可以做到。有一些可能性,但我相信下面的解决方案为您提供了如何使用
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
你显然可以做到。有一些可能性,但我相信下面的解决方案为您提供了如何使用
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
这样就可以了
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
这样就可以了
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
我在添加项目符号时遇到了一些问题,因此下面是对我有效的代码 请注意,这需要添加对Microsoft Word 15.0对象库的引用
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
我在添加项目符号时遇到了一些问题,因此下面是对我有效的代码 请注意,这需要添加对Microsoft Word 15.0对象库的引用
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
威尔尝试了一下,但我真的很想做一些事情,比如1)将样式设置为“标题1”2)编写标题3)新段落4)将段落设置为项目符号列表(或编号列表)5)编写段落。。。重复6)写n段。。。然后重复我不喜欢写段落(然后再添加格式。段落来自另一个数据库源,可能会在每个文档中编号。will会尝试一下,但我真的很想做一些事情,比如1)将样式设置为“标题1”2)写标题3)新段落4)将段落设置为项目符号列表(或编号列表)5)写一段。。。重复6)写n段。。。然后重复我不喜欢写段落(然后再添加格式)。段落来自另一个数据库源,可能会在每个数据集中编号。