so i have this code, it replaces every X in the text with "Asunto (1,2,..): Expediente N°". I try to style this part of the code:
What i want is put that text that replaces the X in Arial 11 Bold
.Text = "Asunto" & " " & i & " " & "Expediente N°"
i tried this but the style applies to the whole document instead of just that text, i don´t know what else to try
Sub Macro1()
'
' Macro1 Macro
'
'
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "X"
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
i = i + 1
.Text = "Asunto" & " " & i & " " & "Expediente N°"
.Find.Execute
.Collapse wdCollapseEnd
.Find.Execute
Loop
With .Font
.Bold = True
.Name = "Arial"
.Size = 11
End With
Application.ScreenUpdating = True
MsgBox i & " Coincidencias."
End With
End Sub
You are NOT applying a Style - all you're doing is overriding whatever Style is already present with hard formatting. Do do with a Style, try for example:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "X"
.Replacement.Text = ""
.Format = False
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Do While .Find.Execute
i = i + 1
.Text = "Asunto" & " " & i & " " & "Expediente N°"
.Style = wdStyleStrong
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
In the above, I've employed Word's 'Strong' Style, via the constant wdStyleStrong. If your text is already 11pt Arial, that's all you need. Otherwise, you should define a suitable 11pt Arial Bold character Style and apply that.
Related
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.
I have written a macro in Word to convert US spellings to UK. In summary, it looks like this:
US_spelling = analyze
UK-spelling = analyse
Call Spell_change (US_spelling, UK_spelling)
The Spell_change sub changes the spelling, adds a comment to the document, and adds 1 to a counter.
I repeat the above three lines, i.e. call the Spell_change sub, about 140 times (for 'program', 'dialog' etc).
Is there a more efficient way of doing this?
Many thanks.
Since you've changed the tag to refer to VBA, perhaps:
Sub Demo()
Application.ScreenUpdating = False
Dim StrFnd As String, StrRep As String, i As Long, Cmt As Comment, StrOut As String
StrFnd = "analyze,color,labor"
StrRep = "analyse,colour,labour"
StrOut = "US_spelling" & vbTab & "UK_spelling"
With ActiveDocument
With .Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = True
For i = 0 To UBound(Split(StrFnd, ","))
.Text = Split(StrFnd, ",")(i)
.Replacement.Text = Split(StrRep, ",")(i)
.Execute Replace:=wdReplaceAll
If .Found = True Then StrOut = StrOut & vbCr & Split(StrFnd, ",")(i) & vbTab & Split(StrRep, ",")(i)
Next
End With
Set Cmt = .Comments.Add(Range:=.Range(0, 0), Text:=StrOut & vbCr & "Total: " & UBound(Split(StrOut, vbCr)))
With Cmt
.Author = ""
With .Range.Paragraphs
.First.Range.Font.Bold = True
.Last.Range.Font.Bold = True
End With
End With
End With
Application.ScreenUpdating = True
End Sub
The above code inserts a comment at the top of the document with a record of all words found & changed, plus a count of those words (but not how many times each word was replaced).
I am trying to insert fields within the headings in a Word document, not before or after them. This is to prepare Word files for import to Madcap Flare which allows file names to be specified within a private Word field. The following code doesn't work because the field is appended before the start of the heading, it needs to be embedded within it. How can I do this.
Sub prepareDocForImport()
Dim headingText As String '
With Selection.Find
.ClearFormatting
.Wrap = wdFindContinue
.Forward = True
.Format = True
.MatchWildcards = False
.Text = ""
.Style = ActiveDocument.Styles("Heading 1 ")
.Execute
While .Found
headingText = Selection.Range.Text
headingText = Replace(headingText , " ", "_")
headingText = LCase(headingText )
Selection.Collapse Direction:=wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PRIVATE:MADCAP:FILENAME<" & headingText & ">")
.Execute
Wend
End With
End Sub
Try moving the cursor one character into the header word.
I also added a line for ensuring that the search starts at the beginning of the document.
Sub prepareDocForImport()
Dim headingText As String
Dim myfield As Field
'Moving to beginning of doc in case a different starting point is selected
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindContinue
.Forward = True
.Format = True
.MatchWildcards = False
.Text = ""
.Style = ActiveDocument.Styles("Heading 1 ")
.Execute
While .Found
headingText = Selection.Range.Text
headingText = Replace(headingText, " ", "_")
headingText = LCase(headingText)
Selection.Collapse Direction:=wdCollapseStart
'Move seleection one character into the header text
Selection.MoveRight Count:=1
Set myfield = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:="PRIVATE:MADCAP:FILENAME<" & headingText & ">", _
PreserveFormatting:=True)
.Execute
Wend
End With
End Sub
I am trying to use a Word Macro to select all text with the style "Number_List" and call the .ConvertNumbersToText function on it. I am having trouble only finding the list or that specific style.
Dim selBkUp As Range
Set selBkUp = ActiveDocument.Range(ActiveDocument.Range.Start, ActiveDocument.Range.End)
With ActiveDocument.Range.Find
.Style = ActiveDocument.Styles("Number_List")
.Forward = True
.Wrap = wdFindContinue
Dim SearchSuccessful As Boolean
SearchSuccessful = .Execute
If SearchSuccessful Then
selBkUp.Select
Selection.Range.ListFormat.ConvertNumbersToText
Else
' code
End If
End With
I select the entire document and covert all of the lists numbers to text, but I am trying to only select ones with that specific style or avoid the other 5 styles that may or may not be present. Any help would be appreciated!
The following code will search for one style and, if found, will convert to another style. Below this code is another subroutine that will list all styles found in a document.
' From http://forums.codeguru.com/showthread.php?448185-Macro-to-Change-Styles-in-Word
' This code will search for a specified Style and convert that to another Style
Sub FindReplaceStyle()
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Style = "Normal" ' Look for 'Normal'
'.Text = ""
.Replacement.Style = "Heading 1" ' Change to 'Heading 1'
' .Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute()
If Selection.Start = Selection.Paragraphs.First.Range.Start Then
Selection.Style = "Heading 1"
End If
Selection.Collapse wdCollapseEnd
Loop
End Sub
The code below will produce a list of all styles found in a document.
Also, I get an error trying to use your "Number_List"
' Following code from: http://www.vbaexpress.com/forum/showthread.php?41125-How-to-get-all-the-applied-Paragraph-Styles-of-a-document
Sub GetActiveStyles()
Application.ScreenUpdating = False
Dim RngStory As Range, oSty As Style, StrType As String, StrStyles As String
With ActiveDocument
For Each oSty In .Styles
For Each RngStory In .StoryRanges
With RngStory.Find
.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Style = oSty.NameLocal
.Forward = True
.Wrap = wdFindStop
.Format = True
.Execute
If .Found Then
Select Case oSty.Type
Case wdStyleTypeCharacter: StrType = "Character"
Case wdStyleTypeList: StrType = "list"
Case wdStyleTypeParagraph: StrType = "Paragraph"
Case wdStyleTypeTable: StrType = "Table"
End Select
StrStyles = StrStyles & oSty.NameLocal & " (" & StrType & ")" & vbCr
Exit For
End If
End With
Next RngStory
Next oSty
End With
Debug.Print StrStyles
'MsgBox StrStyles
Application.ScreenUpdating = True
End Sub
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