There seems to be a problem when trying to get different kinds o quote characters when replacing text:
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\{QUOTE:(*):QUOTE\}"
.Replacement.Text = Chr(147) & "\1" & Chr(148)
.Forward = True
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
When the document contains: {QUOTE:abc:QUOTE}
the code gives: ”abc”
but expected result is: “abc”
Versions of Word: “Microsoft Office Professional Plus 2013” and “Microsoft Office 365 ProPlus”.
The problem does not happen when File->Options->Proofing->AutoCorrect Options->AutoFormat As You Type->Replace as you type->"Straight quotes" with “smart quotes” is not activated. Of course I want the macro to work regardless of any setting for manual typing.
How can the code be changed to get the expected result?
The way to solve this kind of problem is to turn the option off for the duration of the code, then turn it back on again if it was on.
The following code snippet saves the user's setting, turns off the option, then restores the user's setting (whether or not the option was on or off).
Dim bReplaceQuotes as Boolean
'Save the user's setting
bReplaceQuotes = Options.AutoFormatAsYouTypeReplaceQuotes
Options.AutoFormatAsYouTypeReplaceQuotes = False
'Do the FindReplace
'Restore the user's settings at the end
Options.AutoFormatAsYouTypeReplaceQuotes = bReplaceQuotes
Related
My company's workflow currently includes a process where we export documentation from a repository, clean it up a bit, and send it down the line for review/approval. The "clean it up a bit" involves running a macro that, among other things, does a find/replace on styles to change them from what comes out of the repository, to what is specified by our document template. So, for example, the repository gives us a style for "p_body," and we need that to be "body". This macro works pretty well, except for a couple of things that I'd like to see if we can polish up.
The first issue is that the repository tends to behave somewhat unpredictably when it comes to the styles that it delivers. To use the example from above, instead of tagging all of the "p_body" text in that style, it comes back with "p_body", "p_body_1", "p_body_1_1," and the like. Currently, the style switching in the macro is all hard-coded, so in order for the macro to fix something, it has to know it exists. Thus, if the repository spits out a style that's not already in the macro, it won't get fixed. I know that if I was dealing with an HTML doc, I could use a regular expression to find all versions of the "p_body" style, and replace them with "body." Is there a way to use such more intelligent find/replace version instead of hard-coding each style that needs to be replaced?
The second issue will take a separate question to fix, so it's now being snipped out.
For posterity, here's what one of the macro find/replace blocks looks like:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("p_body")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("body")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
For dealing with the style name variations on p_body and others try this. You fill the StyName1 array with the names to find, and in the StyName2 array put the replacement names. It must be a one-for-one match-up.
This is just one of many ways to code it.
Sub StyleNames()
Dim StyName1 As Variant, StyName2 As Variant
StyName1 = Array("p_body", "p_bold")
StyName2 = Array("body", "bold")
Dim i As Long
Dim findStyName As String, replaceName As String
For i = LBound(StyName1) To UBound(StyName1)
findStyName = StyName1(i)
replaceName = StyName2(i)
FindAndReplaceStyles findStyName, replaceName
Next
End Sub
Private Function FindAndReplaceStyles(ByRef findStyName As String, ByRef replaceName As String)
Dim sty As Word.Style, aStory As Word.Range
For Each sty In ActiveDocument.Styles
If InStr(1, sty.NameLocal, findStyName) Then
For Each aStory In ActiveDocument.StoryRanges
With aStory.Find
.ClearFormatting
.Format = True
.Forward = True
.MatchWildcards = False
.Style = sty.NameLocal
.Text = ""
.Wrap = wdFindStop
.Replacement.ClearFormatting
.Replacement.Style = replaceName
.Replacement.Text = ""
.Execute Replace:=Word.wdReplaceAll
End With
Next
Exit For
End If
Next
End Function
I am trying to add text to empty formatted bullet points in a word document, however I can't seem to find any successful way of doing so. I'm not very good at VBA, I just use it to automate reoccuring reports.
This is the format of VBA subs I've been using to find and replace text, I just can't find a way to adjust for adding to bullet points:
Private Sub FixedReplacements()
Dim Rng As Range
Dim SearchString As String
Dim EndString As String
Dim Id As String
Dim Link As String
Set Rng = ActiveDocument.Range
Rng.Find.ClearFormatting
Rng.Find.Replacement.ClearFormatting
With Rng.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Rng.Find.Execute Replace:=wdReplaceAll
End Sub
The goal I have set for empty bullet points is for them to display something along the lines of "No further information." << Just an example.
How this can be done depends very much on how the bullets were inserted. There is no way to specifically search the bullet, itself. A comment mentions
bullet points are the formatted bullet points found straight out of
word.
In that case, the default setting would be to format the paragraphs with the List Paragraph style. If that's the case, here, then Find can search for paragraphs using that style. The Find code in the question would then look as follows. (Note also the changes to the Format and Wrap properties.)
With Rng.Find
.Text = "^p"
.Replacement.Text = "No further information.^p"
.Forward = True
.Style = "List Paragraph"
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If a different style has been used to apply bullets, then that style name can be searched. Keep in mind that style names are case-sensitive.
There is an option in File/Options/Advanced, Editing options section: Use Normal style for bulleted or numbered lists. If this has been activated then things become very difficult. Best you can do is to try to match the applied paragraph formatting (indents, etc.).
I am having to add some functionality to an existing macro for MS Word. This particular macro finds a specific text "#code_bar#" in an existing MS Word document and replaces it with a different text i.e. 3541589479.
Now, once this text is found and replaced (which the macro is already doing correctly), it is meant to change the font to "Free 3 of 9 Extended",which is already imported into Word.
This is the code, which is working properly except the part where I try to change the font and the size, which is actually not taking place. Could anyone help? Thanks.
Public Function sustituirCodigoBarras(codigo_barras)
Dim codigoDeBarras As String
Set codigoBarras = ActiveDocument.Content
'#barras_pedido# codigo de barras del pedido
codigoDeBarras = "#code_bar#"
With obj_Word.ActiveWindow.Selection.Find
.Text = codigoDeBarras
.Replacement.Text = "*" & codigo_barras & "*"
.Replacement.Font.Name = "Free 3 of 9 Regular"
.Replacement.Font.Size = 34
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End Function
After some extra digging and a good-night sleep here's the answer:
.Format needs to = true in order for those changes to actually take place
The font name needs to be correct. In my case I was trying to use the Free 3 of 9 Extended (which was imported into my computer) and not the Regular (which was not imported and therefore not found) as in the code above.
Rookie mistake.
Hope this can help someone else
This is my first post, I am not a programmer.
I built a Word macro (visual basic) that does a search and replace for many items (dates and numbers) within a highlighted block of text. Here's a single search and replace segment. It works well, but after each segment a Y/N dialogue box (in this case Word 2003) appears asking if I want to search the remainder of the document--I DONT.
Query: Is there anything I can add to macro that would respond "NO" (after each search and replace segment) as the macro runs so I don't have to select "no" after each of the 20 or so segments?
Typical segment:.
Selection.Find.ClearFormatting.
Selection.Find.Replacement.ClearFormatting.
With Selection.Find.
.Text = "2015".
.Replacement.Text = "2016".
.Forward = True.
.Wrap = wdFindAsk.
.Format = False.
.MatchCase = False.
.MatchWholeWord = False.
.MatchWildcards = False.
.MatchSoundsLike = False.
.MatchAllWordForms = False.
End With.
Selection.Find.Execute Replace:=wdReplaceAll
...
THX
The line
.Wrap = wdFindAsk
tells Word that you want it to ask the user whether to keep searching. Changing that line to
.Wrap = wdFindStop
should fix your problem.
try this:
Application.DisplayAlerts = False
*YOUR CODE HERE*
Application.DisplayAlerts = True
Not sure about Word 2003 (!)
this is my first question,
I have to go over a large number of documents and make sure that several formatting issues are correct. An example of such an issue would be to make sure that all periods, ".", are not bold, italics, underline, etc. Another example would be to make sure that all "etc." are in italics.
I have a list of the needed formatting issues.
Instead of going over each document and using the find/replace function I would rather write a macro that I can apply to each document.
I have no experience with VBA. I do on the other hand, have some experience with programming in C sharp and C in general.
Any help would be greatly appreciated.
BTW, I'm not asking for a complete program, rather a sample from which I can learn and continue with my own.
There are a couple options:
1.Under the Developer tab in Word, you can hit the "Record Macro" button and do a find and replace multiple times while recording the macro using the ctrl + H shortcut.
2.Have multiple smaller macros setup (such as the two below) hit the "Record Macro" and run them in the order that you want.
Sub ItalicizeEct()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = "ect."
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Sub RemoveBoldPeriods()
Dim PunctAllRng As Word.Range
Set PunctAllRng = ActiveDocument.Range
With PunctAllRng.Find
.Format = True
.Text = "."
.Font.Bold = True
.Replacement.Text = "."
.Replacement.Font.Bold = False
.Execute Replace:=wdReplaceAll
End With
End Sub
3.Or simply write a large macro that will go through all the editing processes you need