I am trying to find all of the cells with a certain text of "0.118" in column 2 of my table and do a list of commands for that row
I am also trying to take the value from column 5 of that selected text found in that row and subtract the value I put in the input box for that row.
The problem I am having is that it only changes one of my found "0.118" and not all of them in each row.
And I can't figure out how to search for the column(5) of that selected row.
Any help would be greatly appreciated.
Thank you.
Sub ConvertTo_3MM()
Dim oTable As Table
Dim stT As Long, enT As Long
Dim stS As Long, enS As Long
With Selection.Find
.Forward = True
.MatchPhrase = True
.Execute FindText:="0.118"
End With
For Each oTable In ActiveDocument.Tables
Do While Selection.Find.Execute = True
stT = oTable.Range.Start
enT = oTable.Range.End
stS = Selection.Range.Start
enS = Selection.Range.End
If stS < stT Or enS > enT Then Exit Do
Selection.Collapse wdCollapseStart
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Cell(nRow, 2).Range
.Text = "3 MM" & vbCrLf & "-" & vbCrLf & "6 MM"
End With
End If
Selection.MoveRight Unit:=wdCell
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Cell(nRow, 3).Range
.InsertAfter Text:=vbCrLf & "-" & vbCrLf & "SHANK"
End With
End If
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
response = InputBox("Cut Length For 3 MM")
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Cell(nRow, 5).Range
.Text = response & vbCrLf & "-" & vbCrLf & (column(5).value - response)
End With
End If
Selection.Find.Execute Replace:=wdReplaceAll
Loop
Selection.Collapse wdCollapseEnd
Next
Application.ScreenUpdating = True
End Sub
I would be very surprised if the code in your question actually does anything as it doesn't even compile.
Your code is rather a confused mess so I'm not entirely certain that I have correctly understood what you are attempting to do, but try this:
Sub ConvertTo_3MM()
Application.ScreenUpdating = False
Dim oTable As Table
Dim response As String
For Each oTable In ActiveDocument.Tables
With oTable.Range
With .Find
.Forward = True
.MatchPhrase = True
.Text = "0.118"
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found = True
.Text = "3 MM" & vbCr & "-" & vbCr & "6 MM"
With .Rows(1)
.Cells(3).Range.InsertAfter Text:=vbCr & "-" & vbCr & "SHANK"
response = Val(InputBox("Cut Length For 3 MM"))
With .Cells(5).Range
.Text = response & vbCr & "-" & vbCr & (Val(.Text) - response)
End With
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Next
Application.ScreenUpdating = True
End Sub
Thi may not be a solution, but I do see some problems:
You do:
For Each oTable In ActiveDocument.Tables
Then you do inside that loop:
Do While Selection.Find.Execute = True
but this Find will not be limited to the table of the For Each loop.
Though harmless, inside this Do While loop you do:
If ActiveDocument.Tables.Count >= 1 Then
but of course this is true because the For Each already determined there is at least 1 table.
I suggest you lookup the documentation of Find, rethink the logic and then run it step by step in the debugger to see what the code is doing.
Try this code:
Sub ConvertTo_3MM()
Dim oTable As Table, rng As Range
Dim nRow As Long, response As String
For Each oTable In ActiveDocument.Tables
With oTable
Set rng = .Range
Do
If rng.Find.Execute("0.118") Then
If rng.Information(wdEndOfRangeColumnNumber) = 2 Then
nRow = rng.Information(wdEndOfRangeRowNumber)
.Cell(nRow, 2).Range.Text = "3 MM" & vbCrLf & "-" & vbCrLf & "6 MM"
.Cell(nRow, 3).Range.InsertAfter Text:=vbCrLf & "-" & vbCrLf & "SHANK"
response = Val(InputBox("Cut Length For 3 MM"))
.Cell(nRow, 5).Range.Text = response & _
vbCrLf & "-" & vbCrLf & (Val(.Cell(nRow, 5).Range.Text) - response)
End If
Else
Exit Do
End If
rng.Collapse wdCollapseEnd
Loop
End With
Next
Application.ScreenUpdating = True
End Sub
Before
After
Related
I'm trying to search specific words in the selected/highlighted text. The result should show how much the word is used throughout the highlighted selected area.
I wrote a macro, but the total value of words shown is calculated through the entire document, not the selected part.
Sub CountWords()
'macros for counting specific words in the document
'to count the number of a specified word, this word needs to be highlighted
Dim rng As Range
Dim sWord As String
Dim i As Long
Dim sWord As String
Set rng = Selection.Range
Application.ScreenUpdating = False
sWord = InputBox( _
Prompt:="What word do you want to count?", _
Title:="Count Words", Default:="")
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = sWord
.Forward = True
.MatchWholeWord = True
.MatchWildcards = False
.Wrap = wdFindStop
Do While .Execute
i = i + 1
Loop
End With
Select Case i
Case 2 To 4
MsgBox "word " & Chr(171) & sWord & Chr(187) & " occurred in the document " & i & " times", _
vbInformation, "word count"
Case 1
MsgBox "word " & Chr(171) & sWord & Chr(187) & " occurred in the document " & i & " times", _
vbInformation, "word count"
Case Else
MsgBox "word " & Chr(171) & sWord & Chr(187) & " occurred in the document " & i & " times", _
vbInformation, "word count"
End Select
rng.Find.Text = ""
Application.ScreenUpdating = True
End Sub
I've tried a bunch of stuff, even other peoples codes. Every one of them counts specific words throughout the entire document.
For example:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, sWord As String, i As Long
sWord = InputBox(Prompt:="What word do you want to count?", Title:="Count Words", Default:="")
With Selection
Set Rng = .Range
.Collapse wdCollapseStart
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = sWord
.Forward = True
.MatchWholeWord = True
.MatchWildcards = False
.Wrap = wdFindStop
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Do
i = i + 1
Loop
End With
End With
Rng.Select
MsgBox "The word " & Chr(171) & sWord & Chr(187) & " occurred " & i & " times in the selected range.", _
vbInformation, "word count"
Application.ScreenUpdating = True
End Sub
In the following code I am trying to insert a picuture into my word document based on the text I found while searchiung. The problem is it will only return to me the firsat character od the text. How do I get all of the text? How do I get the actual line it was found in? The text I am looking for is directly after the text found. IE: "Insert screen shot here of Boxshot" So I am trying to load a file called Boxshot. NOT working. Help.
Sub NewPic()
'
' NewPic Macro
'
Dim screenshot, Dirname, selfound As String
Dim Dn As Long
'
With Selection.Find
.Text = "Insert screen shot here of "
'.Replacement.Text = ""
.Forward = True
End With
Selection.Find.Execute
'
'Insert picture and find next match
'
While Selection.Find.Found
Selection.TypeParagraph
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Select
selfound = Selection.Characters.First
MsgBox ("Text=" & selfound)
'
'Is picture there?
'
Dirname = ActiveDocument.Name
Dn = InStr(Dirname, "User")
Dirname = Left(Dirname, Dn)
screenshot = "C:\Users\User 1\Desktop\VB Upload files\CD's\" & Dirname & "\" &
Selection.Text & ".jpg"
MsgBox ("Screenshot= " & screenshot & ", Sellectedtext=" & Selection.Text)
'
If Dir(screenshot) <> "" Then
Else
screenshot = "C:\Users\User 1\Desktop\Mylogo.jpg"
End If
'
Selection.InlineShapes.AddPicture FileName:= _
screenshot, LinkToFile:=False, SaveWithDocument _
:=True
'"C:\Users\User 1\Desktop\Mylogo.jpg", LinkToFile:=False, SaveWithDocument _
':=True
Selection.TypeParagraph
Selection.TypeParagraph
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.TypeParagraph
Selection.Find.Execute
Wend
'
End Sub
Your use of Selection makes your code unnecessarily complex and slow. The following macro will insert the relevant pictures wherever "Insert screen shot here of " is followed by the pic name (no error-checking for valid names & files). If you don't want to retain the pic names below the pics, simply un-comment the commented-out line.
Sub NewPics()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Insert screen shot here of "
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindContinue
End With
Do While .Find.Execute
.Text = vbCr
.Collapse wdCollapseEnd
.End = .Paragraphs.Last.Range.End - 1
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.InlineShapes.AddPicture FileName:=ActiveDocument.Path & "\" & .Text & ".jpg", LinkToFile:=False, SaveWithDocument:=True
'.Start = .Start + 1: .Delete
Loop
End With
Application.ScreenUpdating = True
End Sub
I using a VBA code to batch find and replace highlighted text. The macro finds and replaces the words in the document. It works well with a few number of highlighted text on a small document (1-2 pages). However, when I use this macro on a large documents which has over a 100 pages, Microsoft word crashed and becomes unresponsive so I have to forced to quit.
The code is to help make it easy to redact information. I am replacing the highlight text which occur also in tables with XXXXX and highlighted black.
Does anyone have any tips to make the code more efficient?
Here is the code
Sub FindandReplaceHighlight()
Dim strFindColor As String
Dim strReplaceColor As String
Dim strText As String
Dim objDoc As Document
Dim objRange As Range
Application.ScreenUpdating = False
Set objDoc = ActiveDocument
strFindColor = InputBox("Specify a color (enter the value):", "Specify Highlight Color")
strReplaceColor = InputBox("Specify a new color (enter the value):", "New Highlight Color")
strText = InputBox("Specify a new text (enter the value):", "New Text")
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.Highlight = True
Do While .Execute
If Selection.Range.HighlightColorIndex = strFindColor Then
Set objRange = Selection.Range
objRange.HighlightColorIndex = strReplaceColor
objRange.Text = strText
objRange.Font.ColorIndex = wdBlack
Selection.Collapse wdCollapseEnd
End If
Loop
End With
End With
Application.ScreenUpdating = True
End Sub
Try:
Sub FindandReplaceHighlight()
Application.ScreenUpdating = False
Dim ClrFnd As Long, ClrRep As Long, strTxt As String
Const StrColors As String = vbCr & _
" 1 Black" & vbCr & _
" 2 Blue" & vbCr & _
" 3 Turquoise" & vbCr & _
" 4 Bright Green" & vbCr & _
" 5 Pink" & vbCr & _
" 6 Red" & vbCr & _
" 7 Yellow" & vbCr & _
" 8 White" & vbCr & _
" 9 Dark Blue" & vbCr & _
"10 Teal" & vbCr & _
"11 Green" & vbCr & _
"12 Violet" & vbCr & _
"13 Dark Red" & vbCr & _
"14 Dark Yellow" & vbCr & _
"15 Gray 50" & vbCr & _
"16 Gray 25%"
ClrFnd = InputBox("Specify the old color (enter the value):" & StrColors, "Specify Highlight Color")
ClrRep = InputBox("Specify the new color (enter the value):" & StrColors, "New Highlight Color")
strTxt = InputBox("Specify the new text (enter the value):", "New Text")
With ActiveDocument
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Highlight = True
.Forward = True
.Wrap = wdFindStop
End With
Do While .Find.Execute
If .HighlightColorIndex = ClrFnd Then
.HighlightColorIndex = ClrRep
.Text = strTxt
.Font.ColorIndex = wdBlack
.Collapse wdCollapseEnd
End If
Loop
End With
End With
Application.ScreenUpdating = True
End Sub
I'm trying to make the "number of occurrences" either be written in red or in bolded red. Can someone please point me in the right direction. I'm new to coding. This is a word-counter, and when 2+ words are found...it displays the number of words found at the bottom of the word document.
Sub a3()
Dim Word As String
Dim wcount As Integer
Word = InputBox("Search for a word")
If (Word <= "") Then
MsgBox ("Did not enter word")
End If
If (Word > "") Then
wcount = 0
With Selection
.HomeKey Unit:=wdStory
With ActiveDocument.Content.Find
.Text = Word
Do While .Execute
wcount = wcount + 1
Selection.MoveRight
Loop
End With
MsgBox ("The word: '" & Word & "' shows up " & wcount & " times in the document")
End With
End If
If (wcount <= 2) Then
ActiveDocument.Content.InsertAfter Text:=(vbCrLf & "Number occurrences: " & wcount)
Selection.Font.ColorIndex = wdRed
ElseIf (wcount <= 3) Then
ActiveDocument.Content.InsertAfter Text:=(vbCrLf & "Number occurrences: " & wcount)
Selection.Font.ColorIndex = wdRed
Selection.Font.Bold = True
Else
ActiveDocument.Content.InsertAfter Text:=(vbCrLf & "Number occurrences: " & wcount)
Selection.Font.ColorIndex = wdBlack
Selection.Font.Bold = False
End If
End Sub
Working with Word Range objects will help with this. Think of a Range like an invisible selection, except that code can work with multiple Range objects, while there can be only one Selection.
Assign the document's content to a Range, then perform the Find and extension on that. Then the formatting can also be applied to the Range. I've altered (but not tested) the code in the question to demonstrate.
In the last part, where text is written at the end of the document, the Range object is set to the entire document, then collapsed (think of it like pressing the right-arrow key with a selection). Then the new text is assigned to the range and formatting applied. Because the range will contain only the new text, the formatting is applied to that, only.
(Additional notes: I've changed the Word variable name to sWord because "Word" could be misunderstood to mean the Word application. I've also changed the comparison to check whether sWord contains something to Len(sWord) > 0 because the "greater than """ comparison is not guaranteed.)
Sub a3()
Dim sWord As String
Dim wcount As Integer
Dim rng as Word.Range
Set rng = ActiveDocument.Content
sWord = InputBox("Search for a word")
If (sWord <= "") Then
MsgBox ("Did not enter word")
End If
If (Len(sWord) > 0) Then
wcount = 0
With Selection
.HomeKey Unit:=wdStory
With rng.Find
.Text = sWord
Do While .Execute
wcount = wcount + 1
rng.Collapse wdCollapseEnd
Loop
End With
MsgBox ("The word: '" & sWord & "' shows up " & wcount & " times in the document")
End With
End If
Set rng = ActiveDocument.Content
rng.Collapse wdCollapseEnd
If (wcount <= 2) Then
rng.Text = (vbCrLf & "Number occurrences: " & wcount)
rng.Font.ColorIndex = wdRed
ElseIf (wcount <= 3) Then
rng.Text = (vbCrLf & "Number occurrences: " & wcount)
rng.Font.ColorIndex = wdRed
rng.Font.Bold = True
Else
rng.Text = (vbCrLf & "Number occurrences: " & wcount)
rng.Font.ColorIndex = wdBlack
rng.Font.Bold = False
End If
End Sub
There are many ways to do this, some of them are based on a preference for ranges or selections and also the structure of the Find statement. Here is my preference.
Sub a3()
Dim wrd As String
Dim wcount As Integer
Dim rng As Word.Range
wrd = InputBox("Search for a word")
If wrd = vbNullString Then
MsgBox ("Did not enter word")
Exit Sub
End If
Set rng = ActiveDocument.Content
wcount = 0
With rng.Find
.ClearFormatting
.Format = False
.Forward = True
.MatchWholeWord = True
.Text = wrd
.Wrap = wdFindStop
.Execute
Do While .found
wcount = wcount + 1
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
.Execute
Loop
End With
MsgBox ("The word: " & "" & wrd & "" & " shows up " & wcount & " times in the document")
ActiveDocument.Content.InsertParagraphAfter
Set rng = ActiveDocument.Content
rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
rng.Text = "Number occurrences: " & wcount
If wcount < 3 Then
rng.Font.ColorIndex = wdRed
ElseIf wcount < 4 Then
rng.Font.ColorIndex = wdRed
rng.Font.Bold = True
Else
rng.Font.ColorIndex = wdAuto
rng.Font.Bold = False
End If
End Sub
Ok, so basically we have a couple of unique sections in a macro-enabled template for Word 2007 and for each section, we have 2 entries that are standard for the form, and then there are about 20 optional entries that are all handled by AutoText. The formatting is identical between the template and the AutoText entries and I'm wanting to auto-number the entries as they are added (either by the user typing the AutoText keyphrase or hitting a button on the ribbon to insert it).
Is there an easy way to do this?
Here is the block of code where one of these (numbered) entries is handled and what I've tried to implement as a numbering scheme from other suggestions on other forums (couldn't find anything useful here):
Case "cboFF"
SetMargins 0, 1, 1
Selection.ParagraphFormat.Space1
Selection.Text = "FINDINGS OF FACT" & vbLf
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = True
Selection.Font.Underline = wdUnderlineSingle
Selection.Collapse (wdCollapseEnd)
Selection.Text = vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
Selection.Font.Underline = wdUnderlineNone
Selection.Font.Bold = False
SetMargins 0, 1, 1
'With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
'.NumberFormat = "%1."
'.TrailingCharacter = wdTrailingTab
'.NumberStyle = wdListNumberStyleNone
'.NumberPosition = InchesToPoints(0.5)
'.Alignment = wdListLevelAlignLeft
'.TextPosition = InchesToPoints(0.5)
'.ResetOnHigher = 0
'.StartAt = 1
'AutoNumberOnFOF
Selection.Text = "On " & strDateFOF & ", an industrial appeals judge certified that the parties agreed to include the Jurisdictional History in the Board record solely for jurisdictional purposes." & vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
Select Case strCaseCategory
Case "IND", "IND SELF-I"
If frmIIOD.optII.Value = True Then
Selection.Text = "II-FF"
Selection.Range.InsertAutoText
Selection.Collapse (wdCollapseEnd)
Selection.Text = vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
GoToEnd
End If
If frmIIOD.optOD.Value = True Then
Selection.Text = "OD-FF"
Selection.Range.InsertAutoText
Selection.Collapse (wdCollapseEnd)
Selection.Text = vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
GoToEnd
End If
If frmIIOD.optNotNeeded.Value = True Then
Selection.Text = vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
End If
Case Else
'Do Nothing
End Select
Any constructive comments will be much appreciated to help solve this issue. I'm still very new to programming as a whole and most of my experience lies in C# and Java.
Edit: The structure of the document is essentially a set of itemized lists containing legal text that is updated by a user as the appeal process goes through various stages. In each of the last 2 sections the itemized lists need to follow a specific numbering scheme (num at .5", text at 1", right tab at 1") which is not native to Word 2007. There is a bolded heading for each of the sections that is the starting point of the numbering. The AutoText entries will be added as needed by the user. The rest of the document pulls information from our database and contains the legal wording necessary for the document.
If I could just figure out how to initiate the numbering for each section individually, then I could finish this up.
I've actually figured out my own solution to the issue. The problem was trying to insert an AutoText entry on the (2nd) line of numbering since it reads the whole line and thinks it is an AutoText entry. Rather than only reading ii-ff or od-ff, it was reading 1. ii-ff, which isn't a valid AutoText entry (by name).
Case "cboFF"
SetMargins 0, 1, 1
Selection.ParagraphFormat.Space1
Selection.Text = "FINDINGS OF FACT" & vbLf
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Bold = True
Selection.Font.Underline = wdUnderlineSingle
Selection.Collapse (wdCollapseEnd)
Selection.Text = vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
Selection.Font.Underline = wdUnderlineNone
Selection.Font.Bold = False
SetMargins -0.5, 1, 1
'AutoNumberOn
Selection.Text = "1." & vbTab & "On " & strDateFOF & strEntry1 & vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
Select Case strCaseCategory
Case "IND", "IND SELF-I"
If frmIIOD.optII.Value = True Then
Selection.Text = "2." & vbTab & strIIEntry & vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
Selection.Text = "3." & vbTab & "" & vbLf
Selection.Collapse (wdCollapseEnd)
End If
If frmIIOD.optOD.Value = True Then
Selection.Text = "2." & vbTab & strODEntry & vbLf
Selection.Text = "3." & vbTab & "" & vbLf
Selection.Collapse (wdCollapseEnd)
End If
If frmIIOD.optNotNeeded.Value = True Then
Selection.Text = "2." & vbTab & "" & vbLf
Selection.ParagraphFormat.SpaceAfter = 6
Selection.Collapse (wdCollapseEnd)
End If
Case Else
'Do Nothing
End Select
This allows the to insert AutoText entries on the empty lines and continue the pre-formatted numbering system which isn't native to Word 2007. Now I just need to figure out the easiest way to higlight specific words within the inserted selection. Shouldn't be too bad:
'set array of text entries (6)
'begin loop
'find and highlight entry(i)
'end loop