【问题标题】:Search and replace in OLE Word automation - how to cover header and footer?OLE Word 自动化中的搜索和替换 - 如何覆盖页眉和页脚?
【发布时间】:2012-12-08 03:04:07
【问题描述】:

我有一个完美的工作功能来查找和替换 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执行方法有参数?

老实说,我从星期一开始就一直在研究这个问题。我的搜索结果最多的是VB、C#、.NET和VBA文档,但是关于VC++ OLE的文档很少,几行代码,但没什么帮助。我什至开始创建和翻译一些 Word 宏,但没有任何效果。

在 Stack Overflow 上,我发现了许多与此主题相关的问题。其中一些看起来很有希望,但似乎他们正在使用一些我不知道的框架,如果我要求提供示例代码或链接,人们没有任何回应。

如果有人能帮我解决这个问题,那就太好了,我非常感谢有关 OLE Word 自动化一般主题的文档和代码的链接(除了这篇 codeproject 文章)。

提前致谢!

【问题讨论】:

  • 我认为您需要在所有故事范围中搜索和替换,如下所述:word.mvps.org/faqs/customization/ReplaceAnywhere.htm
  • 在此期间,我尝试适应许多 VB 和 VBA 宏代码。互联网上有一些功能有效,但我自己的创作崩溃了。我需要一些文档如何使这些功能适应 C++。
  • OLEMethod(DISPATCH_METHOD, &result, pFindReplace, L"Execute", 15, v_false, //match control v_false, //match Alef Hamza v_false, //match didactics v_false, //match kashida v_replace, //replace v_replace_text, //replace with v_false, //format v_wrap, //find wrap v_true, //forward v_false, //match all word forms v_false, //match sounds-like v_true, //match wildcards v_true, //match whole word v_true, //match case v_find); //find text 直接查找/替换。没有任何参数有帮助。 :-(
  • 是否可以将标题和文件提取为字符串,对字符串进行替换,然后将字符串放回去?应该有一种简单的方法来获取和设置页眉和页脚吗?顺便说一句,您为此使用 C++ 的任何具体原因?
  • 如果我知道如何阅读或设置每个 OLE 的整个页脚或页眉,我会很高兴。这只是一个巨大的 mfc 项目的一小部分。

标签: visual-c++ com mfc ole office-automation


【解决方案1】:

这里是一个Delphi函数,在标题中进行搜索和替换。我知道这是一个 C++ 问题,但你可以从函数中看到你需要做什么。

我总是发现在工作中做某事的最简单方法是使用 MacroRecorder 并查看 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;

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多