Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 34

Thread: fonts in range should be in Normal6 style

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    fonts in range should be in Normal6 style

    [VBA]Dim rng2 As Range
    Set rng2 = ActiveDocument.Range.Duplicate
    With rng2.Find.Font
    .Bold = False
    .italic = False
    .Hidden = False
    .Underline = False
    .Name = "Times New Roman"
    .Size = 12
    rng2.TextRetrievalMode.IncludeHiddenText = False
    End With
    Do While rng2.Find.Execute
    With rng2.Style
    .Name = "Normal6"
    End With
    With rng2.Font
    .Reset
    .Bold = False
    .italic = False
    .Underline = False
    End With

    If rng2.End = ActiveDocument.Range.End Then Exit Do
    rng2.Collapse wdCollapseEnd
    If rng2.Characters(1) = Chr(13) & Chr(7) Then rng2.Move wdCharacter, 1
    Loop[/VBA]

    Does anyone have any idea how can I set the style of selected fonts to Normal6

  2. #2
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    [VBA]Selection.Style = ActiveDocument.Styles("Normal 6")[/VBA]
    -Once my PC stopped working, so I kicked it......Then it started working again

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Why not put it in the F&R?
    [VBA]
    With rng2.Find.Font
    .Bold = False
    .italic = False
    .Hidden = False
    .Underline = False
    .Name = "Times New Roman"
    .Size = 12
    rng2.TextRetrievalMode.IncludeHiddenText = False
    End With
    rng2.Find.Replacement.Style = ActiveDocument.Styles("Normal6")
    rng2.Find.Execute eplace:=wdreplaceall
    [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    wow thnx for a quick reply

    One more question would it be possible to also get other atributes from fonts like if they are centered, line spacing and so on

    and aply those atributes to Normal6 style

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, of course.

    And if you are going to use With, you may as well USE With....
    [vba]With rng2.Find
    With .Font
    .Bold = False
    .italic = False
    .Hidden = False
    .Underline = False
    .Name = "Times New Roman"
    .Size = 12
    End With
    rng2.TextRetrievalMode.IncludeHiddenText = False

    .Replacement.Style = ActiveDocument.Styles("Normal6")
    .Execute Replace:=wdReplaceAll
    End With[/vba]

    One other comment. What the heck is a style named "Normal6"? Very informative.

  6. #6
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Ah nevermind that normal6 I changed this style to normal

    But this is so wierd why does it loop all the time

    [VBA]Dim rng2 As Range
    Set rng2 = ActiveDocument.Range.Duplicate
    With rng2.Find.Font
    .Bold = False
    .italic = False
    .Hidden = False
    .Underline = False
    .Name = "Times New Roman"
    .Size = 12
    rng2.TextRetrievalMode.IncludeHiddenText = False
    End With
    Do While rng2.Find.Execute
    rng2.Find.Replacement.Style = ActiveDocument.Styles("Normal")
    rng2.Find.Execute Replace:=wdReplaceAll
    With rng2.Font
    .Reset
    .Bold = False
    .italic = False
    .Underline = False
    End With

    If rng2.End = ActiveDocument.Range.End Then Exit Do
    rng2.Collapse wdCollapseEnd
    Selection.Collapse direction:=wdCollapseEnd
    If rng2.Characters(1) = Chr(13) & Chr(7) Then rng2.Move wdCharacter, 1[/VBA]

    even on a normal text without tables
    Any ideas

  7. #7
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    my guess is this line:

    [VBA]Do While rng2.Find.Execute [/VBA]

    should be something like

    [VBA]Do While rng2.Find.Execute = true[/VBA]

    I have never used "Do While" before, so this is a wild guess
    -Once my PC stopped working, so I kicked it......Then it started working again

  8. #8
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    I think i know what the problem is, but I dont know how to fix it

    In the text there should be no Bold text at all and then this code exits loop if there is even one word bolded it will go into loop

    strange

  9. #9
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    [vba]
    Do While rng2.Find.Execute = True
    [/vba]

    Tried it still the same,

    I dont get this at all?????

  10. #10
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    put in at the top
    [VBA]on error goto error1[/VBA]

    and below your code:
    [VBA]
    exit sub

    error1:
    msgbox err.description[/VBA]

    NB: If there shouldn't be any bold text couldn't you make all the text in the document not bold before your code runs?
    -Once my PC stopped working, so I kicked it......Then it started working again

  11. #11
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    No no I didnt say there should be no bold text it loops when bold text is in Document

    will try that error thing

    Edited:
    Tried that error thing but all I get is empty message box and it is looping also i have to use ctrl+pause to exit the code

  12. #12
    VBAX Mentor OTWarrior's Avatar
    Joined
    Aug 2007
    Location
    England
    Posts
    389
    Location
    Quote Originally Posted by saban
    In the text there should be no Bold text at all and then this code exits loop if there is
    even one word bolded it will go into loop
    I read this as you saying there should be no bold text, sorry about that.

    I saw this in the help:

    Do [{While | Until} condition]
    [statements]
    [Exit Do]
    [statements]
    Loop


    so I think it should be like:

    [vba]
    Dim rng2 As Range
    Set rng2 = ActiveDocument.Range.Duplicate
    With rng2.Find.Font
    .Bold = False
    .italic = False
    .Hidden = False
    .Underline = False
    .Name = "Times New Roman"
    .Size = 12
    rng2.TextRetrievalMode.IncludeHiddenText = False
    End With

    Do While rng2.Find.Execute
    rng2.Find.Replacement.Style = ActiveDocument.Styles("Normal")
    rng2.Find.Execute Replace:=wdReplaceAll
    With rng2.Font
    .Reset
    .Bold = False
    .italic = False
    .Underline = False
    End With
    Loop

    If rng2.End = ActiveDocument.Range.End Then Exit Do
    rng2.Collapse wdCollapseEnd
    Selection.Collapse direction:=wdCollapseEnd

    If rng2.Characters(1) = Chr(13) & Chr(7) Then rng2.Move wdCharacter, 1 [/vba]

    is that better?
    -Once my PC stopped working, so I kicked it......Then it started working again

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by saban
    Ah nevermind that normal6 I changed this style to normal

    But this is so wierd why does it loop all the time

    [vba]Dim rng2 As Range
    Set rng2 = ActiveDocument.Range.Duplicate
    With rng2.Find.Font
    .Bold = False
    .italic = False
    .Hidden = False
    .Underline = False
    .Name = "Times New Roman"
    .Size = 12
    rng2.TextRetrievalMode.IncludeHiddenText = False
    End With
    Do While rng2.Find.Execute
    rng2.Find.Replacement.Style = ActiveDocument.Styles("Normal")
    rng2.Find.Execute Replace:=wdReplaceAll
    With rng2.Font
    .Reset
    .Bold = False
    .italic = False
    .Underline = False
    End With

    If rng2.End = ActiveDocument.Range.End Then Exit Do
    rng2.Collapse wdCollapseEnd
    Selection.Collapse direction:=wdCollapseEnd
    If rng2.Characters(1) = Chr(13) & Chr(7) Then rng2.Move wdCharacter, 1[/vba]

    even on a normal text without tables
    Any ideas
    Saban,

    You seem to keep on doing variations on the same thing and getting in a tangle. Try not to throw code together quite so quickly - a little more effort up front will pay dividends. Work out what you actually want and then try to code to get it.

    I have no idea why your code loops - I don't have enough information to go on, but it seems unlikely that this code is ever going to be right ...
    [VBA]
    Do While rng2.Find.Execute
    ' ...
    rng2.Find.Execute Replace:=wdReplaceAll
    ' whatever

    [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I have asked previously, but...why are you using .Duplicate?

    Also, I do not understand what it is, exactly, you are trying to do.

    Are you looking for words? If so, then you could use Words.[vba]Sub What()
    Dim aWord
    Dim r As Range
    Set r = ActiveDocument.Range
    For Each aWord In r.Words
    If IsWhatever(aWord) = True Then
    aWord.Style = "Normal"
    End If
    Next
    End Sub

    Function IsWhatever(ByRef aWord As Variant) As Boolean
    If aWord.Font.Bold = False And _
    aWord.Font.Italic = False And _
    aWord.Font.Hidden = False And _
    aWord.Font.Underline = False And _
    aWord.Font.Name = "Times New Roman" And _
    aWord.Font.Size = 12 Then
    IsWhatever = True
    End If
    End Function[/vba]The function checks the attributes. Each word goes through the function, if the word has those attributes, it makes the word Normal - a poor idea, but whatever.

    If you are checking paragraphs, then use a paragraph object.[vba]Sub UsingParas()
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
    If IsWhatever2(oPara) = True Then
    oPara.Style = "Normal"
    End If
    Next
    End Sub

    Function IsWhatever2(oPara As Paragraph) As Boolean
    If oPara.Range.Font.Bold = False And _
    oPara.Range.Font.Italic = False And _
    oPara.Range.Font.Hidden = False And _
    oPara.Range.Font.Underline = False And _
    oPara.Range.Font.Name = "Times New Roman" And _
    oPara.Range.Font.Size = 12 Then
    IsWhatever2 = True
    End If
    End Function[/vba]

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    RE: Tony's comment...nope, it won't.

    I totally agree. It would be far far better to actually work out, precisely, what it is you want to happen. I know I am a nag, and "rip" into people on this.

    However, I kid you not. Very few people do this, but it is a good idea.

    Write out what it is you want to have happen. Not code it. Write it out. Gasp! On paper.

    OK, OK, you can do it electronically.

    But I have found that often it is actually better doing it on paper.

    You can circle stuff you determine is in the wrong place and arrow it up to the right place.

    Work it out step-by-step. That is the way VBA deals with it, so it is a VERY good idea to start thinking that way. Step-by-step.

    Coding is, IMO, the least important part of.....ummm, coding. I mean by that, the writing of code.

    Far more important is the logic behind what you are trying to do. And that, my friends, means thinking about what you are trying to do.

    Getting the code (to do the logic you have figured out) is much much easier when you know EXACTLY what it is, logically, you are trying to do.

    It makes it easier to ask questions, as you can ask specifically: I want to do THIS, how do I do that?

    ALL code, without exception, uses logic. Until we have computers that can read minds and extrapolate that into logic, we have to GIVE the computer the logic. It can not, yet, take desires and translate that into logic.

    Even if they could take desires and translate it into logic, bottom line though is it still needs logic.

    That is where we should start. NOT by just coding. Oh, once we get advanced - say a Tony Jollans - we can jump and and just write code.

    However, he can only do that because he has worked with it long enough that he understands the logic behind it.

    Oh sure, we experiment, we try stuff (coding) and see what happens. The point of doing that, though, is to build up chunks of logic in our minds.

    When those chunks are tested and found consistently viable, then we add them to our library. That library is both within our minds, our concepts, our knowledge, but also (hopefully) in a real file.

  16. #16
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Thnx guy for those replies I sure will look into them carefully


    Just to see what I am actually strugling with
    The problem that I am facing is:

    In documents I work on are full of different styles (but fonts are just bolditalic, italic, bold and normal)

    Those styles cause trouble with certain translation program

    So I thought if I select all fonts that are bolditalic (or italic...) and asign them a common style (Normal) but afterwards format them back to bolditalic, centered or whatever they were formatted before asigning normal style to them.

    So the fonts will look the same after this proces as they were before.
    It is just that instead of style ex. customMystyle now it will be Normal style

    That is prety much all the problem I have

    Thnx again
    Saban

  17. #17
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Gerry,

    The duplicate is my fault - just being over-cautious.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  18. #18
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    To take this a bit further ..

    What is the difference between
    [VBA]Set R = ActiveDocument.Range[/VBA]
    and
    [VBA]Set R = ActiveDocument.Range.Duplicate[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  19. #19
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Duplicate statement gives back another range ??
    It finds some range and them imediatelly looks for another instance of it
    Or what ?

  20. #20
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    fumei why does this part not change style to normal
    [vba] aWord.Style = "Normal"[/vba]

    This code of yours is what I want and it gives the same results as my code(most of it written by Tony) - but my goes into a loop and your seems it dont

    will test it further and let you know

Posting Permissions

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