dcsimg
Results 1 to 5 of 5

Thread: How to apply formatting to a word document

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    1

    How to apply formatting to a word document

    Hello
    i was wondering if somebody could help me. i am trying to write a macro to insert a paragraph before the words "legislation infringed" bold and underline the words legislation infringed. insert a paragraph after legislation infringed.

    i am a complete novice and fumbling around in the dark any help would be greatly appreciated. i have wrote this by pulling lines from other posts i have read. unfortunately it doesn't work.

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = " Legislation infringed: "
    .Replacement.ClearFormatting
    .Font.Bold = True
    .Font.Underline =True
    .Replacement.Text = " Legislation infringed: "
    .Insertbefore Text:=" Legislation infringed:"
    .InsertParagraphbefore
    .InsertAfter Text:=" Legislation infringed:"
    .InsertParagraphAfter.Execute Replace:=wdReplaceAll, Forward:=True, _
    Wrap:=wdFindContinue
    End With


    i would really appreciate if someone could point out where i am going wrong. if i can get this macro written it will save me a huge amount of time.
    also if someone could recommend a good book or learning resource for a complete novice like myself that would be appreciated. there is so much info out there on the internet it is hard to know where to start.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,868

    Re: How to apply formatting to a word document

    "What" doesn't work?
    Is it not even finding your "legislation infringed"?
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: How to apply formatting to a word document

    i would think you need to execute the find method, and better to work with a range object rather than directly with the document content

    i am pretty sure you can only work directly with a range or the selection
    if you specify .font.bold before executing the text will not me found

    just as a start try like
    Code:
    Dim r As Range
    Set r = ActiveDocument.Content
    
    With r.Find
      .ClearFormatting
      .Text = "Legislation infringed:"
    
      .Execute
      r.Font.Bold = True
    
    End With
    you can add other bits and test as you go
    Last edited by westconn1; May 14th, 2019 at 07:52 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,868

    Re: How to apply formatting to a word document

    Pete, nevermind the leading Space-Character in " Legislation infringed: ", and do we know if that Phrase is at the Start of a Line (which is implied with the "want to insert a Paragraph"....)
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: How to apply formatting to a word document

    nevermind the leading Space-Character in " Legislation infringed: "
    i did notice that, but assumed it must be required, in hind sight and with your prompting, probably not, i will edit the code on that basis
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width