Replacing a beacon by a Bookmark in word using excel vba - vba

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.

Related

VBA Word Copy text from specific word till the end of the document

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

How To Move Minus Sign From Right To Left?

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

make selection in a whole document

in a file MS WORD I wish to select a "/" and make bold all characters till the following ^p. The following code works well only for the first occurrence. In the file I have many occurrences and I am not able to apply this in the whole file. I tried several times with "for...next" and others, unfortuntely without success.
Many thanks for your help! Gianluca
Sub bold_title()
Set myRange = ActiveDocument.Content
Selection.Find.ClearFormatting
With Selection.Find
.Text = "/"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute
Selection.Font.Bold = True
End Sub
The trick is to "collapse" the Selection- think of it like pressing the right-arrow key - so that the next search starts after the "found" text:
Selection.Collapse wdCollapseEnd
Usually, a Do While...Loop construct is used in order to repeat the Find until the end of the document. You'll find lots and lots of examples if you search here and elsewhere on the Internet.
For this to work successfully, make sure you set Wrap to wdFindStop (and not wdFindContinue as is currently in the code shown).

VBA in MS Word affects whole document, not just selection

I want to have a VBA script perform a find / replace in a selection and then assign the macro to a button on the Quick Access ToolBar, to save having to click through the usual Find/Replace procedure.
I recorded a macro while doing this and this is what I get:
Sub FindReplace()
'
' FindReplace Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
But when I run this macro on a selection to change paragraph marks into spaces it continues on to change the whole document, not just the selection.
I'm no stranger to VBA but I can't see how to fix it so it stops when the selection is done.
You are going to need to use
.Wrap = wdFindStop
Instead of
.Wrap = wdFindAsk

VBA word macro / automatically find and replace specific characters for whole document

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