2017-06-01 14 views
0

マクロの目的は次のとおりです:メインフォルダ内に、複数のサブフォルダ、サブサブフォルダ、サブサブサブフォルダなどがあります。メインフォルダには、異なる拡張子(例:doc、docx)のMS Office Wordファイルがありますサブフォルダ/サブサブフォルダ/サブサブサブフォルダ。すべてのWordファイルが1秒/ 3階層フォルダにあるわけではありません。それらのWordファイルは、数千から数万、またはほんの数houndredsすることができます。MS Wordマクロプログラミング - このコードで何が間違っていますか?

特定の単語(「水」など)や特定の語句(「古い本」など)や文全体を検索する方法を探していますか?そのため、私が求めている自動化されたツールは、例外なく、特定のキーワードを含むWordファイルをすべて表示します。

コードは次のようであるとの問題が、次のされているので、私は(私に問題のある行を教えてくださいと何の代わりがあるはずです)私のコードで間違っているかを知りたいと思います:

私はすでに定義した後検索すると、私はすでにテキスト/文字列を定義した後、どちらが検索するパスは、次のメッセージが発生します。

「実行時エラー 『424』:必要なオブジェクト」

私は上をクリックした場合ボタンをデバッグして叫ぶOWコードの次のテキストの背景「」FSOはその後何もない場合は「」、したがって、黄色の矢印は、次の行の左側にある:

If FSO Is Nothing Then Set FSO = CreateObject("scripting.filesystemobject") 

CODE:

Sub Search() 
    ' 
    ' Search Macro 
    'Option Explicit 

    Public FSO As Object 'a FileSystemObject 
    Public oFolder As Object 'the folder object 
    Public oSubFolder As Object 'the subfolders collection 
    Public oFiles As Object 'the files object 

    Dim i As Long, strNm As String, strFnd As String, strFile As String, strList As String 

Sub FindTextInDocs() 
    ' Minimise screen flickering 
    Application.ScreenUpdating = False 

    Dim StrFolder As String 
    ' Browse for the starting folder 
    StrFolder = Trim(InputBox("What is the Top Folder?", "Get Top Folder")) 
    If StrFolder = "" Then Exit Sub 

    strFnd = InputBox("What is the string to find?", "File Finder") 
    If Trim(strFnd) = "" Then Exit Sub 

    strNm = ActiveDocument.FullName 
    ' Search the top-level folder 
    Call GetFolder(StrFolder & "\") 

    ' Search the subfolders for more files 
    Call SearchSubFolders(StrFolder) 

    ' Return control of status bar to Word 
    Application.StatusBar = "" 

    ' Restore screen updating 
    Application.ScreenUpdating = True 

    MsgBox i & " files processed." & vbCr & "Matches with " & strFnd & " found in:" & strList, vbOKOnly 
End Sub 


Function GetTopFolder() As String 
    GetTopFolder = "" 
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) 

    If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path 
    Set oFolder = Nothing 
End Function 


Sub SearchSubFolders(strStartPath As String) 
    If FSO Is Nothing Then Set FSO = CreateObject("scripting.filesystemobject") 

    Set oFolder = FSO.GetFolder(strStartPath) 
    Set oSubFolder = oFolder.subfolders 

    For Each oFolder In oSubFolder 
     Set oFiles = oFolder.Files 
     ' Search the current folder 
     Call GetFolder(oFolder.Path & "\") 
     ' Call ourself to see if there are subfolders below 
     SearchSubFolders oFolder.Path 
    Next 
End Sub 


Sub GetFolder(StrFolder As String) 
    strFile = Dir(StrFolder & "*.doc", vbNormal) 

    ' Process the files in the folder 
    While strFile <> "" 
     ' Update the status bar is just to let us know where we are 
     Application.StatusBar = StrFolder & strFile 
     i = i + 1 
     Call DocTest(StrFolder & strFile) 
     strFile = Dir() 
    Wend 
End Sub 


Sub DocTest(strDoc As String) 
    Dim Doc As Document 

    ' Open the document 
    If strDoc <> strNm Then 
     Set Doc = Documents.Open(strDoc, AddToRecentFiles:=False, ReadOnly:=True, Format:=wdOpenFormatAuto, Visible:=False) 
     With Doc 
      With .Range 
       With .Find 
        .Text = strFnd 
        .MatchCase = False 
        .MatchAllWordForms = False 
        .MatchWholeWord = False 
        .Execute 
        If .Found Then strList = strList & vbCr & strFile 
       End With 
      End With 
      .Close SaveChanges:=False 
     End With 
    End If 

    ' Let Word do its housekeeping 
    DoEvents 
    Set Doc = Nothing 
End Sub 

答えて

0

削除最初の行、Sub Search()

関連する問題