I need an MSWord macro to convert these below values :
568.63-
682.3-
12.78-
To
-568.63
-682.3
-12.78
You don't actually need a macro for this. All you need is a wildcard Find/Replace with:
Find = (<[0-9.]#)(-)
Replace = \2\1
As a macro, this would become:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(<[0-9.]#)(-)"
.Replacement.Text = "\2\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
As for the 80.9-100.1 issue identified by #Freeflow, I'd be inclined to replace those hyphens with something other than a normal hyphen (e.g. a non-breaking hyphen). In that case, you might use a macro coded like:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "([0-9])-([0-9])"
.Replacement.Text = "\1^~\2"
.Execute Replace:=wdReplaceAll
.Text = "(<[0-9.]#)(-)"
.Replacement.Text = "\2\1"
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
Related
I am developping an excel macro and i need to replace a specific word (beacon) in my .docx by a bookmark, i found this on the microsoft site.
Sub BMark()
' Select some text in the active document prior
' to execution.
ActiveDocument.Bookmarks.Add _
Name:="tableauxvdd", Range:=Selection.Range
End Sub
But i dont know how to fix the range, i have the idea to select the word with smthg like that :
With word_fichier.Application.Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:="#tableauxvdd"
End With
But it need a range and not a selection.
For example:
With word_fichier.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "#tableauxvdd"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
If .Find.Found = True Then .Bookmarks.Add Name:="tableauxvdd", Range:=.Duplicate
End With
Note: As I commented in your other thread, and as demonstrated above, there is no need to use 'Selection' at all for what you are doing.
I need to copy text from specific word (Let's say it is Specific Requirements) till the end of the document.
I have prepared code which could potentially solve the issue but I don't know what has to be added to .text to make it work as I wish.
Sub Copying()
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.MatchWildcards = True
.Wrap = wdFindContinue
.Text = "Specific Requirements"
.Execute
End With
If .Find.Found = True Then .Copy
End With
End Sub
When you use Find, the active Range moves to the Found range. To extend that to the end of the document you need code like:
Sub Copying()
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "Specific Requirements"
.Execute
End With
If .Find.Found = True Then
.End = ActiveDocument.Range.End
.Copy
End If
End With
End Sub
I use the following code in order to remove formatting. Somehow it does not work.
Sub rep_test()
Dim TempS As String
TempS = Replace_chars("]", "]")
End Sub
Function Replace_chars(search_txt As String, replace_txt As String)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = search_txt
.Replacement.Text = replace_txt
.Replacement.Font.Bold = False
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll, Format:=False
End Function
TextSample:
Aaa [BBB] CC [DDD]
Any idea why?
Try:
Sub rep_test()
Call Replace_chars("]")
End Sub
Sub Replace_chars(search_txt As String)
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = search_txt
.Replacement.Text = "^&"
.Font.Bold = True
.Replacement.Font.Bold = False
.Format = True
.Forward = True
.MatchWildcards = False
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
If you want to remove formatting the correct way to do it is to apply the character style "Default Paragraph Font". This will reset the formatting to match the underlying paragraph style. It also has the advantage that you don't need to know what formatting needs to be removed/applied.
Sub FindAndResetFormatting(search_txt As String)
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = search_txt
.Replacement.Style = ActiveDocument.Styles(wdStyleDefaultParagraphFont)
.Forward = True
.Wrap = wdFindStop
.Format = True
.Execute Replace:=wdReplaceAll
End With
End Sub
I'm trying to do the following using the macro:
Upon opening the document, automatically search whole document for brackets {{ }} and delete them including the text inside.
It doesn't do the job well, just operates on the text selected, not the whole document.
Sub SelectToBracketsDelete()
With Selection.Find
.ClearFormatting
.Text = "{{"
.Forward = False
.Wrap = wdFindStop
.Execute
End With
Selection.Extend
With Selection.Find
.Text = "}}"
.Forward = True
.Execute
.Text = ""
End With
Selection.Text = ""
End Sub
Is this what you are looking for?
Word 2007 -> stackoverflow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\{\{*\}\}"
.Replacement.Text = ""
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
I have a selection in a word document which I set with
With Selection.Find
.Text = "Blala"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Tada"
.Forward = True
.Wrap = wdFindStop
.MatchCase = True
End With
Selection.Find.Execute
But in that selection there are also tables, I only want to format text. Is there a way to exclude the tables from that selection?
Thanks!
This code loops through all paragraphs within the selection and only performs the replacement of "Blala" with "Tada" if there are no tables in that paragraph:
Sub ReplaceTextButSkipTables()
Dim rng As Range
Dim p As Paragraph
Set rng = Selection.Range
For Each p In rng.Paragraphs
If p.Range.Tables.Count = 0 Then
With p.Range.Find
.ClearFormatting
.MatchCase = True
With .Replacement
.ClearFormatting
.ParagraphFormat.LeftIndent = InchesToPoints(1.27)
End With
.Execute FindText:="Blala", ReplaceWith:="Tada", Replace:=wdReplaceAll
End With
End If
Next p
End Sub