2017-06-29 5 views
-4

私は、docx拡張子を持つTest1、Test2、およびTest3という3つのワードドキュメントを持つフォルダを持っています。C#:フォルダから複数のワードドキュメントを開く方法

私たちは手動でファイルを開きたい場合は、我々はファイルパスを指定し、次のコードを使用することができます知っている

Word.Application fileOpen = new Word.Application(); 
Word.Document document = fileOpen.Documents.Open(filePath); 

しかし、そのフォルダを選択するが、一度に一つのファイルを開くために、他の方法があるが? >上記繰り返し、最後にTest3はのために同じことを行う - Test2.docxを開き

  • 近い> - いくつかの変更を行います> - - >保存

    1. がTest1.docxを開き:私は、次の操作を実行したいと思います.docx

    私はどこでも検索しましたが、これに関連するものは見つかりませんでした。どんな助けでも感謝します、ありがとう。

  • 答えて

    0

    私の質問に対する答えが見つかりました。私は以下のサンプルコードと説明を含んでいます。

    このためには、次のものを使用する必要があります。

    Using System.IO; 
    

    この例では、すべての "doc"ファイルと "docx"ファイルをデスクトップディレクトリから取得します。

    //Directory to search through: 
    string sampleFilePath = @"C:\Users\YourUsername\Desktop"; 
    
    //Now we create a list to hold all the filepaths that exist in this directory 
    List<string> allFilePaths = new List<string>(); 
    //Extract all doc/docx files on Desktop 
    string[] start = Directory.GetFiles(sampleFilePath, "*doc"); 
         for (int begin = 0; begin < start.Length; begin++) 
         { 
          allFilePaths.Add(start[begin]); 
         } 
    
    //The above code does not extract doc/docx files that are contained inside folders that exist on desktop. 
    //So we need to get those filepaths separately. 
    string[] filePaths = Directory.GetDirectories(sampleFilePath); 
         for (int i = 0; i < filePaths.Length; i++) 
         { 
          //Any doc and docx files found in the subdirectories are added to the list 
          string[] files = Directory.GetFiles(filePaths[i], "*doc"); 
          for (int j = 0; j < files.Length; j++) 
          { 
           allFilePaths.Add(files[j]); 
          } 
          //Continue checking the subdirectories for more folders until you reach the end, then move on to the second subdirectory from the very beginning. 
          string[] checkMoreDirectories = Directory.GetDirectories(filePaths[i]); 
          checkForMoreDirectories(checkMoreDirectories, ref allFilePaths); 
         } 
    

    以下に、checkForMoreDirectoriesメソッドを示します。

    static void checkForMoreDirectories (string[] checkMoreDirectories, ref List<string> myList) 
        { 
         //This function calls itself recursively for every subdirectory within the previous subdirectory until it reaches the end where there are no more folders left 
         //Continuously add any doc/docx files found before going deeper into the subdirectory 
         for (int i = 0; i < checkMoreDirectories.Length; i++) 
         { 
          string[] files = Directory.GetFiles(checkMoreDirectories[i]); 
          for (int j = 0; j < files.Length; j++) 
          { 
           myList.Add(files[j]); 
          } 
          string[] temp = Directory.GetDirectories(checkMoreDirectories[i]); 
          //Recursive call 
          checkForMoreDirectories(temp, ref myList); 
         } 
        } 
    

    は今、あなたは、DOC/DOCXファイルを保存する多くのサブフォルダと1000個のフォルダーごとを持っている場合でも、あなたはあなたのリストに保存されているもののそれぞれのためのファイルパスを持っています。その後、このリストにアクセスして、Microsoft Word Interopsを使用して各ファイルを開き、変更を加えることができます。

    +5

    H @ @Dylan、他の回答のOPは、あなたのコメント。あなたがそれを感じるなら、あなたは[meta](https://meta.stackoverflow.com/q/351673/578411)でチンミングしてもいいですか?我々は噛んでいません... – rene

    +0

    ここにいくつか言及するべきことがあります。 1.私は彼の答えを投票しましたが、「評判が15未満の投票は記録されますが、公に表示を変えない」と言います。 2.はい彼の答えは正しいと私は1で言及した通りでした。 3.私が投稿したコードは彼とは大きく異なっており、相互に存在するより大きな規模のサブフォルダのために働いていました。 – Dylan

    +0

    はい、評判の要件は確かに投稿者のメタポストに気付きました。私はそれを私のメタ回答に明示しました。フォルダの解決策が必要だったことは、他の回答者の驚きを私が思う質問からはっきりとは分かりませんでした。 AFAICT我々は今すべて良いです。あなたのコメントをありがとう、あなたの一日をお楽しみください。 – rene

    4

    この場合、&のファイルを開くフォルダを知っているとします。

    まず、あなたがその名前空間内でSystem.IO

    using System.IO; 
    

    をインポートするんだ、Directory.GetFiles()は、文字列配列内のあなたのファイル名を与えます。

    private static void FolderReader() 
    { 
        string folderPath = @"C:\someFolder\"; 
        Word.Application fileOpen = new Word.Application(); 
        string[] filePaths = Directory.GetFiles(folderPath); 
    
        foreach (string filePath in filePaths) 
        { 
         Word.Document document = fileOpen.Documents.Open(filePath); 
         // perform continue with your algorithm... 
         // close the file when you're done. 
        } 
    } 
    

    うまくいけば、これで十分です。がんばろう。

    関連する問題