【问题标题】:Find specific string in MS Word document and set a bookmark at that location在 MS Word 文档中查找特定字符串并在该位置设置书签
【发布时间】:2014-03-25 22:23:32
【问题描述】:

大家好,

我需要通过 Excel VBA 在 MS Word 文档中查找特定字符串(格式为标题 1),然后在该位置设置书签。一旦我得到了搜索字符串的范围,后一部分应该不是问题。

但是,我似乎不知道如何使用 Excel VBA 在 Word 中搜索字符串。

我尝试了以下(缩短):

Option Explicit

Sub exportWord(button As IRibbonControl)
Application.ScreenUpdating = False

Dim wrdDoc As Word.document
Dim wrdApp As Word.Application
Dim wrdLoc As String
wrdLoc = ThisWorkbook.Worksheets("Konfiguration").Range("changelogPath")

Set wrdApp = New Word.Application
wrdApp.Visible = True

Set wrdDoc = wrdApp.Documents.Open(wrdLoc)

Dim wrdRange As Word.Range
Dim searchString As String

Set wrdRange = wrdDoc.Range
searchString = "Test"

With wrdRange.Find
    .Text = searchString 
    .Replacement.Text = "Replacement Test"
    .wrap = wdFindContinue
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Highlight = False
    .Execute Replace:=wdReplaceAll
End With
End Sub

这实际上不会做任何事情,但我只是想检查发现的工作。但它并没有,Excel 只是崩溃而没有任何 VBA 错误或任何东西。只是说了一些“程序没有响应,应用程序遇到问题将被关闭”的行

有谁知道为什么 Excel 会在没有任何正确错误消息的情况下崩溃?或者如何在 Word 文档中正确实现搜索?

最好的问候,

扎扎

【问题讨论】:

    标签: vba excel ms-word


    【解决方案1】:

    尝试了一些不同的方法并解决了它:

     For Each rngStory In wrdDoc.StoryRanges
            With rngStory.Find
                .Replacement.ClearFormatting
                .Text = "Änderungen in Test12345"
                .Replacement.Text = "test"
                .wrap = wdFindContinue
                .ExecuteReplace:=wdReplaceAll
            End With
        Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多