填写超过255个字符的字形字段 [英] Fill in word form field with more than 255 characters

查看:156
本文介绍了填写超过255个字符的字形字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式填写Microsoft字词形式. 如果使用以下代码,如果字符串小于255个字符,则可以成功执行此操作,但是如果我尝试使用大于255个字符的字符串,则表示字符串太长...如何克服此限制?如果我在word中打开doc这个词,则可以输入255个以上的字符,而不会出现问题.有人知道如何通过C#代码输入更多字符吗?

I am trying to programmaticly fill in a microsoft word form. I am successfully able to do so if the string is under 255 chars with the following code below, however it says the string is too long if i try and use a string over 255 chars... How do I get past this limitation? If I open the word doc in word I can type in more than 255 chars without a problem. Does anyone know how to input more characters via c# code?

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

推荐答案

这是Microsoft在

Here's my C# translation of the workaround Microsoft offers at 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();
    }
}

这篇关于填写超过255个字符的字形字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆