How to remove a selection and repeat that action.

  • Thread starter Ralf van der Eerden
  • Start date
R

Ralf van der Eerden

Hi everyone ...

First let me apologize for crossposting in case that is a nono ... I am new
to these newsgroups and not aware yet of any netiquette.

With that said, I hope people can help me with what sounds simple (to me)
but is not easy for someone who has no programming skills whatsoever (me
again).

What I want to accomplish is this....


I want to open a document, remove text starting from a word(s) till a
word(s) (for example: from "dear sir" till "kind regards"), save the
remaining text in a new file --> open the next document in the directory,
remove text starting from the same word I already defined in the first
session and save the remaining text in the already created new file (add
text to already present text).

So in short:

1. Select directory
2. Give selection 1 (word(s))
3. Give selection 2 (word(s))
4. Open first document in the directory
5. Delete the text between 1 --> 2
6. Save the remaining text in a new file
8. Close document 1
9. Open the second document in the directory
10. Delete the text between 1 --> 2
11. Save the remaining text in the new file created at step 6 (add text to
already present text)
12. Close document 2
13. Open the third document in the directory
14. Delete the text between 1 --> 2
15. Save the remaining text in the new file created at step 6 (add text to
already present text)
16. Close document 3
Etc etc ...

I hope someone can help me or point me in the right direction.

Kind regads

Ralf
 
J

Jezebel

Not clear what sort of help you're actually asking for. All the bits of your
application are easy enough; but stringing together the whole lot is a
fairly serious programming task. For a competent VBA/Word programmer it's
about 4 hours work to get it to run; about the same again if it has to be
used by strangers.
 
R

Ralf van der Eerden

The help I am asking for is how to put it all together ..

As I said .. I am no programmer and I can not in any way see how difficult
this is or not.
If it is as you said .. too much work to "just throw together" then I will
find a way to figure it out some how ...

But .. let me thank you for at least giving me a reasonable answer and view
on the issue so I can work from there ..

Ralf
 
H

Helmut Weber

Hi Ralf,
break your small project up into seperate pieces.
One question after another. Otherwise you may get an
answer, that would be too difficult to adapt.
Though it is working here and now:
Sub Test143()
Dim i As Integer
Dim dRes As Document ' Document for result
Dim dTmp As Document ' temporary Document
Dim sPth As String
Dim sTmp As String
Dim sWr1 As String
Dim sWr2 As String
Resetsearch
sWr1 = "[a-z]" ' start of text to search for eg. "dear sir"
sWr2 = "[a-z]" ' end of text to search for e.g "kind regards"
sTmp = "c:\result.doc" ' the doc fpr the results
sPth = "c:\test\new" ' the path to search for docs
Set dRes = Documents(sTmp)
With Application.FileSearch
.NewSearch
.LookIn = sPth
.SearchSubFolders = False
.FileName = "*.doc"
.Execute
For i = 1 To .FoundFiles.Count
Set dTmp = Documents.Open(.FoundFiles(i), Visible:=False)
With dTmp.Range.find
.text = sWr1 & "*" & sWr2
.Replacement.text = ""
.MatchWildcards = False
.MatchCase = True
.Execute Replace:=wdReplaceAll
End With
dRes.Range.FormattedText.InsertAfter dTmp.Range.FormattedText & vbCr
dTmp.Saved = True
dTmp.Close
Next
End With
Resetsearch
End Sub

Sub Resetsearch()
With Selection.find
.ClearFormatting
.Replacement.ClearFormatting
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
T

Tony Strazzeri

Ralf,

Have you can start by recording a macro while you do all of the
actions on a single document. You could even start with the opening
of the document.

The trick with the find is to use the F8 function key after finding
the first word. F8 is the extend funcgtion. in extends the selection
from the first word to the second word.

Then do the delete bit and the save bit. Then repeat the process in a
logical way.

After one iteration you will have a fair bit of recorded code that you
can look at. A lot of it will be unnecessary because the recorder
records every aspect of an action, but you can leave it in to start
with until you become more comfortable with the language. Use the VBA
help files to help you understand the commands that have been
recorded.

This will get you started. You can then come back here with more
specific questions.

Hope this helps.

Cheers
TonyS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top