Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA用于在Word中创建编号列表_Vba_Excel_Ms Word_Office 2007 - Fatal编程技术网

Excel VBA用于在Word中创建编号列表

Excel VBA用于在Word中创建编号列表,vba,excel,ms-word,office-2007,Vba,Excel,Ms Word,Office 2007,我正在尝试使用Excel中的VBA代码在Word文档中创建编号列表 Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Set wrdApp = CreateObject("Word.Application") wrdApp.Visible = True Set wrdDoc = wrdApp.Documents.Add With wrdDoc For i = 0 To 5 .Content.Inser

我正在尝试使用Excel中的VBA代码在Word文档中创建编号列表

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document

Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add

With wrdDoc
    For i = 0 To 5
        .Content.InsertAfter ("Paragraph " & i)
        .Content.InsertParagraphAfter
    Next

    .Paragraphs(1).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior
End With

Set wrdApp = Nothing
Set wrdDoc = Nothing
当我运行此命令时,我得到一个错误:

对象“ListFormat”的方法“ApplyListTemplateWithLevel”失败


我已检查了Excel VBA引用列表中的Microsoft Word 12.0对象库。

确定我发现了问题。我遥控进入朋友的电脑进行检查。如果有其他word文档打开,我会遇到与您相同的错误。如果没有打开其他word文档,那么代码就可以正常工作

试试这个代码。它与Word应用程序绑定,因此不需要添加引用

Sub Sample()
    Dim oWordApp As Object, oWordDoc As Object

    '~~> 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

        DoEvents

        .Paragraphs(1).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作为对象
“~~>建立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
以
设置oWordApp=Nothing
设置oWordDoc=Nothing
端接头

我在Excel 2010中测试了Word 2010的代码。它工作得很好。。。在2007年进行测试该死!VMWare上的My Vista正在配置更新。我需要一段时间才能测试上述内容…最终在Office2007中进行了测试。它工作得很好。好的,谢谢,仍然不为我工作,所以请等待其他人是否有任何想法。如果为导致错误的行添加行
DoEvents
,会发生什么情况?