Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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写入Word 我想使用Excel将“标记名”存储在A列中,并将其关联的“替换文本”存储在B列中。当代码运行时,它需要收集每个标记,一次一个(逐行),在整个Word文档中搜索这些单词,并将其替换为相应的替换项 我注意到页眉和页脚上的特殊标记没有被替换。我转向本文()并发现使用一系列范围(或循环使用文档中所有可用的故事范围)我能够做到这一点 我改进了我的代码,正如上面链接中所建议的那样,只要我的代码嵌入到我的“普通”Word文件中,就行了,从而使用Word中的VBA代码对另一个Word文档进行操作。但是,目标是在读取Excel文件时使用VBA Excel操作替换 当我把代码移到Excel时,我被一个自动化错误挂断了_Vba_Excel_Runtime Error - Fatal编程技术网

将数据从Excel写入Word 我想使用Excel将“标记名”存储在A列中,并将其关联的“替换文本”存储在B列中。当代码运行时,它需要收集每个标记,一次一个(逐行),在整个Word文档中搜索这些单词,并将其替换为相应的替换项 我注意到页眉和页脚上的特殊标记没有被替换。我转向本文()并发现使用一系列范围(或循环使用文档中所有可用的故事范围)我能够做到这一点 我改进了我的代码,正如上面链接中所建议的那样,只要我的代码嵌入到我的“普通”Word文件中,就行了,从而使用Word中的VBA代码对另一个Word文档进行操作。但是,目标是在读取Excel文件时使用VBA Excel操作替换 当我把代码移到Excel时,我被一个自动化错误挂断了

将数据从Excel写入Word 我想使用Excel将“标记名”存储在A列中,并将其关联的“替换文本”存储在B列中。当代码运行时,它需要收集每个标记,一次一个(逐行),在整个Word文档中搜索这些单词,并将其替换为相应的替换项 我注意到页眉和页脚上的特殊标记没有被替换。我转向本文()并发现使用一系列范围(或循环使用文档中所有可用的故事范围)我能够做到这一点 我改进了我的代码,正如上面链接中所建议的那样,只要我的代码嵌入到我的“普通”Word文件中,就行了,从而使用Word中的VBA代码对另一个Word文档进行操作。但是,目标是在读取Excel文件时使用VBA Excel操作替换 当我把代码移到Excel时,我被一个自动化错误挂断了,vba,excel,runtime-error,Vba,Excel,Runtime Error,“运行时错误“-2147319779(8002801d)”:未注册自动化错误库。” 我一直在寻找答案,从查看注册表到使用“Word.Application.12”代替“Word.Application” 我有一台64位的Windows7机器,配有MicrosoftOffice2007。我选择了以下库: 卓越: Visual Basic For Applications Microsoft Excel 12.0对象库 OLE自动化 Microsoft Access 12.0对象库 Micr

“运行时错误“-2147319779(8002801d)”:未注册自动化错误库。”

  • 我一直在寻找答案,从查看注册表到使用“Word.Application.12”代替“Word.Application”
我有一台64位的Windows7机器,配有MicrosoftOffice2007。我选择了以下库:

  • 卓越:

    • Visual Basic For Applications
    • Microsoft Excel 12.0对象库
    • OLE自动化
    • Microsoft Access 12.0对象库
    • Microsoft Outlook 12.0对象库
    • Microsoft Word 12.0对象库
    • Microsoft Forms 2.0对象库
    • Microsoft Office 14.0对象库
  • 字:

    • Visual Basic For Applications
    • Microsoft Word 12.0对象库
    • OLE自动化
    • Microsoft Office 12.0对象库
我在Excel内部操作VBA时没有问题。通常,我会向这个函数传递一组字符串,但现在,我已经将字符串嵌入到函数中,就好像我只计划将一个字符串(对于任意数量的实例)与另一个预定字符串交换一样

Function Story_Test()
Dim File As String
Dim Tag As String
Dim ReplacementString As String

Dim a As Integer

Dim WordObj As Object
Dim WordDoc As Object
Dim StoryRange As Word.Range
Dim Junk As Long

Dim BaseFile As String

'Normally, these lines would be strings which get passed in
File = "Z:\File.docx"
Tag = "{{Prepared_By}}"
ReplacementString = "Joe Somebody"

'Review currently open documents, and Set WordDoc to the correct one
'Don't worry, I already have error handling in place for the more complex code
Set WordObj = GetObject(, "Word.Application")
BaseFile = Basename(File)
For a = 1 To WordObj.Documents.Count
    If WordObj.Documents(a).Name = BaseFile Then
        Set WordDoc = WordObj.Documents(a)
        Exit For
    End If
Next a

'This is a fix provided to fix the skipped blank Header/Footer problem
Junk = WordDoc.Sections(1).Headers(1).Range.StoryType


'Okay, this is the line where we can see the error.
'When this code is run from Excel VBA, problem.  From Word VBA, no problem.
'Anyone known why this is???
'***********************************************************************
For Each StoryRange In WordObj.Documents(a).StoryRanges
'***********************************************************************
    Do
        'All you need to know about the following function call is
        ' that I have a function that works to replace strings.
        'It works fine provided it has valid strings and a valid StoryRange.
        Call SearchAndReplaceInStory_ForVariants(StoryRange, Tag, _
          ReplacementString, PreAdditive, FinalAdditive)
        Set StoryRange = StoryRange.NextStoryRange
    Loop Until StoryRange Is Nothing
Next StoryRange

Set WordObj = Nothing
Set WordDoc = Nothing

End Function
应该是

For Each StoryRange In WordDoc.StoryRanges
因为你刚刚在上面的循环中分配了它

应该是

For Each StoryRange In WordDoc.StoryRanges

由于您刚刚在上面的循环中分配了它。

现在,我必须得出结论,因为我不可能进行相反的测试,在一个VBA环境中使用Microsoft Office 12对象库和在另一个VBA环境中使用Microsoft Office 14对象库是有区别的。我也没有改变的手段/授权,所以现在我必须得出结论,即两者之间的差异是罪魁祸首。因此,如果我继续前进并期望得到不同的结果,我会假设MicrosoftOffice12对象库是正确的库,其中14有一些我不知道的差异


感谢所有提供意见的人。如果您有任何其他建议,我们可以讨论并转发。谢谢

现在,我必须得出结论,因为我不可能进行相反的测试,在一个VBA环境中使用Microsoft Office 12对象库和在另一个VBA环境中使用Microsoft Office 14对象库是有区别的。我也没有改变的手段/授权,所以现在我必须得出结论,即两者之间的差异是罪魁祸首。因此,如果我继续前进并期望得到不同的结果,我会假设MicrosoftOffice12对象库是正确的库,其中14有一些我不知道的差异


感谢所有提供意见的人。如果您有任何其他建议,我们可以讨论并转发。谢谢

这是为了更新散布在正文和页眉页脚上的一组链接。 我写这篇文章不仅仅是因为我做了一系列的修复、包含和调整。 它向您展示了如何覆盖所有不同的部分,并且可以轻松地进行修改以在您的参数范围内工作。 完成后请发布您的最终代码

Public Sub UpdateAllFields()
Dim doc As Document
Dim wnd As Window
Dim lngMain As Long
Dim lngSplit As Long
Dim lngActPane As Long
Dim rngStory As Range
Dim TOC As TableOfContents
Dim TOA As TableOfAuthorities
Dim TOF As TableOfFigures
Dim shp As Shape
Dim sctn As Section
Dim Hdr As HeaderFooter
Dim Ftr As HeaderFooter

' Set Objects
Set doc = ActiveDocument
Set wnd = ActiveDocument.ActiveWindow

' get Active Pane Number
lngActPane = wnd.ActivePane.Index

' Hold View Type of Main pane
lngMain = wnd.Panes(1).View.Type

' Hold SplitSpecial
lngSplit = wnd.View.SplitSpecial

' Get Rid of any split
wnd.View.SplitSpecial = wdPaneNone

' Set View to Normal
wnd.View.Type = wdNormalView

' Loop through each story in doc to update
For Each rngStory In doc.StoryRanges
    If rngStory.StoryType = wdCommentsStory Then
        Application.DisplayAlerts = wdAlertsNone
        ' Update fields
        rngStory.Fields.Update
        Application.DisplayAlerts = wdAlertsAll
    Else
        ' Update fields
        rngStory.Fields.Update
    End If
Next

'Loop through text boxes and update
For Each shp In doc.Shapes
    With shp.TextFrame
        If .HasText Then
            shp.TextFrame.TextRange.Fields.Update
        End If
    End With
Next

' Loop through TOC and update
For Each TOC In doc.TablesOfContents
    TOC.Update
Next

' Loop through TOA and update
For Each TOA In doc.TablesOfAuthorities
    TOA.Update
Next

' Loop through TOF and update
For Each TOF In doc.TablesOfFigures
    TOF.Update
Next

For Each sctn In doc.Sections
    For Each Hdr In sctn.Headers
        Hdr.Range.Fields.Update
        For Each shp In Hdr.Shapes
            With shp.TextFrame
                If .HasText Then
                    shp.TextFrame.TextRange.Fields.Update
                End If
            End With
        Next shp
    Next Hdr
    For Each Ftr In sctn.Footers
        Ftr.Range.Fields.Update
        For Each shp In Ftr.Shapes
            With shp.TextFrame
                If .HasText Then
                    shp.TextFrame.TextRange.Fields.Update
                End If
            End With
        Next shp
    Next Ftr
Next sctn

' Return Split to original state
wnd.View.SplitSpecial = lngSplit

' Return main pane to original state
wnd.Panes(1).View.Type = lngMain

' Active proper pane
wnd.Panes(lngActPane).Activate

' Close and release all pointers
Set wnd = Nothing
Set doc = Nothing
End Sub

这是为了更新散布在正文和页眉页脚上的一组链接。 我写这篇文章不仅仅是因为我做了一系列的修复、包含和调整。 它向您展示了如何覆盖所有不同的部分,并且可以轻松地进行修改以在您的参数范围内工作。 完成后请发布您的最终代码

Public Sub UpdateAllFields()
Dim doc As Document
Dim wnd As Window
Dim lngMain As Long
Dim lngSplit As Long
Dim lngActPane As Long
Dim rngStory As Range
Dim TOC As TableOfContents
Dim TOA As TableOfAuthorities
Dim TOF As TableOfFigures
Dim shp As Shape
Dim sctn As Section
Dim Hdr As HeaderFooter
Dim Ftr As HeaderFooter

' Set Objects
Set doc = ActiveDocument
Set wnd = ActiveDocument.ActiveWindow

' get Active Pane Number
lngActPane = wnd.ActivePane.Index

' Hold View Type of Main pane
lngMain = wnd.Panes(1).View.Type

' Hold SplitSpecial
lngSplit = wnd.View.SplitSpecial

' Get Rid of any split
wnd.View.SplitSpecial = wdPaneNone

' Set View to Normal
wnd.View.Type = wdNormalView

' Loop through each story in doc to update
For Each rngStory In doc.StoryRanges
    If rngStory.StoryType = wdCommentsStory Then
        Application.DisplayAlerts = wdAlertsNone
        ' Update fields
        rngStory.Fields.Update
        Application.DisplayAlerts = wdAlertsAll
    Else
        ' Update fields
        rngStory.Fields.Update
    End If
Next

'Loop through text boxes and update
For Each shp In doc.Shapes
    With shp.TextFrame
        If .HasText Then
            shp.TextFrame.TextRange.Fields.Update
        End If
    End With
Next

' Loop through TOC and update
For Each TOC In doc.TablesOfContents
    TOC.Update
Next

' Loop through TOA and update
For Each TOA In doc.TablesOfAuthorities
    TOA.Update
Next

' Loop through TOF and update
For Each TOF In doc.TablesOfFigures
    TOF.Update
Next

For Each sctn In doc.Sections
    For Each Hdr In sctn.Headers
        Hdr.Range.Fields.Update
        For Each shp In Hdr.Shapes
            With shp.TextFrame
                If .HasText Then
                    shp.TextFrame.TextRange.Fields.Update
                End If
            End With
        Next shp
    Next Hdr
    For Each Ftr In sctn.Footers
        Ftr.Range.Fields.Update
        For Each shp In Ftr.Shapes
            With shp.TextFrame
                If .HasText Then
                    shp.TextFrame.TextRange.Fields.Update
                End If
            End With
        Next shp
    Next Ftr
Next sctn

' Return Split to original state
wnd.View.SplitSpecial = lngSplit

' Return main pane to original state
wnd.Panes(1).View.Type = lngMain

' Active proper pane
wnd.Panes(lngActPane).Activate

' Close and release all pointers
Set wnd = Nothing
Set doc = Nothing
End Sub

是的,看来“a”可能会被禁止,不是吗?但我怀疑这是否能解决在找到基本文件的情况下出现的问题,因为“a”变量超出了范围……a将从1开始,并向word文档的数量方向发展。但是,如果没有,则更复杂的代码允许使用“GetObject(,“Word.Application”)”进行错误处理,这实际上是在上面几行中捕获的。如果WordObj.Documents.Count=0的事件(出于某些疯狂的原因),那么for循环将在开始之前结束,从而从一开始就绕过它。在这种情况下,“WordDoc”不会被初始化,但不用担心,会有更多的错误处理。非常感谢。Tim Williams…虽然我可以理解为什么您会建议进行更改,但在提供的错误方面似乎没有区别。如果您要编写用于替换word文档任何位置中的字符串的简单代码,这些代码都是从Excel VBA中完成的,您会怎么做??也许我没有用最有效的方式??提前感谢您的时间@如果在WordObj.Documents集合中找不到基本文件,则KirkWoods a将超出绑定范围。您需要记住,在这种情况下,a的值为WordObj.Documents.Count+1。这就是为什么我警告可能的越界。但正如我所说,如果找到了基本文件,就不会发生这种情况。啊……好的,是的。这是有道理的。也许我需要将较低的代码移动到for循环中,以满足找到BaseFile的条件。非常感谢。是的,看来“a”可能变成“o”