I am writing a script that finds a term from a list and inserts a link to its definition using the Range.Find object. As I don't want to e.g. insert a link to 'cat' in 'catalogue', I use the MatchWholeWord property.
Now, this works well if the term is only a single word. However, if the term contains spaces or phrases, MatchWholeWord is simply ignored.
How can I make e.g. 'e-mail' not match 'free-mail'?
In case it matters, I use MS Word 2010.
Try using FindText:="<(e-mail)>" with MatchWildcards:=True.
With ActiveDocument.Content.Find
.Execute FindText:="<(e-mail)>", MatchWildcards:=True, MatchCase:=False, _
ReplaceWith:="blah", Replace:=wdReplaceAll
End With
Greetings
Axel
Related
I’m trying the define a range that starts and ends with a string e.g. range.start is “text string a” and range.end is “text string z” and then applying formatting to strings within this range only.
I can solve this problem with Select, but I can’t restrict to an instance as the length or number of times the phrase appears changes. It’s also very slow.
I use With Selection.Find with the usual Properties/Methods, but can’t target to a specific part of the document. Only .Execute Replace:=wdReplaceAll or .Execute Replace:=wdReplaceOne
I’d like to be able to target a range and then I can freely execute formatting changes.
With Range
Code
End With
I'm trying to insert a formatted table that I have saved in word named "DV Table" as part of the building blocks using VBA. I need this table to be inserted at the 13th paragraph of the word document.
Here's my code below. The first 3 lines just sets the selection to be at the 12th paragraph and create a new paragraph (13) after that. The last line of code is to insert the table. But when I run this, it gives the error.
Compile Error: Sub or Function not defined
I guess that this is not the proper way of defining the location. Would like some help on this. Thanks.
ActiveDocument.Paragraphs(12).Range.Select
Selection.EndKey Unit:=wdLine
Selection.Paragraphs.Add
ActiveDocument.AttachedTemplate.BuildingBlockEntries("DV Table" _
).Insert Where:=Paragraphs(13).Range.Select, RichText:=True
The Where parameter requires a Range object. There are two problems with Paragraphs(13).Range.Select
it's a method - it's an action, selecting something, not returning an object
Paragraphs(13) isn't "fully qualified" - VBA doesn't know what it is/what is meant.
One possibility would be
ActiveDocument.Paragraphs(13).Range
Notice ActiveDocument. preceding Paragraphs: this "fully qualifies" Paragraphs(13) - it tells VBA to what that belongs. And, since Where requires a Range object, Paragraphs(13).Range should be a correct "target" (I have not tested your code).
Generally, it's preferable not to work with Selection, just with Range objects. There's usually no need to actually select something using VBA. An alternative to the code snippet in the question could be
Dim rng As Word.Range
Set rng = ActiveDocument.Paragraphs(13).Range
rng.Collapse wdCollapseEnd 'like pressing right-arrow for a selection
rng.InsertParagraphAfter
rng.Collapse wdCollapseStart ' like pressing left-arrow for a selection
'rng.Select ' for testing / demo purposes
ActiveDocument.AttachedTemplate.BuildingBlockEntries("DV Table" _
).Insert Where:=rng, RichText:=True
In this case, the selection in the document does not change. There's no screen flicker; and code executes more quickly. This way of working takes getting used to, but once one is familiar with it, it's much easier to recognize what the code should be doing... Selection is rather vague as to what is being manipulated, especially if there's a lot of code using it.
Disclaimer: I am not a tech guy, and my chosen profession is law. I know just enough to help my company create forms that can be filled out by using macros in Word.
I have gotten as far as completing the code and have it all working properly. However, I have realized that not all TextBoxes will be filled out every single time. I want the unused TextBox to appear blank in the Word doc instead of having the place holders. Example of the code and issue is below.
If TextBox1.Value <> "" Then _
ActiveDocument.Range.Find.Execute _
FindText:="<T Agent>", ReplaceWith:=UCase(TextBox1.Value), Replace:=wdReplaceAll
Currently, if I do not fill out textbox 1, it will leave the place holder as <T Agent> on the Word doc. I want it to be just blank.
What do I need to add to make this work?
Does that make sense?
Try altering the ReplaceWith statement to execute an if statement that checks the state of the TextBox before it is written. Something like:
ReplaceWith:=IIf(TextBox1.Value="<T Agent>", "", TextBox1.Value)
The double I in IIF is intentional.
Remove the If TextBox1.Value <> ""
ActiveDocument.Range.Find.Execute FindText:="<T Agent>", ReplaceWith:=UCase(TextBox1.Value), Replace:=wdReplaceAll
Do the replace all the time.
I don't think you even need to use an IF to find out whether the TextBox contains anything. If it's "empty" it returns a zero-length string, which is exactly what you want to write to your document:
ActiveDocument.Range.Find.Execute _
FindText:="<T Agent>", _
ReplaceWith:=UCase(TextBox1.Text), Replace:=wdReplaceAll
(I put in the _ in order to make the long line of code more readable - you can delete them and bring everything together on a single line, if you prefer.)
Note that my preference for coding this would be to assign the textbox's content to a variable and work with that:
Dim sTextBox1Value as String
sTextBox1Value = TextBox1.Text
ActiveDocument.Range.Find.Execute _
FindText:="<T Agent>", ReplaceWith:=UCase(sTextBox1Value), _
Replace:=wdReplaceAll
The objective is to build a list of text elements in a Word 2010 document, identified by having a specific style applied. This list is then sent somewhere else to have exciting things done to it.
Context: this style is applied by hand as authors build up their documents. As well as formatting the text appropriately, it identifies text that must later be pulled out into another summary document. This is currently an Excel worksheet but, dependent on requirements, could also be another Word document.
I note with interest this question and its answer: Find paragraphs by style in word 2010 using interop
I (think I) need to do this in VBA rather than C#, however, as I would like to add it to my Normal.dotm default template, for easy distribution to my team.
I think the question I am really asking is "what is the most efficient way to walk through a document and check the style applied to each word, performing an action on that word if required?"
The target documents can be quite large - >400 pages - and so efficiency may well be a thing.
Things I can't do, for business reasons:
Change to another product or format for the documents involved
Change to a newer version of Word
Use a language or tool that doesn't come as default with Word
You could use the .Find method and specify the style with .Style.
Example:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Style = "Heading 1" 'Replace this with name of your style
While .Execute
ActiveDocument.Range.InsertAfter vbCr & oRng
ActiveDocument.Paragraphs.Last.Range.Style = "Normal"
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Source: https://www.msofficeforums.com/word-vba/23001-creating-list-all-text-specific-style.html
I use word to open (special) html documents for printing and want to write a script to fix a small issue:
In the html file I have numbers like '15 cm' written as '15 cm', so there will be no line break between the number '15' and the unit 'cm'.
My problem is: The spacing is too wide, especially when word expands the spaces to fit a sentence to the margins.
So I'd like to replace these with some kind of thin spaces using a word VBA script. I guess I need to enumerate the paragraphs, but I'm not sure how to replace text in there. This is what I came up with so far, but I don't know how to write the html nbsp in word and what to use for thin spaces, maybe someone can help me here?
Sub MakeThinSpaces()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
replace the with some here?
Next
End Sub
Thin space is Unicode 8201. (A narrow no-break space would be 8239).
It should work with this code using the selection object.
^s is the wildcard for a protected space, you could also use Chr(160)
Selection.WholeStory
With Selection.Find
.Text = "^s"
.Replacement.Text = ChrW(8201)
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Regarding replacement in the entire text, there is an alternative solution to selecting the whole story.
E.g. following code would replace all non-breaking spaces in the document with normal spaces:
With ActiveDocument.Content.Find
.Execute "^s", replacewith:=" ", Replace:=wdReplaceAll
End With
The wildcard-match parameter, should it really be required, could be packet into the .Execute statement in the same way.