Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert/Format static text into multiple Word docs in multiple folders

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US
Note: For those that have helped me previously, this does not pertain exactly to other projects that I have been working on (same documents, different need).

I have a lot of Word documents that I would like to enter a heading and paragraph into. The heading and paragraph are the same for each document and will be placed within the document direct before a different specific paragraph heading which is currently present in each document. However, this specific paragraph heading that would be used as the selection (insertion point) is not always the 2nd, 3rd or 4th paragraph in each document, it varies.

What I need is code to open each document, find the insertion point of where I want the new text (which would be just before a different paragraph heading), insert the new heading and paragraph, format the new heading and paragraph (bold and/or font size), and resave the documents in the same location.

I am new to VBA and I am reluctant to post the code that I have thus far (as I know it is incorrect - I found examples online and cannot figure out how to make it work), therefore I will start with some of the specifics:

Here are the folders that contain all of the documents:
Code:
Dim FolIn2 As String
Dim FolIn6 As String
Dim FolIn7 As String
Dim FolIn8 As String
Dim FolIn9 As String
Dim FolIn10 As String
Dim FolIn11 As String
Dim FolIn12 As String
Dim FolIn15 As String
Dim FolIn16 As String
Dim FolIn17 As String
Dim FolIn18 As String
Dim FolIn19 As String
Dim FolIn20 As String
Dim FolIn21 As String
Dim FolIn22 As String
Dim FolIn23 As String
Dim FolIn24 As String

FolIn2 = "I:\Ryan Plew\Copy of Cost Pages Originals\2Proposal - 401(a) 403(b) Cost pages"
FolIn6 = "I:\Ryan Plew\Copy of Cost Pages Originals\1Proposal - 401(k) Cost pages\401(k) FS LS Cost Pages"
FolIn7 = "I:\Ryan Plew\Copy of Cost Pages Originals\1Proposal - 401(k) Cost pages\401(k) TPA OneAlliance Cost Pages"
FolIn8 = "I:\Ryan Plew\Copy of Cost Pages Originals\1Proposal - 401(k) Cost pages\401(k) Unallocated Cost Pages"
FolIn9 = "I:\Ryan Plew\Copy of Cost Pages Originals\3Proposal - 401(a) 457(b) Cost pages\401(a) 457(b) FS LS Cost Pages"
FolIn10 = "I:\Ryan Plew\Copy of Cost Pages Originals\3Proposal - 401(a) 457(b) Cost pages\401(a) 457(b) PFRK Cost Pages"
FolIn11 = "I:\Ryan Plew\Copy of Cost Pages Originals\4Proposal - 403(b) Cost pages\403(b) FS LS Cost Pages"
FolIn12 = "I:\Ryan Plew\Copy of Cost Pages Originals\4Proposal - 403(b) Cost pages\403(b) TPA OneAlliance Cost Pages"
FolIn15 = "I:\Ryan Plew\Copy of Cost Pages Originals\5Proposal - 457(b) Cost pages\457(b) Voluntary Cost Pages"
FolIn16 = "I:\Ryan Plew\Copy of Cost Pages Originals\5Proposal - 457(b) Cost pages\457(b) FS LS Cost Pages\457(b) NR FS LS Cost Pages"
FolIn17 = "I:\Ryan Plew\Copy of Cost Pages Originals\5Proposal - 457(b) Cost pages\457(b) FS LS Cost Pages\457(b) REG FS LS Cost Pages"
FolIn18 = "I:\Ryan Plew\Copy of Cost Pages Originals\5Proposal - 457(b) Cost pages\457(b) PFRK Cost Pages\457(b) NR PFRK Cost Pages"
FolIn19 = "I:\Ryan Plew\Copy of Cost Pages Originals\5Proposal - 457(b) Cost pages\457(b) PFRK Cost Pages\457(b) REG PFRK Cost Pages"
FolIn20 = "I:\Ryan Plew\Copy of Cost Pages Originals\6Proposal Templates - 401(k)"
FolIn21 = "I:\Ryan Plew\Copy of Cost Pages Originals\7Proposal Templates - 401(a) 403(b)"
FolIn22 = "I:\Ryan Plew\Copy of Cost Pages Originals\8Proposal Templates - 401(a) 457(b)"
FolIn23 = "I:\Ryan Plew\Copy of Cost Pages Originals\9Proposal Templates - 403(b)"
FolIn24 = "I:\Ryan Plew\Copy of Cost Pages Originals\10Proposal Templates - 457(b)"

Within each document, I need to find the words: "Withdrawal Charge:" and insert text just before.

Text to be inserted: "Stable Value Account:" with font Arial Narrow, size 11, and bold. On the next line the text would be: "All Contributions and transfers to the Stable Value Account (SVA) will earn interest at the rate in effect at the time such contribution or transfer is made. All monies in the SVA will earn interest at that rate until that rate is changed. We may declare a new rate for the SVA that becomes effective on January 1 of each calendar year; however, we may declare an increase in the rate at any time." with font Arial Narrow, size 10 (no bold). Following this paragraph would need to be an empty line (double return).

That's it. I've found some code examples online, yet with my lack of knowledge, I am unable to excute them correctly. If you would like me to post what I have so far (the incorrect code), I will do so.

Thanks in advance for your help. I really appreciate it!!




 
Oh...so much....

Let's start with first things first (although it is almost the last thing you mentioned).
Following this paragraph would need to be an empty line (double return).
No! No! No!


If you do NOT do this, 80% of the other stuff becomes 100% easier.

The very fact you mention this demonstrates that you are NOT using Styles (plus the fact you mention formatting like "font Arial Narrow, size 10 (no bold)").

A properly used Word document would never, ever, ever, use an "empty line (double return)".

I have thousands of documents with hundreds of thousands of pages and not one, zip, nada, uses a single "empty line (double return)".

Why? Because the spacing - the purpose of those "empty line (double return)" - is defined within the Style. A style, say, named "MainText" is defined has having a space following of 10 pts (say). Thus, there is no need for a "empty line (double return)", because that space is already there.

Following that...
insert the new heading and paragraph, format the new heading and paragraph (bold and/or font size),
Again, no, no, no. If you used Styles fully, there would be NO need to mention formatting whatsoever.

This is how Word is designed, this is how, in fact, Word works.

If you were fully using Styles, you simply open each file, find "Withdrawal Charge:", and insert the text with the appropriate Style.

However, since you are not....probably the best way would be to:

1. make a chunk of text (your "Stable Value Account:" with font Arial Narrow, size 11, and bold. On the next line the text would be: "All Contributions and transfers....etc.)

2. select that (formatted as you want), and make it an AutoText. I did, and named it "MyInsert"

3. use the AutoText in each file.

I just tested 30 files, all with different locations of "Withdrawal Charge:". The following inserted the appropriate text, appropriately formatted, at the appropriate location.
Code:
Sub Insert_VersionA()
Dim file
Dim path As String
path = "c:\zzz\Bookmarks\"
file = Dir(path & "Insert*.doc")
Do While file <> ""
   Documents.Open FileName:=path & file
   With Selection
      With .Find
         .ClearFormatting
         .Text = "Withdrawal Charge:"
         .Replacement.Text = ""
         .Forward = True
         .Execute
      End With
      .HomeKey Unit:=wdLine
   End With
   NormalTemplate.AutoTextEntries("MyInsert").Insert _
      Where:=Selection.Range, _
      RichText:=True
   With ActiveDocument
      .Save
      .Close
   End With
   file = Dir()
Loop
End Sub
All documents in the folder have the new text.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top