2013-09-25 2 views
2

Open XML을 사용 중이며 단어 파일의 헤더에있는 텍스트를 변경해야합니다. 문서의 특정 단락을 변경하려면 다음 코드를 사용하십시오.OpenXml 단어 파일의 머리글에서 텍스트 편집

Dim body = wdDoc.MainDocumentPart.Document.Body 
      Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)() 
      Dim header = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Header)() 


      For Each para In paras 
       For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)() 
        For Each testo In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)() 
         If (testo.Text.Contains("<$doc_description$>")) Then 
          testo.Text = testo.Text.Replace("<$doc_description$>", "replaced-text") 
         End If 
        Next 
       Next 
      Next 

미리 감사드립니다.

답변

6

당신은 워드 문서의 헤더에 태그를 교체하려면 다음 코드를 사용할 수 있습니다

Using wdDoc = WordprocessingDocument.Open("header.docx", True) 

    For Each headerPart In wdDoc.MainDocumentPart.HeaderParts 
    For Each currentParagraph In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)() 
     For Each currentRun In currentParagraph.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Run)() 
     For Each currentText In currentRun.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)() 

      If (currentText.Text.Contains("$doc-description$")) Then 
      Console.WriteLine("found") 
      currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text") 
      End If 

     Next 
     Next 
    Next 
    Next 

End Using 

첫째, 워드 문서의 모든 HeaderParts을 열거. 그런 다음 대체 할 태그가 들어있는 Text 요소 을 모두 검색하십시오. 그런 다음 태그를 텍스트로 바꿉니다.

<>_ 자없이 태그를 사용해야합니다. 태그에 이러한 문자가 포함되어있는 경우 word는 여러 개의 Text 요소 사이에서 텍스트를 분할합니다.

당신이 테이블에 텍스트를 변경하려면 (또는 다른 요소에) 다만 모든 Text 요소에 대한 검색 : 응답이 실제로 작동을 위해 :) 나는 또한 함께 노력 을

Using wdDoc = WordprocessingDocument.Open("header.docx", True) 

    For Each headerPart In wdDoc.MainDocumentPart.HeaderParts 
    For Each currentText In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)() 

     If (currentText.Text.Contains("$doc-description$")) Then 
     Console.WriteLine("found") 
     currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text") 
     End If 

    Next 
    Next 

End Using 
+0

@ andrea85 : 테이블에 포함 된 텍스트를 바꾸는 방법을 보여주기 위해 답변을 업데이트했습니다. 답변의 왼쪽에있는 빈 화살표를 클릭하면 도움이된다면 동의/upvote 내 대답. – Hans

0

감사 다음 코드 :

 
    For Each headref In mainDoc.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.HeaderReference)() 
    headerRelationshipId = headref.Id.Value 
    headerType = headref.Type.Value.ToString() 
    Dim header01 As DocumentFormat.OpenXml.Wordprocessing.Header = DirectCast(wdDoc.MainDocumentPart.GetPartById(headerRelationshipId), HeaderPart).Header 
    Dim headerText As New StringBuilder() 

    For Each text00 As DocumentFormat.OpenXml.Wordprocessing.Text In header01.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)() 
     If (text00.Text.Contains("")) Then 
      text00.Text = text00.Text.Replace("", "replaced-text") 
     End If 
    Next 
Next 

단락 대신 텍스트를 변경하고 싶습니까?

1

한스 응답에서 C#으로 이식되었습니다!

//Gets all the headers 
foreach (var headerPart in doc.MainDocumentPart.HeaderParts) 
{ 
     //Gets the text in headers 
     foreach(var currentText in headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>()) 
     { 
      currentText.Text = currentText.Text.Replace("[Thanks]", "Thanks"); 
     } 
} 
관련 문제