2017-06-29 70 views
-4

我有一個文件夾,其中包含三個名爲Test1,Test2和Test3的word文檔,所有這些文檔都帶有docx擴展名。如何打開文件夾中的多個word文檔

我知道,如果我們要手動打開一個文件,我們可以指定文件路徑,並使用下面的代碼:

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

但是否有任何其他的方式來選擇文件夾,但在同一時間打開一個文件?我想做到以下幾點:

  1. 打開Test1.docx - >做一些修改 - >保存 - >關閉
  2. 打開Test2.docx - >重複以上,最後做Test3的同.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); 
     } 
    } 

現在,即使你有每個1000個文件夾與存儲DOC/DOCX文件很多子,你將不得不爲那些存儲在列表中的每一個文件路徑。然後,您可以訪問此列表,並通過使用Microsoft Word Interop打開每個文件並進行更改等。

+5

Hé@Dylan,其他答案的OP在他們根據您的意見提供答案時預期得到不同的結果。如果你覺得這是你介意在[meta](https://meta.stackoverflow.com/q/351673/578411)?。我們不咬... – rene

+0

這裏有幾件事要提到。 1.我投了他的答案,但它說,「投票少於15聲望的投票記錄,但不公開更改顯示」。 2.是的,他的回答是正確的,我的確如我在1中提到的那樣。 3.我發佈的代碼與他的顯着不同,它適用於存在於另一箇中的較大規模的子文件夾。 – Dylan

+0

是的,聲譽要求確實被評論者注意到了,並且我在meta回答中明確表達了這一點。你需要一個文件夾的解決方案並不清楚我認爲這個問題是否讓其他答覆者感到意外。 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. 
    } 
} 

希望這足以讓你開始。祝你好運。