I have some faulty paragraphs, which are causing my other macros to not work properly.
They are usually heading style 2, style 3
Empty (not sure)
before OR after table (not sure)
surrounded by dotted line
causes the heading and next table to merged together (not sure)
I tried to replace/removed those with the following macro:
Sub HeadingParaBug()
Dim H As Range
Set H = ActiveDocument.Range
LS = Application.International(wdListSeparator)
With H.Find
.Text = "^p "
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = " ^p"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^p ^p"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
.Text = "^13{2" & LS & "}"
.Replacement.Text = "^p"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Text = ""
.Style = wdStyleHeading2
.MatchWildcards = False
Do While .Execute
If H.Text <> vbCr Then
H.Collapse 0
H.Select
H.InsertParagraph
H.Delete
End If
H.Collapse 0
Loop
End With
Set H = ActiveDocument.Range
With H.Find
.Style = wdStyleHeading3
Do While .Execute
If H.Text <> vbCr Then
H.Collapse 0
H.Select
H.InsertParagraph
H.Delete
End If
H.Collapse 0
Loop
End With
End Sub
But somehow, it do not completely removed/replace the faulty paragraph marks. The above macro finds those paragraphs, add new and then remove it. which eventually removed the dotted line.
Can anybody explain this phenomena? what is the right ways to remove/replace those paragraphs. please download and see test file with error on page 7
Update: Even I tried with the following code but it did nothing (on MacOS Video). I think it is not finding the hidden paragraphs:
Sub HidNempty()
Dim H As Range
Set H = ActiveDocument.Range
With H.Find
.Text = "^p "
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = " ^p"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^p ^p"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
.Text = "^p"
Do While .Execute
If H.Font.Hidden = True Then
H.Font.Hidden = False
If Len(Trim(H.Paragraphs(1).Range.Text)) = 1 Then
H.Delete
End If
End If
Loop
End With
End Sub
To unhide all document paragraphs, please try the next piece of code:
Sub UnHideParagraphs()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If para.Range.Font.Hidden Then
para.Range.Font.Hidden = False
End If
Next para
End Sub
It should work even if only part of the paragraph range is hidden...
Find/Replace won't delete duplicate paragraph breaks before a table, between tables, or after a table. Try:
Sub Demo()
Application.ScreenUpdating = False
Dim LS As String, Tbl As Table, bHid As Boolean
LS = Application.International(wdListSeparator)
bHid = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = True
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.Replacement.Font.Hidden = False
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "^p^w"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^w^p"
.Execute Replace:=wdReplaceAll
.MatchWildcards = True
.Text = "^13{2" & LS & "}"
.Execute Replace:=wdReplaceAll
.Wrap = wdFindStop
End With
Do While .Find.Execute = True
With .Duplicate
.Font.Hidden = False
.Start = .Start + 1
.Text = vbNullString
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
For Each Tbl In ActiveDocument.Range.Tables
With Tbl.Range
Do While .Characters.First.Previous.Previous = vbCr
.Characters.First.Previous.Previous = vbNullString
Loop
.Characters.First.Previous.Font.Hidden = False
Do While .Characters.Last.Next = vbCr
If .Characters.Last.Next.End = ActiveDocument.Range.End Then Exit Do
If .Characters.Last.Next.Next.Information(wdWithInTable) = True Then Exit Do
.Characters.Last.Next = vbNullString
Loop
.Characters.Last.Next.Font.Hidden = False
End With
Next
ActiveWindow.View.ShowHiddenText = bHid
Application.ScreenUpdating = True
End Sub
You will observe various lines in the code that apply .Font.Hidden = False. Depending on what you're trying to achieve visually, you may or may not want those.
Related
I have a transliteration function (from cyrillic to latin). I will use this function in a unviersal subroutine (with text of any lenght). This sub must to copy the source text, transliterate (from cyrillic to latin) and paste it below without any formatting changes and without using selection. The next step is reverse transliteration (again copy and paste below). There must be 3 textes in the final. I kinda know how to realize it, but i don't know what i should use instead of selection.
*
P.S. i tried use For Each word In ActiveDocument.Range.Words but it works bad with reverse transliteration (exactly that. without it, the function works perfectly in debugging)
P.P.S. sorry for mistakes in the text, i'm not a native speaker
Since you haven't posted any actual transliteration code, I'll leave you to add the cyrillic and latin character sets to the code below:
Sub Transliterate()
Application.ScreenUpdating = False
Dim p As Long, i As Long, StrLng1, StrLng2
'Insert the character codes for the cyrillic characters here
StrLng1 = Array(ChrW(&H430), ChrW(&H431), ChrW(&H432))
'Insert the corresponding latin characters here
StrLng2 = Array("a", "b", "c")
With ActiveDocument.Range
Do While .Characters.Last.Previous = vbCr
.Characters.Last.Previous.Delete
Loop
.InsertAfter vbCr
'Duplicate Content
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindContinue
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^l"
.Execute Replace:=wdReplaceAll
.Font.Bold = True
.Text = "[!^l]#^l"
.Replacement.Text = "^p^&"
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Text = "^l^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Execute Replace:=wdReplaceAll
.Text = "[!^13]#^13"
.Replacement.Text = "^&^&^p"
.Execute Replace:=wdReplaceAll
End With
.Characters.Last.Previous.Delete
.Characters.First.Delete
'Loop through duplicated paragraphs
For p = .Paragraphs.Count - 1 To 2 Step -3
With .Paragraphs(p).Range
.Font.Italic = True
'Transliterate paragraph
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindStop
.MatchWildcards = False
.MatchCase = True
.Font.Bold = False
For i = 0 To UBound(StrLng1)
.Text = StrLng1(i)
.Replacement.Text = StrLng2(i)
.Execute Replace:=wdReplaceAll
Next
End With
'Duplicate translated paragraph
.Characters.Last.Next.FormattedText = .FormattedText
End With
Next
.Characters.Last.Previous.Delete
'Loop through duplicated paragraphs
For p = .Paragraphs.Count To 3 Step -3
With .Paragraphs(p).Range
.Font.Underline = wdUnderlineSingle
'Reverse Transliterate paragraph
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Wrap = wdFindStop
.MatchWildcards = False
.Font.Bold = False
.MatchCase = True
For i = 0 To UBound(StrLng1)
.Text = StrLng2(i)
.Replacement.Text = StrLng1(i)
.Execute Replace:=wdReplaceAll
Next
End With
End With
Next
End With
Application.ScreenUpdating = True
End Sub
I want to delete section break or page break at current page.but thes vba code does not work.how to modfiy it?
Sub Del_sectionbreakORpagebreak()
Selection.Bookmarks("\page").Range.Select 'select current page
With Selection.Find
.ClearFormatting
.Execute FindText:="^b", Format:=True 'find section break
fnd = .Found
End With
If fnd = True Then
With Selection.Find
.ClearFormatting
.Text = "^b"
.Replacement.Text = " "
.Forward = True
.Execute Replace:=wdReplaceOne
End With
Else
With Selection.Find
.Text = Chr(12)
.Replacement.Text = ""
.Forward = True
.Execute Replace:=wdReplaceOne
End With
End If
End Sub
Sub Del_sectionbreak()
Selection.Bookmarks("\page").Range.Select
With Selection.Find
.ClearFormatting
.Execute FindText:="^b", Format:=True
fnd = .Found
End With
If fnd = True Then
Selection.Delete
Else
With Selection.Find
.Text = Chr(12)
.Replacement.Text = vbNullString
.Forward = True
.Execute Replace:=wdReplaceOne
End With
End If
End Sub
the code fuction is delete sectionbreak or pagebreak in current page
You can't delete an automatic page break. For other page/section breaks:
Sub Demo()
With Selection.Bookmarks("\page").Range.Characters.Last
If .Previous.Text = Chr(12) Then .Previous.Text = vbNullString
If .Text = Chr(12) Then .Text = vbNullString
End With
End Sub
The below VBA Word macro is run after selecting several paragraphs or for this example all of them.
I attach a sample .rtf file on which to run the macro.
The biblical references at the start of the paragraphs all get a pair around them, except the one that has a hyperlink. Is my macro at fault or is this an issue with Word 2010. As a secondary point it would be helpful to know if this works on Office 365
(I have tried the same, on LibreOffice and it does match even if word is hyperlinked one
(^)([A-Z123I ]{1,3}[^ ]{1,15} )([0-9]{1,3}:[0-9-\–]{1,7})
$1$2$3$2$3
So please don't suggest that I have not made any effort to find if this should work, or that I have not tried different settings. It would have been more helpful for someone to post that it did not work for them to at least show they had taken the time to download the macro test file and actually do a test)
Private Sub RelRefWithBibleName_Click()
InSelection = False
If selection.Type = wdSelectionIP Then InSelection = True
If InSelection = True Then
MsgBox ("select some text")
Exit Sub
End If
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
selection.Find.Replacement.Font.Reset
Application.ScreenUpdating = False
With selection
'Added this to make selection go beyond the start of the selected paragraph
'so that the detection would work
selection.MoveStartUntil Cset:=wdCharacter, Count:=wdBackward
strFindText = "([^13])([A-Z123I ]{1,3}[! ]{1,15} )([0-9]{1,3}:[0-9\-\–]{1,7})"
strReplaceText = "\1<ref>\2\3</ref>\2\3"
End With
With selection.Find
.MatchWildcards = True
.ClearFormatting
.Replacement.ClearFormatting
.text = strFindText
.Replacement.text = strReplaceText
.Format = False
.MatchWholeWord = True
.Forward = True
.Wrap = wdFindStop
End With
selection.Find.Execute Replace:=wdReplaceAll
selection.Shrink
selection.Move
Application.ScreenUpdating = True
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
End Sub
Looping through the hyperlinks collection is no big deal. That said, there is another way:
Sub Demo()
Application.ScreenUpdating = False
Dim RngFnd As Range, StrTxt As String
With Selection
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[A-Z1-3 ]{1,3}[! \<\>]{1,15} [0-9]{1,3}:[0-9\-\?]{1,7}"
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .InRange(RngFnd) Then
If .Paragraphs.Count > 1 Then .Start = .Paragraphs(1).Range.End
If .Start = .Paragraphs(1).Range.Start Then
StrTxt = .Text
.InsertBefore "<ref>" & StrTxt & "</ref>"
.Font.Bold = False
.Start = .End - Len(StrTxt)
.Font.Bold = True
End If
If .Hyperlinks.Count > 0 Then
If .Hyperlinks(1).Range.Start = .Paragraphs(1).Range.Start Then
With .Hyperlinks(1).Range
StrTxt = .Text
.InsertBefore "<ref>" & StrTxt & "</ref>"
.Font.Bold = False
.Start = .End - Len(StrTxt)
.Font.Bold = True
End With
End If
End If
Else
Exit Do
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
RngFnd.Select
Application.ScreenUpdating = True
End Sub
There is nothing wrong with your Find/Replace expressions, though they could be simplified:
strFindText = "([^13])([A-Z1-3 ]{1,3}[! ]{1,15} [0-9]{1,3}:[0-9\-\–]{1,7})"
strReplaceText = "\1<ref>\2</ref>\2"
The Word version is of no consequence. For hyperlinks, you could loop through the hyperlinks collection and, if applicable, testing the display text, before inserting the tags either side of them.
Thanks in advance for any reply.
I am working on presentation of some reports. The periodical reports are imported from a different software into Word template. For all tables and for each row I would like to change the color of the negative numbers in column 14 only if there is a certain text in column 3.
Unfortunately I have to use a Word template to do this. It seems that a macro is my only option so I have tried to Frankenstein something from different macros I found online:
Dim varColumn As Column
Dim clColumn As Column
Dim cCell As Variant
Set clColumn = Selection.Columns(3)
Set varColumn = Selection.Columns(14)
With clColumn
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.text = "value"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
Selection.MoveRight Unit:=wdCell, Count:=11
End If
If cCell < 0 Then
Selection.Font.color = wdColorRed
End If
Loop
End With
End Sub
I think the macro needs lines to repeat the search. See the two lines added before Loop.
With Selection
.HomeKey Unit:=wdStory 'Starts at the beginning, to search all tables.
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "value"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True And _
.Cells(1).ColumnIndex = 3 Then 'Confirms it's in the 3rd column.
.MoveRight Unit:=wdCell, Count:=11
End If
If .Range < 0 Then
.Font.Color = wdColorRed
End If
.Collapse wdCollapseEnd 'Collapses the selection to no characters.
.Find.Execute 'Searches again from the current selection point.
Loop
End With
Try:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "-[0-9][0-9,.]{1,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found = True
If .Information(wdWithInTable) = True Then
If .Cells(1).ColumnIndex = 14 Then
If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
.Font.ColorIndex = wdRed
End If
End If
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
If the table might have vertically-merged cells, change:
If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
to:
If Split(.Tables(1).Cell(.Cells(1).RowIndex, 3).Range.Text, vbCr)(0) = "specified text" Then
I'm trying to list every instance of the "Heading 1" style within my document (to eventually list them in a combobox on a form).
The following code appears to find the instances of "Heading 1", as there are the correct number of entries listed within the Immediate Window, but .text is returning nothing.
What am I doing wrong? Thanks.
Dim blnFound As Boolean
Dim i as Integer
i = 1
With ThisDocument.Range.Find
.Style = "Heading 1"
Do
blnFound = .Execute
If blnFound Then
Debug.Print i & " " & .Text
i = i + 1
Else
Exit Do
End If
Loop
End With
I don't believe the object you have has a .Text property. Selection has a .Text property. Try this:
Sub FindHeadings()
' October 28, 2014
Dim blnFound As Boolean
Dim i As Integer
i = 1
' Set up the find conditions
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, count:=1
'Repeat while you find the style
blnFound = True
While blnFound
With Selection.Find
.Execute
If .Found = True Then
Debug.Print i & " " & Selection.Text
i = i + 1
' Move to the next character
Selection.MoveRight Unit:=wdCharacter, count:=1
Else
Debug.Print "Done"
blnFound = False
End If
End With
Wend
End Sub