I am trying to make my macro bring up a search box that allows me to enter as many words as I want, separated by comma, and then find each word in the list in the document and make them bold and blue. What I have right now actually DOES exactly what I want it to do. It finds each word that I enter into the searchbox (separated by commas), finds the word, and changes it to bold blue font.
The problem is this: I need to be able to enter a page or more of words. InputBox only gives me 255 characters to use at a time. Is there a modification to make to my code to be able to have no limit to the number of words I can paste in to change?
It doesn't help that I am new at this. It has taken me a day and a half to get where I am right now.
Sub BlueWords()
Dim blueword As String
Dim numberofwords As Long
Application.ScreenUpdating = False
' Enter words that need to become bold blue words.
blueword = InputBox("Enter items to be found here,seperated by comma: ", "Items to be found")
numberofwords = UBound(Split(blueword, ","))
' Find each item and replace it with new one respectively.
For numberofwords = 0 To numberofwords
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.Text = Split(blueword, ",")(numberofwords)
.Replacement.Text = ""
.Replacement.Font.Bold = True
.Replacement.Font.ColorIndex = wdBlue
.Format = True
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
Next numberofwords
Application.ScreenUpdating = True
End Sub
It works. I just need to be able to input more than 255 characters of words at a time. I need "no limit".
Add a user form instead, then add a button and a text box (there is no limit on text boxes in forms) then set the button to run the macro taking the input from the forms text box
Sub BlueWords()
Dim blueword As String
Dim numberofwords As Long
Application.ScreenUpdating = False
' Enter words that need to become bold blue words.
UserForm1.Show
'blueword = The super long input string in the userform
blueword = UserForm1.TextBox1.Text
numberofwords = UBound(Split(blueword, ","))
' Find each item and replace it with new one respectively.
For numberofwords = 0 To numberofwords
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.Text = Split(blueword, ",")(numberofwords)
.Replacement.Text = ""
.Replacement.Font.Bold = True
.Replacement.Font.ColorIndex = wdBlue
.Format = True
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
Next numberofwords
Application.ScreenUpdating = True
Unload UserForm1
End Sub
Related
Just wanted to say people who can code are seriously next level,
I am unfortunately not one of those people.
I'v been set a task at work, which I assume there must be a quicker way to complete.
After research I came across this, but it only works for 15 words at a time, but I have around 500 words to find and replace, which I had been doing manually.
Any adaptations, if you dont mind, please dumb it down for me, all I know is how to paste to Visual Basic and run. I can input comma separated values/paths.
Sub FindAndReplaceMultiItems()
Dim strFindText As String
Dim strReplaceText As String
Dim nSplitItem As Long
Application.ScreenUpdating = False
' Enter items to be replaces and new ones.
strFindText = InputBox("Enter items to be found here,seperated by comma: ", "Items to be found")
strReplaceText = InputBox("Enter new items here, seperated by comma: ", "New items")
nSplitItem = UBound(Split(strFindText, ","))
' Find each item and replace it with new one respectively.
For nSplitItem = 0 To nSplitItem
With Selection
.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = Split(strFindText, ",")(nSplitItem)
.Replacement.Text = Split(strReplaceText, ",")(nSplitItem)
.Format = False
.MatchWholeWord = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End With
Next nSplitItem
Application.ScreenUpdating = True
End Sub
Any help would be very appreciated : )
Bonus Question: How to apply all these find and replacements to 900 documents?
Example data: (Controlled documents so I cant share real data unfortunately)
A dfe-tbh,scf-b9jt,dvf-hnk,pol-jbv,dn-fgm,gh-jkl
B df12-hbt,dvf-dgf,hj-gngk,dbs-ghdn,fbh-ghg,gfn-dhn
Desired outcome is when the user presses their shortcut, the cursor jumps to the next instance of multiple search terms. For now let's say "[Ff]izz" and "[Bb]uzz". I don't want to make any changes, just jump forward through the doc to the next instance.
Current code is as follows, but what it's doing is jumping to the next instance of "fizz" if Wrap is set to wdFindStop, or "buzz" if Wrap is set to wdFindContinue or wdFindAsk. If a line was just "Fizz buzz buzz fizz", I'd like it to be able to select each individual word in order from left to right.
Sub FizzBuzzFinder()
Dim StrFind As String
Dim i As Long
StrFind = "[Ff]izz,[Bb]uzz"
Selection.Find.ClearFormatting
With Selection.Find
.MatchWildcards = True
.Wrap = wdFindAsk
.Forward = True
For i = 0 To UBound(Split(StrFind, ","))
.Text = Split(StrFind, ",")(i)
.Execute
Next i
End With
End Sub
Any idea what I'm doing wrong? Cheers in advance
The issue was your For Loop. It searches for Fizz, selects it, then searches for Buzz, advancing though the text. Since this happens together, the user only sees it select Buzz.
To correct this, I removed the For loop and changed the search string from two items to one item that matches either Fizz or Buzz.
Sub FizzBuzzFinder()
Dim StrFind As String
Dim i As Long
StrFind = "[FfBb][ui]zz"
Selection.Find.ClearFormatting
With Selection.Find
.MatchWildcards = True
.Wrap = wdFindAsk
.Forward = True
.Text = StrFind
.Execute
End With
End Sub
Unfortunately, this also matches Fuzz and Bizz.
I have a simple script that goes through and sets the style for all paragraphs beginning with a certain character. Easy, I thought. It changes all the paragraphs so they have the same properties as the "Details" Style. But for some reason only the last paragraph ends up with "Details" as its style and all the ones before go back to "Normal". Here's my code so far:
Sub Format_doc()
Dim txt As String
For Each par In ActiveDocument.Paragraphs
txt = par.Range.Text
If Left(txt, 1) = "/" Then
par.Style = "Details"
par.Range.Text = Right(txt, Len(txt) - 1)
End If
Next
End Sub
I'd like to keep them attached to the style because I toggle the "hidden" font property in another macro. I'll need to toggle this hidden property for these paragraphs on-and-off several times and assigning a single paragraph style seemed like an easy solution. Here's the other code:
Sub Toggle_hidden()
ActiveDocument.Styles("Details").Font.Hidden = Not ActiveDocument.Styles("Details").Font.Hidden
End Sub
Solutions? I'm working on Mac, but ultimately this will end up on a Windows.
Your code works fine, here. But perhaps that's due to the version of MacWord... I tested with Office 2016 (Office 365 subscription).
If it's not working for you it may have something to do with the way you're removing the / by basically replacing the paragraph's content. This will also affect the paragraph mark, which is responsible for the paragraph formatting, including the style. Try the following, which explicitly removes the first character and leaves everything else intact:
Sub Format_doc()
Dim txt As String
Dim par As Word.Paragraph
For Each par In ActiveDocument.Paragraphs
txt = par.Range.Text
If Left(txt, 1) = "/" Then
par.Style = "Details"
'par.Range.Text = Right(txt, Len(txt) - 1)
par.Range.Characters(1).Delete
End If
Next
End Sub
Here's a different approach that should also work - and be somewhat faster.
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p/"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
.Execute
End With
Do While .Find.Found
With .Duplicate
.Start = .Start + 1
.End = .Paragraphs(1).Style = "Details"
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
I have a number of lengthy Word 2010 documents riddled with acronyms and many of the same acronyms are common to all these docs. I also have a vertical list of these acronyms that are in hyperlink format within each document in a table format (at then end of each document).
e.g. Hyperlink list:
AA1
AA2
AA3
...
I have very little Word 2010 macro experience but wonder if it's possible to have a macro that would work like this:
Within any given doc, when I place my cursor on the top of the list (i.e. at the cell with hyperlink contents "AA1"), it would select the contents of this first cell, then copy it to clipboard, then do a global replace (using ^c I suppose) of all text formatted words "AA1" within the document with the hyperlink formatted word "AA1" (assuming it found the word to replace).
It would then move down to the second cell with a hyperlink content of "AA2" and replace all "AA2" text within the document with the hyperlinked "AA2" value etc. until all the hyperlinked words in the hyperlinked list have replaced all the text words within the document.
Is this do-able?
if i understand your mean correct [Question was not clear and visual] this is the base code that you need:
Sub replaceHyperLink()
Dim searchText As String
searchText = "AA1"
Selection.Find.ClearFormatting
With Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do
With Selection.Find
.text = searchText
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"stackoverflow.com/", SubAddress:="", ScreenTip:="", TextToDisplay:=searchText
Else
Exit Do
End If
Loop
End Sub
you must do it in loop for AA2,AA3,....
if you want to hyperlink just one column of table, select that
column and then copy it in new document (code for do that is simple)
then do operation and again paste it in selected column.
and if you want to get text from cells:
Dim cellText As String
For i = 1 To ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables(1).Rows(i).Cells(3).Select
cellText = Selection.text
'do what you want with text
Next i
I have 2 headers or markers that are a part of my RTF document. In my example I am showing a sentence when in reality it will be multiple sentences or paragraphs. I have used brackets instead of less than and greater than signs as they disappear in my question. All I want to do is replace the text between the 2 markers with the following sentence, "text goes here", without quotation marks.
[EmbeddedReport]Lots of text, thousands of character, multiple paragraphs[/EmbeddedReport]
I want replace all the text between the 2 markers replaced with "text goes here".
It would end up looking like this...
"[EmbeddedReport]text goes here[/EmbeddedReport]"
I've literally spent 2 days trying to solve this. Any help would be appreciated.
This is the last thing I tried...
Sub RemoveReport()
Dim c As Range
Dim StartWord As String, EndWord As String
Selection.HomeKey Unit:=wdStory
StartWord = "<ImageTable>"
EndWord = "</ImageTable>"
Set c = ActiveDocument.Content
c.Find.ClearFormatting
c.Find.Replacement.ClearFormatting
With c.Find
.Text = StartWord & "*" & EndWord
' MsgBox (.Text)
.Replacement.Text = "<ImageTable>text goes here</ImageTable>"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
c.Find.Execute
While c.Find.Found
Debug.Print c.Text
'~~> I am assuming that the start word and the end word will only
'~~> be in the start and end respectively and not in the middle
Debug.Print Replace(Replace(c.Text, StartWord, ""), EndWord, "")
c.Find.Execute
Wend
End Sub
Word VBA is not my area of expertise, but it seems similar to a question I answered a few days ago.
Turns out the wildcard match was not doing what I hoped it would do, or at least it was not reliable. Also, I ran in to some trouble using angle brackets, so this uses square brackets. I suspect that word treats the angle brackets as markup/syntax, and thus does not interpret them as text in the Find object. There is probably a way around this, but Word VBA is not my specialty. There is also probably a more elegant solution, but again, Word VBA is not my specialty :)
Try something like this:
Option Explicit
Sub Test()
Dim doc As Document
Dim txtRange As Range
Dim startTag As String
Dim endTag As String
Dim s As Long
Dim e As Long
startTag = "[EmbeddedReport]"
endTag = "[/EmbeddedReport]"
Set doc = ActiveDocument
Set txtRange = doc.Content
'Find the opening tag
With txtRange.Find
.Text = startTag
.Forward = True
.Execute
If .Found Then
s = txtRange.Start
Else
GoTo EarlyExit
End If
End With
'Find the closing tag
Set txtRange = doc.Range(txtRange.End, doc.Content.End)
With txtRange.Find
.Text = endTag
.Forward = True
.Execute
If .Found Then
e = txtRange.End
Else
GoTo EarlyExit
End If
End With
Set txtRange = doc.Range(s, e)
txtRange.Text = startTag & "text goes here" & endTag
Exit Sub
EarlyExit:
MsgBox "Header not found in this document!", vbInformation
End Sub
It takes some time to figure it out at first, but learning to navigate the object model reference documentation for VBA will make these tasks a lot easier to figure out in the future.