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. 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
How can I use a RegEx for that. 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)
the specimen is received in CONTAINER OF FORMALIN at this workstation
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
CONTAINER OF FORMALIN <----- here is where I want to paste from the first match
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.
I use delphi xe3 and I know how to use regex with other files but not in word using 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. 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;