PDA

View Full Version : Search & Find Strings - Copy Strings into a Placeholder Area



saphire99
12-19-2015, 05:46 AM
Hello,

to all forum experts and members.

I am looking for some help from the experts.

I am trying to copy strings that begin with a symbol to the top of my document into a placeholder range.

===================================

Lets say that some where in my document I have paragraphs that begin with >>

>> Science Studies

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the

>> Maths Topics - Algebra, Trigonometry
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the

>> Computers Science - Programming, VBA, Excel
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

======================================

I would like to copy the above sentences to the top of my document into an placeholder area.

I will style the placeholder area. So it may have a character style - I thought this would be a good way to insert the strings.

So I will end up with


Placeholder Area
Science Studies
Maths Topics - Algebra, Trigonometry
Computers Science - Programming, VBA, Excel



I tried running a macro - but I just get copy selection and paste selection.:crying:

I tried to create a macro


Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long

StrFnd = " "

.Text = "(>>)[1][A-za-z]{1,}"

For i = 0 To UBound(split(StrFnd, ","))
Set Rng = ActiveDocument.Range
With Rng.Find
.ClearFormatting
.Text = split(StrFnd, ",")(i)


to say I am overwhelmed -

How can I go about doing this?

Thank you so much in advance

Saphire

gmayor
12-19-2015, 08:07 AM
Put a bookmark where you want the placeholder text in the document then run the following macro to call and fill the bookmark name with the requested items


Option Explicit

Sub GetHeadings()
Dim orng As Range
Dim strText As String
strText = ""
Set orng = ActiveDocument.Range
With orng.Find
Do While .Execute(FindText:=">> ")
orng.MoveEndUntil Chr(13)
strText = strText & orng.Text & vbCr
orng.Collapse 0
Loop
End With
strText = Left(strText, Len(strText) - 1)
FillBM "Bookmarkname", strText
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

Private Sub FillBM(strBMName As String, strValue As String)
'Graham Mayor
Dim orng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set orng = .Bookmarks(strBMName).Range
orng.Text = strValue
orng.Bookmarks.Add strBMName
End With
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub

saphire99
12-19-2015, 09:23 AM
Hello Graham,

thank you so muchhhhh....:biggrin:

We are nearly there ...it duplicated the paragraphs and put it in the placeholder area.

I never thought of using bookmarks before, thats a good idea.

>> Science Studies

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the

>> Maths Topics - Algebra, Trigonometry
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the

>> Computers Science - Programming, VBA, Excel
Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Am I meant to modify the code so it only copies the strings
thank you for helping me

saphire

gmaxey
12-19-2015, 05:06 PM
It appears that Graham assumed that you created a new paragraph at the end of the line containing the string. if you used a linebreak then change Graham's MoveEndUntil to Chr(11).

saphire99
12-20-2015, 03:34 AM
Hello Greg,
thank you for that input:yay

And thank you Graham for the delightful macro.
Yippety! It worked like a beautiful charm -it will make my life so much easier now that I know about the bookmarks feature.


I am very happy. :biggrin:

I have one regex problem that I am working on getting nowhere, I will post another thread hope the kind gentlemen experts won't mind helping solve that one.

This problem is solved!

Thank you so much

Saphire