简体   繁体   English

使用RegEx从Delphi中搜索MS Word文档中的单词并导入到Delphi应用程序

[英]Searching for words in MS Word Document from Delphi with RegEx and import to Delphi app

I am working with our lab report system and want to automate some of the tasks. 我正在使用我们的实验室报告系统,并希望自动执行某些任务。 The system we use is not intuitive and uses word documents to enter data. 我们使用的系统不直观,使用word文档输入数据。 There are several paragraphs with headings (protected headings). 标题有几个段落(受保护的标题)。

I want to copy a phrase in one of the paragraphs and paste it into another paragraph using a Delphi app 我想在其中一个段落中复制一个短语,并使用Delphi应用程序将其粘贴到另一个段落中

GetActiveOleObject('Word.Application');

How can I use a RegEx for that. 我怎样才能使用RegEx。 The good thing is the searchable phrases I want to copy are in uppercase while everything else is sentence case. 好消息是我要复制的可搜索短语是大写的,而其他一切都是句子。 example: 例:

3rd paragraph heading:---> Receiver Notes <---- this is not editable in the document (protected) 第3段标题:---> Receiver Notes <----这在文档中是不可编辑的(受保护的)

  1. the specimen is received in CONTAINER OF FORMALIN at this workstation 样品在此工作站的FORMALIN CONTAINER OF FORMALIN中收到

  2. the specimen is received FRESH WITH NO FIXATIVE at this workstation 在这个工作站上,没有固定地接收样品

my result has to be something like: 我的结果必须是这样的:

4th paragraph heading --->Methods of Receiving <------ protected again 第4段标题--->接收方法<------再次受到保护

  1. CONTAINER OF FORMALIN <----- here is where I want to paste from the first match FORMALIN的容器<-----这里是我要从第一场比赛中粘贴的地方

  2. FRESH WITH NO FIXATIVE <----- and here the second match … etc 没有固定的新鲜<-----而这里是第二场比赛......等等

So my feeling is to have a delphi code to search between paragraph heading "Receiver Note" and "Methods of Receiving" for those in upper case and list them in the next paragraph. 因此,我的感觉是使用delphi代码在大写字母的“接收者注释”和“接收方法”之间进行搜索,并在下一段中列出它们。

I use delphi xe3 and I know how to use regex with other files but not in word using delphi. 我使用delphi xe3,我知道如何使用正则表达式与其他文件,但不是使用delphi的单词。 Any input, code snippets, examples, etc would be much appreciated! 任何输入,代码片段,示例等将非常感谢!

Ok I finally got this to work and I am posting the code if incase someone needs this. 好吧,我终于让这个工作,如果有人需要这个,我发布代码。 I had to copy the document to my delphi Memo and work it there with regex and then paste it back where I want. 我不得不将文档复制到我的delphi备忘录并使用正则表达式在那里工作,然后将其粘贴回我想要的地方。 Although the process may seem cumbersome, it executes very fast. 虽然这个过程看起来很麻烦,但执行起来非常快。 The word documents I work with are usually one or two pages anyways. 我工作的单词文件通常是一两页。

procedure TForm1.Button1Click(Sender: TObject);
var
  DXRANGE, DXWORD: OleVariant;
  n : Integer;
  regexpr: TRegEx;
  Match: TMatch;
begin
  try
    DXWORD := GetActiveOleObject('Word.Application');

    DXRANGE := DXWORD.Documents.Item(1)
      .Range(DXWORD.Documents.Item(1).Range.Start, DXWORD.Documents.Item(1)
      .Range.End);
    DXRANGE.Copy;
    Memo1.Clear;
    Memo1.PasteFromClipBoard;
    regexpr := TRegEx.Create('\b[A-Z][A-Z][A-Z]+(?:\s+[A-Z]+)*\b');
    Match := regexpr.Match(Memo1.Text);
    n := 1;
    Memo2.Clear;
    while Match.Success do
    begin
      Memo2.Lines.Add(IntToStr(n) + Match.Value);
      Memo2.Lines.Add('');
      Match := Match.NextMatch;
      n := n + 1;
    end;
    Memo2.SelectAll;
    Memo2.CopyToClipboard;
    DXWORD.Selection.PasteSpecial(wdPasteRTF)
  except
    on E: exception do
    begin
      ShowMessage(E.Message);
    end;
  end;
end;

As a general rule when working with Word (or any office app) and ActiveX Delphi component, is to use the amazing macro recorder to see how it would do it. 作为使用Word(或任何办公应用程序)和ActiveX Delphi组件的一般规则,是使用神奇的宏录制器来查看它将如何做到这一点。

eg. 例如。

  • Open your word document 打开word文档
  • Select [Record Macro] from the tools menu 从工具菜单中选择[记录宏]
  • Do your search 做你的搜索
  • Copy it to the clipboard 将其复制到剪贴板
  • Replace your code 替换你的代码
  • Do Whatever else you need to do. 做你需要做的其他事情。
  • Stop Macro 停止宏

Now open up the macro VBA organiser and look at the code VBA has generated for what you did. 现在打开宏VBA组织器,查看VBA为您所做的事情生成的代码。 This will give you a very good idea of the functions you need to get your delphi code to call. 这将使您非常了解获取delphi代码所需的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM