2012-10-23 3 views
0

프로그래밍 방식으로 Microsoft Word 형식을 채우려고합니다. 문자열이 255 자 미만인 경우 다음과 같은 코드를 사용하여 성공적으로 수행 할 수 있습니다. 그러나 255 자 이상의 문자열을 사용하면 문자열이 너무 길다는 메시지가 나타납니다.이 제한을 어떻게 지나치게 받습니까? 단어 doc을 열면 문제없이 255 자 이상을 입력 할 수 있습니다. 누구든지 C# 코드를 통해 더 많은 문자를 입력하는 방법을 알고 있습니까?255 자 이상의 단어 형식 필드 채우기

object fileName = strFileName; 
object readOnly = false; 
object isVisible = true; 
object missing = System.Reflection.Missing.Value; 
//open doc 
_oDoc = _oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly, 
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); 

_oDoc.Activate(); 

//write string 
_oDoc.FormFields[oBookMark].Result = value; 

//save and close 
oDoc.SaveAs(ref fileName, 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); 


_oWordApplic.Application.Quit(ref missing, ref missing, ref missing); 

답변

0

아니요,이 제한 사항은 책갈피 필드의 Result 속성을 사용하여 무시할 수 없습니다.
내가 내 텍스트로 책갈피를 교체 문제를 우회 한

// Loop on the bookmarks collection 
foreach(Bookmark bk in workDoc.Bookmarks) 
{ 
    currentData = GetCurrentDataForBookmark(bk.Name); 
    // Insert the value or cut away the bookmark if data is zero lenght 
    bk.Select(); 
    if(currentData.Length == 0) 
     _myWordApp.Selection.Cut(); 
    else 
     _myWordApp.Selection.Text = currentData; 
} 

이 방법을 사용하면 때문에 북마크 수집이 전멸하는 업데이트의 끝에서 시킴으로 빨리 문서의 복사본을 만들고 템플릿처럼 사용해야합니다 아웃.

1

여기 .. ​​여기 내가 발견 통해 반복과 기능을 대체 한 후 248 문자 크기로 긴 텍스트를 슬라이스하고이 문제를 극복 Microsoft는 http://support.microsoft.com/kb/163192

using Word = Microsoft.Office.Interop.Word; 
public void CreatePackage(string filePath, string longText) 
{ 
    Word.Application wordApp = new Word.Application(); 
    Word.Document doc = wordApp.Documents.Open("MyOriginalDoc.docx"); 
    try 
    { 
     //If the document is protected Select() will throw an exception 
     if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection) 
     { 
      doc.Unprotect(); 
     } 

     foreach (Microsoft.Office.Interop.Word.FormField f in doc.FormFields) 
     { 
      //My situation prohibits me from adding bookmarks to the document, so instead I'm 
      //using sentinel values that I search the doc for. 
      if (f.Result.Equals("MySentinalValue")) 
      { 
       //You need some easily removed dummy characters in the field for this to work. 
       f.Result = "****"; 

       //If you don't follow these next three steps you'll end up replacing the formfield 
       //rather than inserting text into it 
       f.Range.Select(); 
       wordApp.Selection.Collapse();    
       wordApp.Selection.MoveRight(Word.WdUnits.wdCharacter, 1); 

       //Insert the text 
       wordApp.Selection.TypeText(longText); 

       //Now remove the dummy characters. If you don't re-select the range, Find won't catch the 
       //the first one. 
       f.Range.Select(); 
       Word.Find find = wordApp.Selection.Find; 
       object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; 
       find.ClearFormatting(); 
       find.Text = "*"; 
       find.Replacement.ClearFormatting(); 
       find.Replacement.Text = ""; 
       find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); 
      } 
     //Restore the doc protections. Note that if NoReset != true all data entered in fields will be lost 
     doc.Protect(Word.WdProtectionType.wdAllowOnlyFormFields, true); 
     doc.SaveAs(filePath); 
    } 
    catch (System.Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
    finally 
    { 
     doc.Close(); 
     wordApp.Quit(); 
    } 
} 
0

에서 제공하는 해결의 내 C# 번역의 내 코드입니다

int repeat; 
     if (Value.Count() > 254) 
      repeat = ((Value.Count()/255)); 
     //string spiltedText; 
     else 
      repeat = 0; 

     if (repeat > 0) 
     { 
      for (int i = 0; i <= repeat; i++) 
      { 
       try { spiltedText = Value.Substring(i * 248, 248); spiltedText += "<الوصف>"; } 
       catch { spiltedText = Value.Substring(i * 248, Value.Count() - (i * 248) - 1); } 
       range.Find.Execute(findtext, findmatchcase, findmatchwholeword, 
        findmatchwildcards, findmatchsoundslike, findmatchallwordforms, findforward, 
        findwrap, findformat, spiltedText, findreplace, missing, 
        missing, missing, missing); 
      } 
     } 

     else 
     range.Find.Execute(findtext, findmatchcase, findmatchwholeword, 
       findmatchwildcards, findmatchsoundslike, findmatchallwordforms, findforward, 
       findwrap, findformat, spiltedText, findreplace, missing, 
       missing, missing, missing); 

    } 

주목하라 spiltedText 크기 함수 서브 스트링 (248)이며, 다음 < 함께 연쇄 الوصف>는 255 문자 단어를 검색하고, 다음 spilted 교체 i`ll 사이즈 - 것 - 어떤 텍스트

왼쪽 긴 텍스트가 248보다 작 으면 throw하고 catch 문으로 연결되는 예외를 throw합니다. 그러면 검색 할 단어를 < 설명>없이 spiltedText로 바꿉니다.

코드가 테스트 됨 :)

관련 문제