2012-10-23 104 views
0

我試圖通過編程方式填寫一個Microsoft Word表單。 我成功地能夠這樣做,如果字符串是在255字符下使用下面的代碼,但它表示字符串太長,如果我嘗試使用字符串超過255個字符...我如何通過這個限制?如果我用word打開word文檔,我可以輸入超過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

這裏的替代方法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#編譯我的切片長文本248字符的大小,然後通過查找循環和替換功能克服這個問題..這裏是我的代碼

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大小是從功能SUBSTRING(248),然後用<串聯它الوصف>哪位將使它255炭大小 - 字我心底搜索並與下一個替換spilted文本

當左邊的長文本小於248時,它會拋出異常並將您引導至catch語句,這會將最後少於248個字符的子字符串轉換爲spiltedText,而不會添加要搜索的單詞<。

代碼測試:)