Microsoft word VBA macro search only certain table - vba

Is there a way in VBA to search and delete cells that are only in a certain table, maybe based on the table title?
I have code written to delete cells that match the word I am looking for but this searches all tables in the document.
Dim myString As String
myString = "5"
Selection.Find.ClearFormatting
With Selection.Find
.Text = myString
.Wrap = wdFindContinue
.MatchWholeWord = True
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Selection.Rows.Delete
End If
What would be the best way to limit this to only 1 table?
Also this would delete any cell that contains the word I am looking for, is there a way to limit it to cells that only contain what I am searching for.
Thanks for the help.

Related

How to run a "replace order" in Word every other time

I have a sort of long document with all parentheses toward one side; like (out of stock(.
Now I'm wondering how I can write a replace order replacing every other parenthesis.
I already know that the replace order is like:
Set myRange = ActiveDocument.Content myRange.Find.Execute FindText:="(", _ ReplaceWith:=")", Replace:=wdReplaceAll
but unfortunately I don't know how to apply
If i Mod 2 = 0 Then
to the code above.

Color-Coding Microsoft Word Table Columns Depending on the String Data in the Cells, VBA

I need a way to save time in Microsoft Word. I am expected to adhere to a fast turnaround time every day, at work. I need VBA code to colour-code the background of certain table cells in Microsoft Word. I have 10 tables. In each table, I have hundreds of rows of data. Each row of data is a facility. Each facility is in one of 6 different geographical jurisdictions. Each geographical region has its own name (Northern, Southern, Eastern, Western, Central, Provincial). Each geographical region has its own color. I need to add that colour into the background of the columns of cells in the tables that reference the geographical region.
I currently copy and paste color formatting. It leads to mistakes. I am brand new with VBA. I tried to implement another solution someone had created for colour-coding based on numerical values but I was unsuccessful in my attempt to convert and adapt their code to regions.
I would feel super thankful if you could teach me. Thank you
For example, assuming your geographical region names (Northern, Southern, Eastern, Western, Central, Provincial) are in the 1st column of each table:
Sub Demo()
Application.ScreenUpdating = False
Dim Tbl As Table, Rw As Row
For Each Tbl In ActiveDocument.Tables
For Each Rw In Tbl.Rows
With Rw.Cells(1)
Select Case Trim(Split(.Range.Text, vbCr)(0))
Case "Northern": .Shading.BackgroundPatternColorIndex = wdBrightGreen
Case "Southern": .Shading.BackgroundPatternColorIndex = wdYellow
Case "Eastern": .Shading.BackgroundPatternColorIndex = wdTurquoise
Case "Western": .Shading.BackgroundPatternColorIndex = wdPink
Case "Central": .Shading.BackgroundPatternColorIndex = wdViolet
Case "Provincial": .Shading.BackgroundPatternColorIndex = wdDarkYellow
End Select
End With
Next
Next
Application.ScreenUpdating = True
End Sub

Word Copying, Pasting and Deleting Columns with VBA

I have a word document with tables throughout.
I have a macro that I use to copy values from cells in certain columns and paste them into other columns. Then the macro deletes several columns from the table.
Generally I use bookmarks for the copying and pasting.
The Bookmarks are all cells in a specific column, except the header.
So for example "Objectives" bookmark would span from row2 of column 2 of table5, until the end of column2. So if the user adds a row, that would also be included in the bookmark.
Here is an example below:
If ActiveDocument.Bookmarks.Exists("ProposedOverallObj") = True Then
ActiveDocument.Bookmarks.Item("ProposedOverallObj").Select
Selection.Cut
Selection.GoTo What:=wdGoToBookmark, Name:="Objectives"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.PasteAndFormat (wdPasteDefault)
End If
Dim Table1 As Table
Set Table1 = ThisDocument.Tables(5)
If Table1.Columns.Count >= 5 Then
With Table1
.Columns(5).Delete
.Columns(4).Delete
.Columns(3).Delete
End With
Else
MsgBox "Not enough columns!"
End If
and it goes on like this for about 13 tables. The first few tables work fine, then i start running into a weird error where randomly the line of text above my table gets copied into the table header and then my "delete columns" code breaks and tells me that one of the columns has already been deleted. (it hasn't).
Is there a better way to do this? Might this be happening because I am using .Select? I know it can be sketchy. Would it be better to assign IDs to my column ranges rather than using bookmarks and select? if so, could you provide me with a small example code for using Dim on a range in a table (my range will always start on row2 and go to the end of the column)?

Word 2010 VBA - Nested Do Until ... Loop for Search and Replaces

I'm attempting to create a nested Do Until... Loop that will perform 15 different search and replaces in Word documents. I need to accomplish the following (semicolon at end only for listing purposes):
Search space ^t; replace with ^t;
Search ^t space; replace with ^t;
search ^t^t; replace with ^t;
search ^t^p; replace with ^p;
search $^t; replace with $;
search $ space; replace with $;
search ^t%; replace with %;
search space %; replace with %;
search (^t; replace with (;
search ( space; replace with (;
search ^t); replace with );
search space ); replace with );
search space ^p; replace with ^p;
search ^p space; replace with ^p; and
search ^p^p; replace with ^p.
I've recorded the macro and would like to edit it, replacing the numerous
Selection.Find.Execute Replace:=wdReplaceAll
that occurred for each search/replace in the macro until the result was 0 instances. I know a Do Until... Loop will make this macro more efficient. Please help!
Something along these lines should at least get you started:
Dim searches() As String
Dim replaces() As String
searches = Split("( , %", ",")
replaces = Split("(,%", ",")
Dim i As Integer
For i = LBound(searches) To UBound(searches)
With Selection.Find
Do
.ClearFormatting
.Text = searches(i)
.Replacement.Text = replaces(i)
.Execute Replace:=wdReplaceAll
Loop While .Found
End With
Next i
The search strings and replacement strings are comma delimited and have to be the same number of elements - only two were included for clarity (it'll be ugly with the vbTab and vbCr concatenated in all over the place).

Word 2007 remove information from document onscreen

I am working with sensitive information in Word and would like to remove some of the information that breaches privacy legislation. This information is variable, so I cannot do a simple replace. I was able to remove the information from the file after saving it as a text file and looping through the file and saving it to a new text file. The lines where the information occurs always starts with the same information, so that is what I used to trigger the removal of the private info. Ideally, I would like to be able to do this without saving the file first. I am a little familiar with VBA in word, but this is beyond my skillset at the moment.
sorry for the ambiguity....the information that needs to be replaced is variable, similar to a Social Security Number, thus a simple search and replace is not possible. A sample is provided below. What I am looking for is when the beginning of the line contains ID#, the first 4 characters of the ID number be either x'd out or replaced with a blank. The last three characters would still be visible for identification purposes.
-INQFS-07A-26-------------------------------------DATE:111018-162442-
ID#: XXXXXXX NAME: XXXXXX, XXXX LOCATION : XXXXXXXXXXXXXXXXXXXXXXXXX NUMBER : XXX PAGE: 1
--------------C U R R E N T -------------- OPENING BALANCE --------------S A V I N G S -------------- C A N T E E N
CASH HOLDS FREE BALANCE CASH HOLDS FREE BALANCE
306.52 7.50 299.02 DATE 11/10/11 91.68 0.00 91.68 90.00
This seems to do what I want it to do.
Sub MaskIDNUM()
'
' MaskIDNUM Macro
' Blank out first four characters of the ID# in a printout
'
Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "ID#: ????"
.Replacement.Text = "ID# "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub