【问题标题】:Fill in word form field with more than 255 characters填写超过 255 个字符的 word 表单域
【发布时间】:2015-08-25 14:58:51
【问题描述】:

我正在尝试以编程方式填写 Microsoft Word 表单。 如果字符串小于 255 个字符,我可以使用下面的代码成功地做到这一点,但是如果我尝试使用超过 255 个字符的字符串,它会说字符串太长......我该如何克服这个限制?如果我在 word 中打开 word doc,我可以毫无问题地输入超过 255 个字符。有谁知道如何通过 c# 代码输入更多字符?

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);

【问题讨论】:

    标签: c# ms-word com-interop office-interop


    【解决方案1】:

    这是我对微软在http://support.microsoft.com/kb/163192提供的解决方法的 C# 翻译

    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();
        }
    }
    

    【讨论】:

      【解决方案2】:

      不,使用书签字段的 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;
      }
      

      此方法要求您制作原始文档的副本并将其用作模板,因为在更新结束时书签集合被清除。

      【讨论】:

        【解决方案3】:

        我通过将长文本切成 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);
        
            }
        

        请注意,函数 Substring() 的 spiltedText 大小为 248,然后将其与 连接 - 这将使其大​​小为 255 字符 - 我将搜索并替换为下一个 spiltedText 的单词

        当左侧长文本小于 248 时,它会抛出异常并导致您进入 catch 语句,该语句会将最后一个 less-248-chars 子串到 spiltedText 中,而不添加 将搜索的单词..

        代码已经过测试:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-15
          相关资源
          最近更新 更多