Results 1 to 14 of 14
  1. #1
    New Lounger
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating macro for select /delete lines (Word 2000)

    Hi - i wondered if anybody could help at all? I am currently working on a project that requires copy/pasting data from a web page daily to Word (via notepad).
    My problem is that there is quite a bit of data within this page at various intervals that is not required and i have to manually delete the specific lines. However there are repetetive fixed points pre and post the surplus data throughout the page that can be used as points of reference for find/select/delete. I am not a macro guru, but if someone knows of any such commands for a macro that will allow me to perform this task automatically, i would be most grateful to them..

    any help would be very much appreciated..

    many thanks in advance

    m

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Creating macro for select /delete lines (Word

    Does this <post:=288,581>post 288,581</post:> help in any way?

    We can't be too specific without knowing the details of the source file and the regions you want to extract.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Creating macro for select /delete lines (Word

    This macro should do what you want.
    Replace the constants for strStart and strEnd with your starting and ending strings.
    If these strings contain any of the characters <code>( [ ] { } < > ( ) - @ ? ! * )</code> then put a in front to show word that you mean the literal character.

    StuartR
    <code>
    Sub RemoveDelimitedText()
    Const txtStart As String = "-START-"
    Const txtEnd As String = "-END-"
    Dim rng As Range
    Set rng = ActiveDocument.Content
    With rng.Find
    .Text = txtStart & "*" & txtEnd
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    End With
    rng.Find.Execute Replace:=wdReplaceAll
    End Sub
    </code>

  4. #4
    New Lounger
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating macro for select /delete lines (Word

    Hi Andrew and StuartR.. thankyou very much for your replies.


    Firstly Andrew i think i am way out of my depth with the code you pointed me towards plus i am not so worried about extracting the data autmatically straight from the web page....but thankyou for your help.

    StuartR - thankyou very much for the code...sorry to be a dummy, but i've tried using the code alone to create a macro but am struggling. I am getting various errors such as indentifier expected in bracket expression / expecting sub end and also it does not recognise my attempts to enter the start and end text for the constants. It is without doubt something i am doing incorrectly so I appologise but i am relatively new to the macro world so please bare with me..What i am trying to do is this..below is a made up example of the text and constants involved

    No.
    text text text text
    text text text
    text text
    text
    NEXT:

    No. = the start constant and NEXT: = the end constant...
    any data in between (which can vary in line numbers) i would like highlighted and deleted and for the macro to repeat this task until all mentioned areas of text for whole page (around 5 or 6 areas) are deleted.

    just to confirm i am selecting the text from the web page manually copying into notepad and then into word..around the houses but it works..its just in word i'd like to auto execute this task if possible

    i hope i am not confusing you any further

    again thanks for your help


    m

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Creating macro for select /delete lines (Word

    Here is a version of Stuart's macro using your information:

    Sub RemoveDelimitedText()
    Const txtStart As String = "No."
    Const txtEnd As String = "NEXT:^13"
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = txtStart & "*" & txtEnd
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

    I have attached a sample document with some text and with the macro. You can run it by selecting Tools | Macro | Macros..., selecting RemoveDelimitedText and clicking Run.
    Attached Files Attached Files

  6. #6
    New Lounger
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating macro for select /delete lines (Word

    That is great thankyou Hans

    appreciate your post....

    your version works fine in your doc..however i would like to keep data on the line where it says NEXT..(there is also a version i'd like to create with the word FORECAST: there instead as well, though trying to insert this word has no affect when testing with the real web page or testing with your macro in my own doc of web page)

    if i could simplify what i'm trying to do in the below example for you it may help?

    No. text to go
    text to go text to go text to go
    text to go text to go
    text to go FORECAST: text to stay text to stay text to stay

    No.
    text to go text to go text to go
    text to go text to go
    text to go FORECAST: text to stay text to stay text to stay

    etc etc..

    sorry if i'm confusing things, but actually got your version of macro rrunning in you doc, but i see it deletes the data on the line with NEXT:..If possible i wish to see the data beyond NEXT r the word FORECAST: (which will be in same location) .......then when the following "No." is found do the same again..

    thanks for your patience..
    many thanks for your help

    m

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Creating macro for select /delete lines (Word

    In your example, should the word FORECAST be deleted with the text before it, or should it remain, together with the text after it?

  8. #8
    New Lounger
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating macro for select /delete lines (Word

    Hi HansV...

    thanks fo your reply - yes the word FORECAST: should go and just leave the line(s) of text beyond the colon..

    ie: No. text can go text can go
    text can go text can go
    text can go text can
    FORECAST: text can stay text can stay

    becomes

    text can stay text can stay

    many thanks
    m

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Creating macro for select /delete lines (Word

    Try changing the line
    <code>
    Const txtEnd As String = "NEXT:^13"
    </code>
    in the macro to
    <code>
    Const txtEnd As String = "FORECAST: "</code>

  10. #10
    New Lounger
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating macro for select /delete lines (Word

    Hans thats brilliant thankyou.. i did try that earlier but didnt remove the "<13" from the the string..
    now that i have that there are other areas i've tried it on on the page and found that it will be alot more effective than i imagined..thanks

    if i do want to add the same code again within the same macro but for a second pass on a second segment of data with a different set of constants could you posibly tell me how i do this plse.
    I have it all running in seperate macros fine but dont know if there should be extra code or not to divide the commands without the de###### complaining, or failing that how i trigger off another macro n code form?

    this is what i have at the moment..

    Sub new1()
    Const txtStart As String = "No."
    Const txtEnd As String = "FORECAST:"
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = txtStart & "*" & txtEnd
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With

    Const txtStart As String = "No."
    Const txtEnd As String = "NEXT:"
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = txtStart & "*" & txtEnd
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With

    End Sub


    individually the code works which is the main thing...thankyou very much

    m

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Creating macro for select /delete lines (Word

    You can't define a constant with the same name twice in a procedure, but you could do it like this:
    <code>
    Sub RemoveSpecificText(txtStart As String, txtEnd As String)
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = txtStart & "*" & txtEnd
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
    End With
    End Sub

    Sub RemoveText()
    RemoveSpecificText "No.", "FORECAST:"
    RemoveSpecificText "No.", "NEXT:"
    End Sub
    </code>
    The macro to run is RemoveText. It calls RemoveSpecificText twice, passing the start text and end text as arguments. You can add more lines as needed.

  12. #12
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Creating macro for select /delete lines (Word

    I nearly wrote the code that way in the first place, as it seemed more elegant, but I thought it might cause confusion.
    Funny world isn't it.

    StuartR

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Creating macro for select /delete lines (Word

    So it goes... <img src=/S/blackteeth.gif border=0 alt=blackteeth width=20 height=20>

  14. #14
    New Lounger
    Join Date
    Feb 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating macro for select /delete lines (Word

    Thanks Hans ..

    i have now tried that and it works a treat.. :-)

    thank you and to you & all for your time and patience and helpful replies


    m

Posting Permissions

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