Change font on selected text in macro for MS Word












1















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









share|improve this question

























  • 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
    Nov 24 '18 at 5:54











  • You were spot on, .Format = True for those format changes to take place

    – Miguel A
    Nov 24 '18 at 9:49
















1















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









share|improve this question

























  • 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
    Nov 24 '18 at 5:54











  • You were spot on, .Format = True for those format changes to take place

    – Miguel A
    Nov 24 '18 at 9:49














1












1








1








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









share|improve this question
















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






vba ms-word word-vba






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:40







Miguel A

















asked Nov 24 '18 at 2:06









Miguel AMiguel A

113




113













  • 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
    Nov 24 '18 at 5:54











  • You were spot on, .Format = True for those format changes to take place

    – Miguel A
    Nov 24 '18 at 9:49



















  • 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
    Nov 24 '18 at 5:54











  • You were spot on, .Format = True for those format changes to take place

    – Miguel A
    Nov 24 '18 at 9:49

















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
Nov 24 '18 at 5:54





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
Nov 24 '18 at 5:54













You were spot on, .Format = True for those format changes to take place

– Miguel A
Nov 24 '18 at 9:49





You were spot on, .Format = True for those format changes to take place

– Miguel A
Nov 24 '18 at 9:49












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454605%2fchange-font-on-selected-text-in-macro-for-ms-word%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer




























      0














      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






      share|improve this answer


























        0












        0








        0







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 9:52









        Miguel AMiguel A

        113




        113
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454605%2fchange-font-on-selected-text-in-macro-for-ms-word%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen