在word.au3里面
_WordDocFindReplace
是不能替换页眉和文本框的
后来看到
http://www.cnblogs.com/eshizhan/archive/2010/11/03/1868155.html
尝试进行移植,以失败告终
有高人愿意帮忙移植吗?
word.au3的相关源码如下 Select
Case $v_SearchRange = -1
$v_SearchRange = $o_object.Application.Selection.Range
Case $v_SearchRange = 0
$v_SearchRange = $o_object.Range
Case $v_SearchRange > -1
__WordErrorNotify("Error", "_WordDocFindReplace", "$_WordStatus_InvalidValue")
Return SetError($_WordStatus_InvalidValue, 5, 0)
Case Else
If Not __WordIsObjType($v_SearchRange, "range") Then
__WordErrorNotify("Error", "_WordDocFindReplace", "$_WordStatus_InvalidObjectType")
Return SetError($_WordStatus_InvalidObjectType, 5, 0)
EndIf
EndSelect
Local $return
Local $o_Find = $v_SearchRange.Find
With $o_Find
.ClearFormatting ()
.Replacement.ClearFormatting ()
$return = .Execute($s_FindText, $f_MatchCase, $f_MatchWholeWord, $f_MatchWildcards, $f_MatchSoundsLike, _
$f_MatchAllWordForms, $f_Forward, $i_Wrap, $f_Format, $s_ReplaceWith, $i_Replace)
EndWith
而c#的源码如下:/// <summary>
/// 查找并替换文本
/// </summary>
/// <param name="wordApp"></param>
/// <param name="oldStr"></param>
/// <param name="newStr"></param>
public void SearchReplace(string oldStr, string newStr)
{
#region 文字区域
object replaceAll = WdReplace.wdReplaceAll;
wordApp.Selection.Find.ClearFormatting();
wordApp.Selection.Find.Text = oldStr;
wordApp.Selection.Find.Replacement.ClearFormatting();
wordApp.Selection.Find.Replacement.Text = newStr;
wordApp.Selection.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);
#endregion
#region 文本框
StoryRanges sr = wordDoc.StoryRanges;
foreach (Range r in sr)
{
Range r1 = r;
if (WdStoryType.wdTextFrameStory == r.StoryType)
{
do
{
r1.Find.ClearFormatting();
r1.Find.Text = oldStr;
r1.Find.Replacement.ClearFormatting();
r1.Find.Replacement.Text = newStr;
r1.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);
r1 = r1.NextStoryRange;
} while (r1 != null);
}
}
#endregion
}
关键是 foreach等命令实在不懂怎么翻译
如有高人帮忙,感激不尽! |