2016-07-07 3 views
0

단어 문서의 특정 영역에 텍스트를 삽입하는 데 도움이 필요합니다.기존 Word 문서에 텍스트를 삽입하는 방법 C# 정확한 위치에?

.txt 파일의 텍스트를 Word 문서에 삽입하는 다음 코드가 있지만 어디서나 정확한 영역에 배치해야합니다.

내 코드 :

 string[] readText = File.ReadAllLines(@"p:\CARTAP1.txt"); 

     foreach (string s in readText) 
     { 
      Console.WriteLine(s); 
     } 


     Application application = new Application(); 
     Document document = application.Documents.Open(@"P:\PreciboCancelado.doc"); 
     application.Visible = true; 
     application.Selection.TypeText(readText[2]); 
+3

가능한 중복 : // 유래 .com/questions/98484/how-to-a-file-using-c-sharp) – meJustAndrew

+1

구멍 문서를 읽고 다시 쓰면서 새로운 텍스트를 추가해야합니다. 누군가가 이것 [여기] (http://stackoverflow.com/questions/98484/how-to-insert-characters-to-a-file-using-c-sharp)을 물었고 비슷한 대답을 얻었습니다. – meJustAndrew

+1

나는 워드 문서에 익숙하지가 않다.하지만 그것이 나라면 Intellisence 및/또는 [디버거]를 이용할 것이다. (http://www.codeproject.com/Articles/79508/Mastering-Debugging -in-Visual-Studio-A-Beginn)과'Document'에서 발견 된 속성과 메소드를 확인하십시오. 'Document' 객체를 편집하여 저장하고 기존 파일을 덮어 쓸 것입니다. –

답변

0

당신은 Word에서 책갈피로를 시도 할 수 있습니다. 아마도이 링크는 당신에게 http://gregmaxey.mvps.org/word_tip_pages/insert_text_at_or_in_bookmark.html

+0

[Help Center] (http://stackoverflow.com/help)/how-to-answer) : exte에 대한 링크 리소스가 권장되지만 링크 주변에 컨텍스트를 추가하여 동료 사용자가 그것이 무엇인지, 왜 존재하는지 알 수 있도록하십시오. 대상 사이트에 도달 할 수 없거나 영구적으로 오프라인 상태가되는 경우 중요한 링크의 가장 중요한 부분을 항상 인용하십시오. – Adam

3

도움이 나는 마누엘이 언급 한 것처럼 책갈피를 사용하여 할 수있는 방법을 발견 :

string[] readText = File.ReadAllLines(@"p:\CARTAP1.txt"); 

    // declare objects and variables 
    object fileName = @"P:\PreciboCancelado.doc"; 
    object readOnly = false; 
    object isVisible = true; 
    object missing = System.Reflection.Missing.Value; 

    // create instance of Word 
    Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 


    // Create instance of Word document 
    Microsoft.Office.Interop.Word.Document oWordDoc = new Document(); 


    // Open word document. 
    oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref readOnly, 
    ref missing, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, 
    ref missing, ref missing, ref missing, ref missing, ref missing); 

    oWordDoc.Activate(); 

    // Debug to see that I can write to the document. 
    oWordApp.Selection.TypeText("This is the text that was written from the C# program! "); 
    oWordApp.Selection.TypeParagraph(); 


    // Example of writing to bookmarks each bookmark will have exists around it to avoid null. 
    if (oWordDoc.Bookmarks.Exists("Fecha")) 
     { 
      // set value for bookmarks   
      object oBookMark = "Fecha"; 
      oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = readText[2] ; 

      oBookMark = "Nombre"; 
      oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = readText[3]; 

    } 

    // Save the document. 
    oWordApp.Documents.Save(ref missing, ref missing); 


    // Close the application. 
    oWordApp.Application.Quit(ref missing, ref missing, ref missing); 
[C#을 사용하여 파일에 문자를 삽입하는 방법 (HTTP의
관련 문제