简体   繁体   English

VBA从Excel获取Word文档中样式的所有出现

[英]VBA get all occurences of a style in a Word document from Excel

For my work I have to find all the instances of a style in 40 word documents (.doc). 对于我的工作,我必须在40个单词的文档(.doc)中找到样式的所有实例。 Once I get the text, I want to write it in a cell in my excel sheet (.xls). 收到文字后,我想将其写入Excel工作表(.xls)的单元格中。

With the code I wrote I can get the first instance of that style but I cannot get the next one, it goes into an infinite loop and I don't now why (there are about 10 occurrences on each file). 使用我编写的代码,我可以获取该样式的第一个实例,但是我不能获取下一个样式,它进入无限循环,现在我不知道为什么了(每个文件中大约有10次出现)。

Could you tell me where I am wrong? 你能告诉我我哪里错了吗?

I want to go from that: Before running the macro to that: After running the macro 我想从那开始: 在运行宏之前在运行宏之后

Here is my code: 这是我的代码:

'==================================================================
' Declarations
'==================================================================

Dim ObjWord As Object ' Word application object

'==================================================================
' Macro
'==================================================================

Public Sub Macro()

Dim row As Integer
row = 9 'first available row

Set ObjWord = CreateObject("word.application")

Worksheets("Sheet 2").Activate

While (Cells(row, 2).Value <> "End of file list")

    Set file = ObjWord.documents.Open(ThisWorkbook.path & ".\" & Cells(row, 1).Hyperlinks(1).Address)


    Set currentRange = file.Range

    currentRange.Find.ClearFormatting
    currentRange.Find.Forward = True
    currentRange.Find.Text = ""
    currentRange.Find.Style = "MyStyle"
    bFind = currentRange.Find.Execute

    Do While bFind 'here is the endless loop
         row = row + 1
         StyleValue= currentRange.Text 'I get stuck with the first value :-(
         Rows(row).EntireRow.Insert
         Cells(row, 2).Value = StyleValue
         bFind = currentRange.Find.Execute
    Loop

    file.Close

    row = row + 1 ' next File
Wend

ObjWord.Quit
End Sub

I think it needs to be this: 我认为应该是这样的:

Do While currentRange.Find.Found 
    currentRange.Find.Execute 
Loop 

I had to make a number of changes to get it to work for me, mostly because I have Option Explicit on and declare all variables. 我必须进行一些更改才能使其适用于我,这主要是因为我启用了Option Explicit并声明了所有变量。

However while going through it something that may be the issue if it is looping on the first find, is that you have to move the selection past the find to find the next one. 但是,在进行遍历时,如果它在第一个查找中循环播放,则可能是一个问题,那就是您必须将所选内容移到查找之后才能找到下一个。

Do While bFind 'here is the endless loop
    row = row + 1
    StyleValue= currentRange.Text 'I get stuck with the first value :-(
    Rows(row).EntireRow.Insert
    Cells(row, 2).Value = StyleValue

    currentRange.SetRange currentRange.End, currentRange.End

    bFind = currentRange.Find.Execute
Loop 

If that doesn't work I can pop up a full replacement later 如果那不起作用,我可以稍后弹出一个完整的替代品

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM