我正在尝试以编程方式填写一个微软的单词形式。 我成功地能够这样做,如果字符串在255个字符下面,下面的代码如下,但是如果我尝试使用超过255个字符的字符串,那么字符串太长了...我怎么能超越这个限制?如果我在单词中打开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);
答案 0 :(得分: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();
}
}
答案 1 :(得分: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;
}
此方法要求您制作原始文档的副本并将其用作模板,因为在更新结束时,书签集合将被删除。