在OLE Word自动化中搜索和替换 - 如何覆盖页眉和页脚?

10

我有一个完美运作的函数,可以在Word文档中查找和替换变量为文本。

HRESULT CMSWord::FindReplace( CString szVar, CString szText, bool bOnlyOnce/*=false*/ )
{
    if(m_pWApp==NULL || m_pActiveDocument==NULL) return E_FAIL;
    IDispatch *pDocApp;
    {  
        VARIANT result;
        VariantInit(&result);
        OLEMethod(DISPATCH_PROPERTYGET, &result, m_pActiveDocument, L"Application", 0);
        pDocApp= result.pdispVal;
    }
    IDispatch *pSelection;
    {
        VARIANT result;
        VariantInit(&result);
        OLEMethod(DISPATCH_PROPERTYGET, &result, pDocApp, L"Selection", 0);
        pSelection=result.pdispVal;
    }
    IDispatch *pFind;
    {
        VARIANT result;
        VariantInit(&result);
        OLEMethod(DISPATCH_PROPERTYGET, &result, pSelection, L"Find", 0);
        pFind=result.pdispVal;
    }
    OLEMethod(DISPATCH_METHOD, NULL, pFind, L"ClearFormatting",0);

    szText.Replace(_T("\r\n"), _T("\v")); 
    COleVariant sVariable(szVar);
    COleVariant sReplaceText(szText);
    COleVariant replace((long)2);
    COleVariant varBoolTrue;
    varBoolTrue.boolVal = true;
    COleVariant varBoolFalse;
    varBoolFalse.boolVal = false;
    COleVariant wdContinue((long)1);
    bool bFound=false;
    IDispatch *pExecute = NULL;
    {
        for(;;) {
            VARIANT result;
            VariantInit(&result);

            if(OLEMethod(DISPATCH_METHOD, &result, pFind, L"Execute", 8, wdContinue, varBoolTrue, varBoolFalse, varBoolFalse, varBoolFalse, varBoolTrue, varBoolFalse, sVariable)==S_OK) {
                pExecute=result.pdispVal;
                if(!pExecute) break;
                bFound = true;
                if(szText.IsEmpty()) DeleteChar(false);         else SetSelectionText(szText);
            }
            else break;
            if(bOnlyOnce) break;
        }
    }
    pDocApp->Release();
    pSelection->Release();
    pFind->Release();
    if(!bFound) return E_FAIL;
    else return S_OK;
}

问题是,这段代码不会涉及到页眉或页脚中的任何文本。
也许pFind execute方法有一个参数?
老实说,我从星期一开始就一直在研究这个问题。我的大部分搜索结果都是VB、C#、.NET和VBA文档,但VC++ OLE的文档很少,只有几行代码,没有什么帮助。我甚至开始创建和翻译一些Word宏,但什么都不起作用。
在Stack Overflow上,我发现了许多与此主题相关的问题。其中一些看起来很有前途,但似乎它们使用了我不知道的一些框架,如果我要求示例代码或链接,人们没有回应。
如果有人能够帮我解决这个问题,那将是太棒了,我真的很感激提供关于OLE Word自动化的文档和代码的链接(除了这篇codeproject文章)。
提前致谢!

1
我认为你需要在所有的故事范围内进行搜索和替换,如此链接所描述的那样:http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm - Simon Mourier
OLEMethod(DISPATCH_METHOD, &result, pFindReplace, L"Execute", 15, v_false, // 匹配控制 v_false, // 匹配阿勒夫哈姆扎 v_false, // 匹配教学法 v_false, // 匹配卡希达 v_replace, // 替换 v_replace_text, // 替换为 v_false, // 格式 v_wrap, // 查找换行 v_true, // 正向查找 v_false, // 匹配所有单词形式 v_false, // 匹配发音相似的单词 v_true, // 使用通配符匹配 v_true, // 匹配整个单词 v_true, // 区分大小写 v_find); // 查找文本直接查找/替换。很遗憾,这些参数都没有起到作用。 :-( - masche
所以,我猜这意味着没有办法仅通过c++添加/更改页眉/页脚,我是对的吗? Word宏不幸地不是一个选项,因为我们正在生成rtf文档,而在生成期间或之后插入新宏对我来说是不可能的。 - masche
你好!你还对你的问题感兴趣吗? - manuell
当然,如果你有任何提示的话。但我相信,这个问题不能用非托管的VC++来解决。 - masche
显示剩余5条评论
1个回答

1
这是一个在头部搜索和替换的Delphi函数。虽然这是一个C++问题,但您可以从这些函数中看到您需要做什么。
我总是发现,在工作中做某件事最简单的方法是使用宏录制器并查看Word如何操作,然后从您的应用程序调用该代码。
我为你在C++中进行COM编程感到遗憾。
Procedure Find_ReplaceText(find, ReplaceWith: String; Header : Boolean = false);
var
    tmpText: String;
    spos , epos : Integer;
begin
    {Start on first page.}
    fWordApp.Selection.Goto(wdGoToPage,wdGotoFirst);
    {Extra code is needed if I'm trying to replace text in the header}
    if Header then
    begin
        fWordApp.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;



        If fWordApp.Selection.HeaderFooter.IsHeader = False Then
        begin
            fWordApp.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
        end;

        tmpText := fWordApp.ActiveDocument.sections.item(1).Headers.item(wdHeaderFooterPrimary).Range.text;
        spos := pos('[' ,tmptext);
        epos := pos(']' , tmpText);

        tmptext := copy(tmptext,1, spos);
        tmptext := tmptext + ReplaceWith + ']';
        fWordApp.ActiveDocument.sections.item(1).Headers.item(wdHeaderFooterPrimary).Range.text := tmptext;
        fWordApp.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;
    end
    else
    begin
        fWordApp.Selection.Find.Text := find;
        fWordApp.Selection.Find.Execute;
        fWordApp.Selection.typeText(' ');
        fWordApp.Selection.InsertAfter(ReplaceWith);
    end;

end;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接