I am trying to delete empty lines occurring in a word document between two placeholders.
I am not sure how to find the position of the placeholders within the entire document.
I am not sure how to search between the placeholders and replace multiple carriage returns with a single carriage return.
I tried to convert the code here but couldn't get it working.
If your placeholders are always the same, you can accomplish the whole thing from Word's Find and Replace dialog. No code necessary.
For example, to replace multiple carriage returns:
Ctrl+H
turn "Use Wildcards" on
Find: (Placeholder1)^13{2,}(Placeholder2) \include the parentheses
Replace: \1 ^13 \2
If you're trying to incorporate this into a bigger block of code:
With Selection.Find
.Text = "(Placeholder1)^13{2,}(Placeholder2)"
.Replacement.Text="\1" & " ^13 " & "\2"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute
End With
Let me know if that works for you.
Related
I've been tasked with writing VBA code to automate Word. I have no idea about VBA.
What is needed:
There is a standard Word document based on a template, which has a section for writing issues. Each section has a heading, and below the heading there is a table with a couple of rows.
Sections view
The overall document layout with the different sections.
Issue view
A standard issue, with a Heading for the issue title, then a table to be filled, and then a description.
How do I write a macro that will:
Review ALL the issues in the document to see if the issue has a list of "Affected Hosts" in the table below the heading that contains more than 10 hosts
If this is not the case, ignore and move on to the next one.
If this is the case, that list should be replaced with some generic text such as "See Appendix G", and then add to that Appendix G the issue title and below it the list of all those hosts.
Where I am at:
I looked for examples of code snippets, looked at the documentation, etc. This is all I have:
Sub TidyAffectedSystems()
'
' TidyAffectedSystems Macro
'
'
' Loop over all issues by finding the appropriate style (IssueHeading)
With ActiveDocument.Range
With .Find
.ClearFormatting
.Forward = True
.Format = True
.Style = "Issue Heading" ' Heading style to find
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
MsgBox .Text
' If it is the last one, then finish looping over
If .End = ActiveDocument.Range.End Then Exit Do
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub
This code tries to find the headings based on the style ("Issue Heading"), and then print that heading. I'm doing this to make sure that at least I am finding the right sections, but that's it.
I don't know how to for example select the table below the current found item to then see if that table has more than 10 hosts and all that, and of course no idea how to then replace, take it to an Appendix and repeat.
I am trying to highlight duplicate content based on user input, in dark red (color doesn't matter).
I ran the code in the Word document, written in Hebrew, and it appeared to fail. I ran it with English text and it worked. Unfortunately the task is for the code to function in Hebrew not English.
When the code appears, it shows '?????? ???' in the UserInput value when user inputs Hebrew text during debugging.
It seems the character type is not supported, how can I make the character type support the Hebrew text? Is it a different Unicode?
Sub HighlightDupl()
Dim UserInput As String
Dim SentArray() As String
Dim n As Long, i As Long
Application.ScreenUpdating = False
UserInput = InputBox("הדבק משפט לבדיקת כפילויות -- Paste sentence to check for duplicates", "הַצהָרָה -- Statement")
' Check if input is empty, if yes - quit program
If UserInput Is Nothing Then Exit Sub
TargetList = Array(UserInput)
For i = 0 To UBound(TargetList)
Set range = ActiveDocument.range
With range.Find
.Text = TargetList(i)
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(Forward:=True) = True
range.HighlightColorIndex = wdDarkRed
Loop
End With
Next
End Sub
The Hebrew in the code is also not read properly so I removed it from the code I ran, I left it there now as a reference for Hebrew text.
Recently when trying to work on the program again, the code gave a few runtime errors like Type Mismatch. Was there an update in the last few weeks that suddenly made my code error?
I changed the UserInput variable to a Variant type after and there was some progress but the code didn't function as intended. The content will be mostly Hebrew text but there could be numbers as well.
Something else I've tried is to change the input language and proofing language to Hebrew. The document is mixed with both English and Hebrew. Should I have all English removed first for this to work?
At a general level, you could use a wildcard Find/Replace, where:
Find = (<*>) \1
Replace = ^&
and you set the Highlight colour to whatever you prefer. No macros needed.
The above wildcard Find/Replace will find any duplicate pair of words (Hebrew or otherwise) separated by a space, without the need to nominate the words concerned.
To be able to use Unicode (e.g. Hebrew) input via a VBA InputBox, you need to do two things:
in the Windows Control Panel, choose Region>Administrative>Change System Local> Current system locale: and set the native language you want to use. Note that this only directly affects programs that don't support Unicode. You will need to restart Windows for this change to take effect.
In VBA Editor, choose Tools>Options>Editor Format>Font and select, say, Courier New (or any other font that supports Unicode).
I'm trying to do a search and replace on a document that finds all 'small' page breaks ('^m' in Word/VBA), and replaces them with a 'full' page break. These small page breaks all have a paragraph mark after them. When that is removed, they become a full page break.
How do I search and replace for all instances of "^m" and remove one character immediately to the right of any selection found?
In old versions of word, you could tick a box for 'split apart page break and paragraph marks', but no longer. The documents I'm working with are being fed through software that doesn't know how to read small page breaks, hence the need to remove the paragraph marker immediately after any that are found.
I've noticed this can be accomplished by replacing the '^m' with '^m ', which removes the paragraph marker after the page break and inserts a space instead, but then I can't seem to figure out how to remove the space immediately afterward (AND maintain the full sized page break).
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^m"
.Replacement.Text = "^m "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.MoveRight
Selection.TypeBackspace
Selection.TypeBackspace
SendKeys ("{BACKSPACE}")
This code does, somehow, find 'small' page breaks, replace them with 'full' page breaks, and remove a space afterwards. However, when there's multiple page breaks in the document it introduces various issues, including random character deletion (not surprising considering the code).
Any help would be appreciated! I've managed to use VBA to do a lot of amazing things so far, but this (admittedly very basic) task has me stumped, as I don't fully understand how to use the selection/replacement code.
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.
i want to automatically check my document for certain properties:
a space before a parenthesis
look for weasel words, that is specified in a list
Unprintable characters (I set a lot of equations and then changed the type - not good)
Ideally I would like word to marker all these with a comment, so i can fix them later.
Is this testing of text possible/feasible/already existing in word?
Unfortunately, you can't run it at once as it's not possible to highlight hidden document marks if you need some. Try this two subs for different goals (read some comments inside subs, too)
Sub Searching_For_Text()
'will highlight all options added with .HitHighlight method
With ActiveDocument.Content.Find
.ClearHitHighlight
'words
.HitHighlight "Variant" 'any word this way
'any text
.HitHighlight " (" 'this for spaces before parenthesis
'partially specified text
.HitHighlight "doc" 'simply this for words like: _
document, documents
'option 2- this can't be highlighted but no error returned
.HitHighlight "^p" 'paragraph mark
.HitHighlight "^l" 'soft line mark
End With
End Sub
And for special document marks:
Sub Searching_Special_marks()
'for paragraph marks you need to search each separately
'each time you call it you will find next one, next to current selection point
With Selection.Find
'some cleaning
.ClearFormatting
'searching each separately
.Text = "^p" '^l for soft lines, _
^t for tabs, etc.
.Forward = True
.Execute
End With
End Sub
I think you need to make some experiments with these possible solutions.