hydra1018 发表于 2011-1-25 19:08:20

求助C#和Autoit高手,帮助改进word.au3

在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等命令实在不懂怎么翻译
如有高人帮忙,感激不尽!

tpj633 发表于 2011-1-25 21:48:06

C# 的foreach跟au3的For...In...Next 用法一样

papapa314 发表于 2011-1-25 23:40:36

楼上正解。。。

hydra1018 发表于 2011-1-26 19:25:20

StoryRanges sr = wordDoc.StoryRanges;

Range r1 = r;

不是一个foreach的问题

king8462 发表于 2011-1-26 22:49:30

以前也遇到同样的问题!
后来想了一个折中的办法:
1.录制一段修改页眉的宏。
2.使用_WordMacroRun运行该宏。

hydra1018 发表于 2011-1-27 21:00:33

这个是可以,但这样autoit做出来的程序就不具有通用性了

hydra1018 发表于 2011-1-29 13:19:52

来论坛问的三个问题都没人能帮忙解决……

smartzbs 发表于 2011-1-29 15:39:08

本帖最后由 smartzbs 于 2011-1-29 18:35 编辑

写个替换页眉的例子给你吧,其他自己琢磨.
#include <Word.au3>

$oWordApp = _WordCreate(@ScriptDir & "\Test.doc")
$oWordDoc = _WordDocGetCollection($oWordApp, 0)
$oWordDoc.ActiveWindow.ActivePane.View.SeekView   =   1;   //激活页眉的编辑   
$sHeader = $oWordApp.Selection.HeaderFooter.Range.Text; //读出页眉文本
$oWordApp.Selection.HeaderFooter.Range.Text = StringReplace($sHeader , "源文本", "新文本"); //替换页眉为新文本
$oWordDoc.ActiveWindow.ActivePane.View.SeekView   =   0;   //关闭页眉的编辑

你怪人家不回你,先要想想你的问题表述是否清楚?如果别人问你IniRead是否支持Utf8,从中文语法上,是不是只要回答"是"或"否"就算完整了?怎么能算没回呢?
页: [1]
查看完整版本: 求助C#和Autoit高手,帮助改进word.au3