Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 37 of 37

Thread: Solved: Trying to delete text through VBA...

  1. #21
    Cool! Even easier!

    Thank you!!

    I will now mark it as solved!

    Have a good day everyone!

    Mike

  2. #22
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Glad we could help you Mike!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #23
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I found the issue!
    Sorry, but I think you have missed the point.

    We need a paragraph before and after the 'comment' text.
    No you absolutely do NOT. This is totally incorrect. You can have a Comment style with or without other paragraphs. I specifically mentioned do not confuse "text" with "paragraph". Paragraphs are very very specific to Word.

    Therefore, I cannot end with a comment style in a cell, and expect it to disappear.
    This is not accurate. If the paragraphstyle of the text is Comment – it will disappear. If it is not that style…it will not. So, yes you CAN expect to be able to end with a comment - if it properly uses the style.
    Indeed, I need a hard-return after the comment field, and then it will work!
    A "hard return" is what makes it a paragraph!

    So, what I now did was press Enter after the comment, and then, since I was stuck with a blank line, I simply reduced the font to 1pt.
    If you are using styles properly you NEVER, EVER, EVER have to have blank lines, or "extra" paragraphs. This is the power of styles, the step you took, while it does "work" is completely unneeded.

    Therefore, Word will treat this as it's own paragraph, yet the user will not see an extra blank space.
    Sigh, it does not treat it as its own paragraph…it IS a paragraph.

  4. #24
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Gerry,

    As ussual I mist the conversation but that's a nice explanation indeed!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  5. #25
    Hi again, guys!
    It's the first Sunday of the month again, VBA play time!

    OKay, I was wondering if someone could please add one small piece of code to this one:

    [VBA]Sub DeleteComments()
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting

    .Style = ActiveDocument.Styles("Comment")
    .Execute Replace:=wdReplaceAll, Format:=True
    End With
    End Sub [/VBA]


    One of my team members noticed that if a form has no Comment styled paragraphs in it, it will crash! (Of course.. the style isn't found!)

    So, how does one write in VBA, do this "If exists "Comment" with the doc." And if not, just skip the whole business.

    Of course, a workaround I have been using for the past week is to just shove one character of Comment style into my documents, but that is silly!

    So, if someone can help, that would be great!!

    Thanks!

    Mike

  6. #26
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    One way could be to loop through all styles and compare whether the one you want is actually there, but I think simply using On Error might be more efficient. And if you don't want to have this mixed up in your code, you could put that into a function:
    [VBA]
    Function StyleExists(StyleName As String) As Boolean
    Dim Temp As String
    On Error GoTo err
    Temp = ActiveDocument.Styles(StyleName).NameLocal
    StyleExists = True
    Exit Function
    err:
    StyleExists = False
    End Function
    [/VBA]

    Daniel

  7. #27
    Thanks Daniel!
    It sounds like a plan.
    Again, my ignorance shows.

    Where exactly do I put this?

    My routine to kill all commented text sits in the middle of a 6-stage macro.

    Everytime I slip in your code, I get an anexpected end of Sub error.

    Here you will see four parts of my macro, being the following:

    1 - Delete all text before the ReportStart bookmark
    2 - Set the zoom to 100%
    3 - Delete all the commented text
    4 - Reprotect the document with a password.

    *There are other steps before and after. The beginning starts with a Sub BeginReview() and ends with an End Sub.

    So, where exactly do I insert your error-trapping code?

    Thanks!

    Mike

    [VBA] ' Look for the ReportStart bookmark, and delete all all the pages before it
    Selection.GoTo What:=wdGoToBookmark, Name:="ReportStart"
    With ActiveDocument.Bookmarks
    .DefaultSorting = wdSortByName
    .ShowHidden = False
    End With
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1

    'Set the zoom to 100%
    ActiveWindow.ActivePane.View.Zoom.Percentage = 100


    ' Delete all Commented text
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting

    .Style = ActiveDocument.Styles("Comment")
    .Execute Replace:=wdReplaceAll, Format:=True
    End With

    ' ReProtect the document.
    If ActiveDocument.ProtectionType = wdNoProtection Then
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
    NoReset:=True, Password:="1"
    End If[/VBA]

  8. #28
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Like Daniel metioned the fasted way is is to use On error Resume Next in this case.

    In your code that would be:[vba]
    ' Look for the ReportStart bookmark, and delete all all the pages before it
    Selection.GoTo What:=wdGoToBookmark, Name:="ReportStart"
    With ActiveDocument.Bookmarks
    .DefaultSorting = wdSortByName
    .ShowHidden = False
    End With
    Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1

    'Set the zoom to 100%
    ActiveWindow.ActivePane.View.Zoom.Percentage = 100

    On Error Resume Next 'start here
    ' Delete all Commented text
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting

    .Style = ActiveDocument.Styles("Comment")
    .Execute Replace:=wdReplaceAll, Format:=True
    End With
    If Err <> 0 Then Err.Clear 'clear error if needed

    ' ReProtect the document.
    If ActiveDocument.ProtectionType = wdNoProtection Then
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
    NoReset:=True, Password:="1"
    End If
    [/vba]

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  9. #29
    This is great!

    It works!

    I also just took those two lines:

    On Error Resume Next
    and
    If Err <> 0 Then Err.Clear

    ...and I used it for the other part of my macro, where it searches for the "ReportStart" bookmark... Now, when the ReportStart Bookmark is gone, it no longer crashes!

    Very cool!

    Thanks!

    Mike

  10. #30
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Glad we could help you Mike!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  11. #31
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Good to hear it works for you. My final idea, however, was slightly different. I would not use the On error right inside the existing code as there might be other (unexpected) errors that could occur, but put the function in a module and simply call it to check whether a style exists or not (seems a bit tidier to me, but that's just my personal opinion):

    [VBA] Option Explicit

    Sub DeleteComments()
    If StyleExists("Comment") Then
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting

    .Style = ActiveDocument.Styles("Comment")
    .Execute Replace:=wdReplaceAll, Format:=True
    End With
    End If
    End Sub



    Function StyleExists(StyleName As String) As Boolean
    Dim Temp As String
    On Error GoTo err
    Temp = ActiveDocument.Styles(StyleName).NameLocal
    StyleExists = True
    Exit Function
    err:
    StyleExists = False
    End Function

    [/VBA]

    Daniel

  12. #32
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Daniel is absolutely right in his approach. It is much better to use the function. From a design point of view, while there is a use for On Error Resume Next in some cases, it does of course resume after ALL errors. Since we are programming VBA.......it is better to use a function for the specific task of checking for the style, since THAT is what is needed.

  13. #33
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fumei
    Daniel is absolutely right in his approach. It is much better to use the function. From a design point of view, while there is a use for On Error Resume Next in some cases, it does of course resume after ALL errors. Since we are programming VBA.......it is better to use a function for the specific task of checking for the style, since THAT is what is needed.
    Indeed daniel's approach is much cleaner and you will never here me say otherwise! (Nice code D)

    But: The on Error Resume Next is the fasted method in this case and the function is the clean method. (We've had a nice discussion in the past where I was on the other side of the fence)

    Other then that it's very easy to add a general error handler to cope with the remaining unexpected errors in the sub.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  14. #34
    Very interesting.

    Yes, as a former amateur programmer (COBOL/74 & Pascal!), I can understand where the original On Error was a "quick" way out!

    This is great stuff.

    It's working well now!

    Thank you again,

    Mike

  15. #35
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Michael 514
    Very interesting.

    Yes, as a former amateur programmer (COBOL/74 & Pascal!), I can understand where the original On Error was a "quick" way out!
    Indeed depending on the execution speed of your code you can use tools like that to error over steps. (But you always have to know what you're doing...writing a function like Daniel...it shows that he knows what is happening)

    Good luck!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  16. #36
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    thanks Joost - I am trying to pretend that.

    Daniel

  17. #37
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Steiner
    thanks Joost - I am trying to pretend that.

    Daniel
    No you are not my friendly neighbour!

    Later buddy.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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