PDA

View Full Version : Word Macro to select the text at the start of a DOCX located prior to known string



steve8445
03-03-2016, 03:59 PM
I am trying to obtain the (text) TITLE of a DOCX that is always located on the first 1,2, or 3 of the non-empty lines at the start of the DOCX.
I'd like this title to be copied into both the System Clipboard and a Word VBA Variable - and (if possible?) to be accessible in a BASH Shell script (Mac OSX).

The TITLE is always located just prior to a known string e.g. "Joe Smith". It could be on the same line as Joe Smith, or on 1 or more of the lines prior. The title may span multiple lines. There is no other text prior to the Title. It's the first phrase in the DOCX, except for an unpredictable number of blank lines. Thanks.

gmaxey
03-03-2016, 04:22 PM
What have you tried?

This is the second fish that I've given you today? This forum is not a code writing service. Make an effort and most everyone here will be happy to assist you through the tough spots (if they know how).

This may get you started:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim strNeedleInHaystack
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = InputBox("What is the known text", "Enter Text", "Joe Smith")
If .Execute Then
oRng.Collapse wdCollapseStart
oRng.Start = ActiveDocument.Range.Start
strNeedleInHaystack = Trim(oRng)
End If
End With
lbl_Exit:
Exit Sub
End Sub


Google writing text to the clipboard. As for BASH Shell script, I haven't a clue.