私の仕事では、スタイルのすべてのインスタンスを40語の文書(.doc)で検索する必要があります。テキストを取得したら、それをExcelシート(.xls)のセルに書きたいと思います。VBAはExcelのWord文書のすべてのスタイルを取得します
私が書いたコードでは、そのスタイルの最初のインスタンスを取得できますが、次のインスタンスを取得できません。無限ループに入り、なぜですか(それぞれのファイルに約10回の発生があります)。
私が間違っている場所を教えてください。それに Before running the macro ::私はそれから行きたい
ここでは私のコードです:
'==================================================================
' 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
完全な交換をポップアップ表示することができたテキストは、特定の文書構造に関してのどこにあるかにある程度依存します。特に重要なのは、テーブルセルが関与している場合です。私があなたのコードで見る別の潜在的な問題は、VBA(と私たち)がExcelオブジェクトを扱っているときとWordオブジェクトを使用しているときにはわからないことです。 'Range'は、例えば' rows'と 'columns'の両方で使用されます。この質問に明確に答えるためには、何らかの説明をする必要があります。 –