Avatar of forumware
forumware

asked on 

Find and replace text across multiple word files (contains form checkbox)

Hi I have almost a thousand files that contain a block of signatures that I would like to remove.

Before each signature, there is a "YES" and then a form field check box and then the signature line

Please see the word document attached,

 I have given visual of what I am trying to achieve.
members.doc
Microsoft WordVisual Basic.NET

Avatar of undefined
Last Comment
forumware
Avatar of MINDSUPERB
MINDSUPERB
Flag of Kuwait image

Forumware:

In deleting those signature lines quickly, you can do this trick.

Just hold "Ctrl" of the keyboard, then select those lines that you would like to delete. Once the last 2  lines to be deleted are already selected, release  the "Ctrl" key and you can press the Delete Key to delete those lines altogether.

In that way you can delete those lines easily.

I may suggest to try it with the first page, and see if that is what you want.

Just try it out.

Ed

Avatar of forumware
forumware

ASKER

Hey thanks for the tip. However I have almost a thousand files. I do not want to open each file and CRTL select to delete.

I was wondering if there is any vba for this.
Avatar of Howard Cantrell
Why not just write a new file with just the single member code in it?
Avatar of forumware
forumware

ASKER

Each word file has different information on it, some for network scans, some with desktop scans, virus scans, etc.

But now the team consists of 1 person. Management would like the signature blocks removed for the other two people.

And I am left with editing over a thousand of the files. So the manual process would be for me to open each file, remove the signature blocks, then save. and repeat for each file.

I thought a vb code would be better and quicker...
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Are all the files a Word doc?
Avatar of forumware
forumware

ASKER

Thanks for the code, Yes all the files are word docs. I will try it out and get back
Avatar of forumware
forumware

ASKER

Sorry am I doing something wrong.

I opened a new file,
hit ALT+f11
Insert - New Module
Pasted code
Hit Run
and a Macros box pops up... with only "Create" as an option.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

The actual macro takes a parameter so it won't appear in that list. The parameter is the folder name, but I don't know what that is in your case.   You either need to type the usage code that I gave you into the VBA Immediate window (and hit run) or you can turn it into a parameterless calling macro and run that via the Word Tools/Macro menu. You will have to put the correct folder name in instead of "C:\MyFolder" in either case.
Sub CallDelete
   DelecteEndBlocks "C:\MyFolder"
End Sub

Open in new window

Avatar of forumware
forumware

ASKER

Hey thanks again for the help.

The code works on a single document but does not cycle through the rest and I get a runtime error 424
object required.

 The code is what you gave me but I created a new document and hit alt f11 and inserted the code below:

I tried it with 5 sample documents, if I open All 5, code executes (and deletes the lines) but then the runtime error.

If i don't have any sample documents open and try it, the code opens the first document and removes the lines but does not save it or close it and continue to the next document.
Sub CallDelete()
   DeleteEndBlocks "C:\Test"
End Sub
 
Sub DeleteEndBlocks(strFolder As String)
    Dim strFile As String
    Dim Doc As Document
    Dim rng As Range
    
    strFile = Dir$(strFolder & "\*.doc")
    Do Until strFile = ""
        Set Doc = Documents.Open(strFolder & "\" & strFile)
        Set rng = Doc.Range
        If rng.Find.Execute("Member 1 ^t^t^t^tDate^t^p") Then
            rng.Collapse wdCollapseEnd
            rng.End = Doc.Range.End
            rng.Text = ""
        Else
            MsgBox "Expected text not found in " & strFile
        End If
        strDoc.Close wdSaveChanges
        strFile = Dir$()
        DoEvents
    Loop
End Sub

Open in new window

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

I didn't have a set of documents to test so I only tried with one and the added the looping. The first thing that I notice is I have misnamed the document variable as strDoc in line 21 (of your snippet). Change that line to

Doc.Close wdSaveChanges


Avatar of forumware
forumware

ASKER

Wow works great Graham.

One more question...I noticed you said the line must match my sample. What if the text is different can I replace

If rng.Find.Execute("Member 1 ^t^t^t^tDate^t^p") Then

with

If rng.Find.Execute("Auditing Members  ^t^t^tDate^t^p") Then

I noticed that    ^t stands for tab space and ^p for paragraph.. is that correct?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes that is right.
Avatar of forumware
forumware

ASKER

Thanks, however when I substitute that line with my search criteria i.e:

rng.Find.Execute("IV&V Coordinating Committee Member ^t^tDate^t^p")

I get expected text not found... I have double triple checked the line..

I am sorry to be dragging this however I started manually editing the files and the search criteria is on many of them.  Specially

IV&V Coordinating Committee Member and then Date.

I uploaded another sample file... Thanks again


test.doc
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this: "IV&V Coordinating Committee Member^t^tDate^t^p"
(There is no space between "Member" and the first tab.)
Avatar of forumware
forumware

ASKER

You my friend are a life saver. Thank you for saving me countless hours and sticking thru my questions.
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo