2011-03-22 6 views
4

테이블의 첫 번째 열에서 셀을 가져 오려고합니다. 표에 "셀 너비가 혼합 된 열이 포함되어 있으므로"Foreach(Cells c in rng.Tables[1].Columns[1].Cells) "예외가 발생합니다.MS Word에서 셀 너비가 다른 테이블의 열에 액세스하는 방법

예에 대한

: 첫 번째 행, 4 개 세포가와 두 번째 행에서, 단 2 전지 (2 셀 병합)가

오류 메시지 : "테이블이 때문에 컬렉션의 개별 열을 액세스 할 수 없습니다 혼합 세포 폭을 가지고 "그래서 모든 셀 반복하는 것처럼 대신 루프를 사용할 수 있습니다

Document oDoc = open word document 
foreach (Paragraph p in oDoc.Paragraphs) 
    { 
    Range rng = p.Range; 
    /* 

    */ 
    foreach (Cell c in rng.Tables[1].Columns[1].Cells) 
    { 
    //.... 
    } 
} 

답변

4

대신 두 번째 루프에 foreach 루프를 사용 :.

 for (int r = 1; r <= rng.Tables[1].Row.Count; r++) 
     { 
      for (int c = 1; c <= rng.Tables[1].Columns.Count; c++) 
      { 
       try 
       { 
        Cell cell = table.Cell(r, c); 
        //Do what you want here with the cell 
       } 
       catch (Exception e) 
       { 
        if (e.Message.Contains("The requested member of the collection does not exist.")) 
        { 
         //Most likely a part of a merged cell, so skip over. 
        } 
        else throw; 
       } 
      } 
     } 
+0

상어, 너는 내 날을 구했다. 고맙습니다 – Naruto

관련 문제