1
votes

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
1
If you do this Find/Replace manually (Ctrl+H for the dialog box) does it work? And if you then record that in a macro how does that code compare to the code in the question? Right off-hand, I'd say the problem might be you need to change this .Format = False to True...Cindy Meister
You were spot on, .Format = True for those format changes to take placeMiguel A

1 Answers

0
votes

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