2012-05-24 4 views
1

에 필드를 편지 병합합니다. 나는 다음에 내 응용 프로그램에서 호출보내기 텍스트 난 그냥 내가이 작업을 얻을 수 있습니다 테스트하는 현재 단일 MERGEFIELD로 설정 한 간단한 워드 템플릿에 텍스트를 보내려면 다음 코드를 사용하고 마이크로 소프트 워드 2010

public static void ReplaceMailMergeField(string pWordDoc, string pMergeField, string pValue) 
{ 
    object docName = pWordDoc; 
    object missing = Missing.Value; 
    Word.MailMerge mailMerge; 
    Word._Document doc; 
    Word.Application app = new Word.Application(); 
    app.Visible = false; 
    doc = app.Documents.Open(ref docName, ref missing, ref missing, ref missing, ref missing, 
              ref missing, ref missing, ref missing, ref missing, 
              ref missing, ref missing, ref missing, ref missing, 
              ref missing, ref missing, ref missing); 
    mailMerge = doc.MailMerge; 
    foreach (Word.MailMergeField f in mailMerge.Fields) 
    { 
     if (f.Code.Text.IndexOf("MERGEFIELD \"" + pMergeField + "\"") > -1) 
     { 
      f.Select(); 
      app.Selection.TypeText(pValue); 
     } 
    } 
    object saveChanges = Word.WdSaveOptions.wdSaveChanges; 
    doc.Close(ref saveChanges, ref missing, ref missing); 
    app.Quit(ref missing, ref missing, ref missing); 
} 

:

string pWordDoc = @"C:\Users\Pete-Laptop\Documents\CMS Document Mangement\Word Template.dotx"; 
cDocument.ReplaceMailMergeField(pWordDoc, "fieldAddress1", "Put address here!"); 

을하지만 아무 일도 일어나지 않는다 다음과 같이
내가 사용하고있는 코드입니다. 코드를 단계별로 실행하면 app.Documents.Open까지 도달 한 후 고정 된 것처럼 보입니다. 응용 프로그램이 내 Word 문서를 찾을 수 없기 때문에 이것이라고 생각합니다. 전체 파일 경로를 파일 이름 매개 변수로 보내는 것이 맞습니까? 그렇지 않다면 응용 프로그램이 내 Word 서식 파일을 찾을 수있는 방법은 무엇입니까?

답변

4

은 다음과 같습니다

public static void TextToWord(string pWordDoc, string pMergeField, string pValue) 
    { 
     Object oMissing = System.Reflection.Missing.Value; 
     Object oTrue = true; 
     Object oFalse = false; 
     Word.Application oWord = new Word.Application(); 
     Word.Document oWordDoc = new Word.Document(); 
     oWord.Visible = true; 
     Object oTemplatePath = pWordDoc; 
     oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); 
     foreach (Word.Field myMergeField in oWordDoc.Fields) 
     { 
      Word.Range rngFieldCode = myMergeField.Code; 
      String fieldText = rngFieldCode.Text; 
      if (fieldText.StartsWith(" MERGEFIELD")) 
      { 
       Int32 endMerge = fieldText.IndexOf("\\"); 
       Int32 fieldNameLength = fieldText.Length - endMerge; 
       String fieldName = fieldText.Substring(11, endMerge - 11); 
       fieldName = fieldName.Trim(); 
       if (fieldName == pMergeField) 
       { 
        myMergeField.Select(); 
        oWord.Selection.TypeText(pValue); 
       } 
      } 
     } 
    } 

이 코드는 here에서 orignally입니다.

+1

이렇게 winform 응용 프로그램을 빌드 할 것입니다. 아이디어를 제공해 주셔서 감사합니다. –

3

당신이 시도 할 수 :

_doc = _app.Documents.Add(ref _docName , ref _missing , ref _missing , ref _missing); 

대신

_doc = _app.Documents.Open(.......); 

이 라인이 맞다면 다음 확인 :

if (f.Code.Text.IndexOf("MERGEFIELD \"" + pMergeField + "\"") > -1) 

내가이 작동하는 기능 코드가 방법

다음과 같이 내가 사용하고 마지막 코드는 나를 위해 작동 5,983,210
+0

감사 스티브,하지만 당신은 내가 당신의 코드와 함께하시기 바랍니다 대체하고 내 코드의 어떤 라인을 명확히 할 수있다. – PJW

+0

위의 대답 – Steve

+0

@ 스티브, 당신은 http://stackoverflow.com/questions/19980381/perform-microsoft-word-interop-mail-merge-in-에서 내 문제 이상에의 무게를 달 수 있다고 생각하는 일부 코드 및 설명 추가 기음? 필자는 제안을하기 위해 파이핑을하는 사람이 없었으며, 여기에서 편지 병합 문제에 대해 처음으로 알게 된 평판 좋은 사용자입니다. –