/// <Summary>
/// Personalname () is used to call
/// </Summary>
/// <Param name = "oldname"> name to be replaced </param>
/// <Param name = "newname"> name to be replaced </param>
Public void replacename (string oldname, string newname)
{
// Word. Find findobject = word. selection. Find;
// Findobject. clearformatting ();
// Findobject. Text = oldname;
// Findobject. Replacement. clearformatting ();
// Findobject. Replacement. Font. Name = "";
// Findobject. Replacement. Text = newname;
// Object replaceall = word. wdreplace. wdreplaceall;
// Findobject. Execute (ref omissing, ref omissing,
// Ref omissing, ref omissing,
// Ref replaceall, ref omissing );
// Object replaceall = word. wdreplace. wdreplaceall;
// Object missing = type. missing;
//// First clear any existing format setting options, and then set the search string stroldtext.
// This. Word. selection. Find. clearformatting ();
// Word. selection. Find. Text = oldname;
// Word. selection. Find. Replacement. clearformatting ();
// Word. selection. Find. Replacement. Text = newname;
// Word. selection. Find. Replacement. Font. Name = "";
// Word. selection. Find. Execute (
// Ref missing, ref missing,
// Ref missing, ref missing,
// Ref replaceall, ref missing );
Object myfind = word. selection. Find;
Object findtext = oldname;
Object replacetext = newname;
Object replaceall = word. wdreplace. wdreplaceall;
Try
{
Object [] parameters;
Parameters = new object [15];
Parameters [0] = findtext;
Parameters [1] = omissing;
Parameters [2] = omissing;
Parameters [3] = omissing;
Parameters [4] = omissing;
Parameters [5] = omissing;
Parameters [6] = omissing;
Parameters [7] = omissing;
Parameters [8] = omissing;
Parameters [9] = replacetext;
Parameters [10] = replaceall;
Parameters [11] = omissing;
Parameters [12] = omissing;
Parameters [13] = omissing;
Parameters [14] = omissing;
// Word. selection. Find. Replacement. Font. Name = "";
Myfind. GetType (). invokemember ("execute", bindingflags. invokemethod, null, myfind, parameters );
}
Catch (exception ex)
{
MessageBox. Show (ex. tostring ());
}
// Object replaceall = word. wdreplace. wdreplaceall;
// Word. Document document = word. activedocument;
// Word. Range RNG = Document. content;
// RNG. Find. clearformatting ();
// RNG. Find. Text = oldname;
// RNG. Find. Replacement. clearformatting ();
// RNG. Find. Replacement. Text = newname;
// RNG. Find. Replacement. Font. Name = "";
// RNG. Find. Execute (
// Ref omissing, ref omissing,
// Ref omissing, ref omissing,
// Ref replaceall, ref omissing );
// Object missing = type. missing;
// Object replaceall = Microsoft. Office. InterOP. Word. wdreplace. wdreplaceall;
// Microsoft. Office. InterOP. Word. Find findobject = myword. activedocument. content. Find;
// Findobject. clearformatting ();
// Findobject. Text = stroldtext;
// Microsoft. Office. InterOP. Word. Replacement replacementobject = findobject. replacement;
// Replacementobject. clearformatting ();
// Findobject. Replacement. Text = strnewtext;
// Findobject. Replacement. Font. Name = "";
// Findobject. Execute (ref missing,
// Ref missing, ref missing,
// Ref missing, ref replaceall,
// Ref missing, ref missing );
}
The above four methods are similar for beginners, but the three methods that have been commented out may cause errors on some computers:
**************
System. runtime. interopservices. sehexception: An exception occurs for the external component.
In Microsoft. Office. InterOP. Word. Find. set_text (string prop)
In wordtest. form2.replacename (string oldname, string newname)
***************************************
I have collected a lot of information from the Internet, saying that writing parameters is a method that has not been commented out above, but I have not reported "external component exception,
However, it is difficult to set the font to "". // word. selection. find. replacement. font. name = ""; this statement is not in coordination with other statements above, and runs normally on my computer, but directly breaks down on some computers without even error prompts, ask the expert for advice.