0

プログラムでMicrosoftWordフォームに入力しようとしています。以下のコードで文字列が255文字未満の場合は正常に実行できますが、255文字を超える文字列を使用しようとすると、文字列が長すぎると表示されます...この制限を回避するにはどうすればよいですか?docという単語をwordで開くと、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);
4

3 に答える 3

1

Microsoft が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();
    }
}
于 2013-05-10T18:36:49.367 に答える
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;
}

このアプローチでは、元のドキュメントのコピーを作成し、それをテンプレートのように使用する必要があります。これは、更新の最後にブックマーク コレクションが消去されるためです。

于 2012-10-23T23:38:42.633 に答える
0

長いテキストを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 ステートメントに導かれます。このステートメントは、検索する単語を追加せずに、最後の 248 文字未満を spiltedText にサブストリング化します。

コードはテストされています:)

于 2015-08-25T06:58:51.147 に答える