Results 1 to 7 of 7

Thread: MS Word Macro Help (VB Code)

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    8

    Question MS Word Macro Help (VB Code)

    Hello all,

    I am working on this vb word macro that strips a billing code(paragraph) from the top of a document and places it in a text file.

    Here is an example of what the billing code looks like:

    %%%«Today:06/03/90»,”«Matter ID»”,” TDLR”,«Matter type»,”preparing correspondence to «Addressee full name» regarding trademark renewal deadline approaching on «Parent event date:June 3, 199011», seeking instructions regarding same”,”«Mark:LIKE THIS»”,””,””

    The problem is that the billing code is now going to be moved to the bottom of the document. The current macro is not coded to search through the whole document for the billing code, it just looks at the first paragraph.

    Can someone help me adjust the following code so that it will search the entire document for the billing code instead of just the first paragraph.

    Here is the vb code currently being used:

    VB Code:
    1. Sub StripBillingInfo()
    2.     On Error GoTo errorhandler
    3.     'set up variables
    4.     Dim SectionNumber As Integer
    5.     Dim RangeToSpike As Range
    6.     Dim AccumulatedText As String
    7.    
    8.     'loop through sections
    9.     For SectionNumber = 1 To ActiveDocument.Sections.Count
    10.         'mark the first paragraph of the section, less the paragraph mark
    11.         Set RangeToSpike = ActiveDocument.Sections(SectionNumber).Range.Paragraphs.First.Range
    12.         RangeToSpike.MoveEnd wdCharacter, -1
    13.         If RangeToSpike.Start = RangeToSpike.End Then GoTo GetNextSection
    14.         If Mid(RangeToSpike.Text, 1, 3) <> "%%%" Then GoTo GetNextSection
    15.         'add the first paragraph to the spike, then delete it
    16.         RangeToSpike.MoveStart wdCharacter, 3
    17.         AccumulatedText = AccumulatedText & RangeToSpike.Text & vbCr
    18.         ActiveDocument.Sections(SectionNumber).Range.Paragraphs.First.Range.Delete
    19. GetNextSection:
    20.     Next SectionNumber
    21.     'create a new document and dump the spike into it
    22.     Documents.Open FileName:="e:\billing.txt", ConfirmConversions:=False, Format:=wdOpenFormatText
    23.     Selection.EndKey wdStory
    24.     If Selection.Paragraphs.First.Range.Characters.Count > 1 Then
    25.         Selection.InsertParagraph
    26.         Selection.EndKey wdStory
    27.     End If
    28.     Selection.TypeText AccumulatedText
    29.     ActiveDocument.Close wdSaveChanges
    30. errorhandler:
    31. End Sub
    Last edited by RobDog888; Feb 3rd, 2005 at 02:56 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Word Macro Help (VB Code)

    Welcome to the Forums downtown636.

    I have edited your post so the code will be easier to read and indented it too.
    Use the [vbcode]insert your code here.[/vbcode] code tags.
    This makes it easier for member to read and increases the chances of getting
    a response.

    So your needing it to go to the next paragraph(s) and search in there too?

    VB Code:
    1. Set RangeToSpike = ActiveDocument.Sections(SectionNumber).Range.Paragraphs.[b]First[/b].Range
    This is where you are getting the first paragraph, so if you change it to the
    paragraphs collection Items and iterate through those, that should do it for
    you.

    VB Code:
    1. Set RangeToSpike = ActiveDocument.Sections(SectionNumber).Range.Paragraphs(1).Range
    Change the "1" to a variable the will count the number of para's inthe section.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    8

    Question Re: MS Word Macro Help (VB Code)

    Thank you for your help so far RobDog888!!

    I have set up a variable to count the number of paragraph's in the section, how would I create the loop to go through all the paragraphs? (Sorry I'm not very familiar with vb code)

    Thanks

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Word Macro Help (VB Code)

    No prob. To set up your variable, declare it first. Then set it equal to the .Count
    property of the paragraphs collection.

    VB Code:
    1. Dim iKount As Integer
    2. Dim i As Integer
    3. '...Blah
    4. '...Blah
    5. '...Blah
    6. iKount = ActiveDocument.Sections(SectionNumber).Range.Paragraphs.Count
    7. 'Then set up a loop
    8. For i = 1 to iKount
    9. '...Blah
    10. '...Blah
    11. '...Blah
    12. Set RangeToSpike = ActiveDocument.Sections(SectionNumber).Range.Paragraphs(i).Range
    13. '...Blah
    14. '...Blah
    15. '...Blah
    16. Next
    17. '...Blah
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    8

    Question Re: MS Word Macro Help (VB Code)

    Okay, the billing code(paragraph) is at the bottom of the document and the macro still doesn't seem to want to strip it out. Here is what the code looks like now:

    VB Code:
    1. Sub BillingCodeTest()
    2. On Error GoTo errorhandler
    3. 'set up variables
    4. Dim SectionNumber As Integer
    5. Dim RangeToSpike As Range
    6. Dim AccumulatedText As String
    7. Dim iKount As Integer
    8. Dim i As Integer
    9.  
    10. iKount = ActiveDocument.Sections(SectionNumber).Range.Paragraphs.Count
    11. 'loop through sections
    12. For i = 1 To iKount
    13. 'mark the first paragraph of the section, less the paragraph mark
    14. Set RangeToSpike = _
    15. ActiveDocument.Sections(SectionNumber).Range.Paragraphs(i).Range
    16. RangeToSpike.MoveEnd wdCharacter, -1
    17. If RangeToSpike.Start = RangeToSpike.End Then GoTo GetNextSection
    18. If Mid(RangeToSpike.Text, 1, 3) <> "%%%" Then GoTo GetNextSection
    19. 'add the first paragraph to the spike, then delete it
    20. RangeToSpike.MoveStart wdCharacter, 3
    21. AccumulatedText = AccumulatedText & RangeToSpike.Text & vbCr
    22. ActiveDocument.Sections(SectionNumber).Range.Paragraphs.First.Range.Delete
    23. GetNextSection:
    24. Next i
    25. 'create a new document and dump the spike into it
    26. Documents.Open FileName:="e:\billingTEST.txt", ConfirmConversions:=False, Format:=wdOpenFormatText
    27. Selection.EndKey wdStory
    28. If Selection.Paragraphs.First.Range.Characters.Count > 1 Then
    29. Selection.InsertParagraph
    30. Selection.EndKey wdStory
    31. End If
    32. Selection.TypeText AccumulatedText
    33. ActiveDocument.Close wdSaveChanges
    34. errorhandler:
    35. End Sub

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Word Macro Help (VB Code)

    I'm doing my best to try to get it how you need it but without actual data its
    a little hard. So this is what I got. It will loop through each paragraph in each
    section but I'm not sure how you need each range added and deleted when
    found. So I am hoping that this will get you going in the right direction.

    VB Code:
    1. Sub BillingCodeTest()
    2.     On Error GoTo errorhandler
    3.     'set up variables
    4.     Dim SectionNumber As Integer
    5.     Dim RangeToSpike As Range
    6.     Dim AccumulatedText As String
    7.     Dim iKount As Integer
    8.     Dim i As Integer
    9.  
    10.     'loop through sections
    11.     For SectionNumber = 1 To ActiveDocument.Sections.Count
    12.         'mark the first paragraph of the section, less the paragraph mark
    13.         iKount = ActiveDocument.Sections(SectionNumber).Range.Paragraphs.Count
    14.         For i = 1 To iKount
    15.             Set RangeToSpike = ActiveDocument.Sections(SectionNumber).Range.Paragraphs(i).Range
    16.             RangeToSpike.MoveEnd wdCharacter, -1
    17.             If RangeToSpike.Start = RangeToSpike.End Then GoTo GetNextSection
    18.             If Mid(RangeToSpike.Text, 1, 3) <> "%%%" Then GoTo GetNextSection
    19.             'add the first paragraph to the spike, then delete it
    20.             RangeToSpike.MoveStart wdCharacter, 3
    21.             AccumulatedText = AccumulatedText & RangeToSpike.Text & vbCr
    22.             ActiveDocument.Sections(SectionNumber).Range.Paragraphs(i).Range.Delete
    23.         Next i
    24. GetNextSection:
    25.     Next SectionNumber
    26.     'create a new document and dump the spike into it
    27.     Documents.Open FileName:="e:\billing.txt", ConfirmConversions:=False, Format:=wdOpenFormatText
    28.     Selection.EndKey wdStory
    29.     If Selection.Paragraphs.First.Range.Characters.Count > 1 Then
    30.         Selection.InsertParagraph
    31.         Selection.EndKey wdStory
    32.     End If
    33.     Selection.TypeText AccumulatedText
    34.     ActiveDocument.Close wdSaveChanges
    35. errorhandler:
    36. End Sub
    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    8

    Question Re: MS Word Macro Help (VB Code)

    Hello RonDogg888,

    Thank you for your help so far, the code you supplied will strip the billing code(parapgraph) from the very top of the document, but if it is placed anywhere else in the document I can't seem to make it work.

    Here is an example of a word document used with the billing code that has to be stripped and placed in a txt file in the bottom in red:


    «PROFESSIONAL NAME»
    «Professional email»
    «Professional telephone»
    «Today:June 3, 1990»

    «Addressee Block»

    Dear «Associate Associate Salutation»:

    RE: «Matter description»
    Our file: «Matter ID»
    «IF ANSWERED(Matter client reference) AND Matter client reference != " "»«Your file:Matter client reference»
    «END IF»

    Enclosed herewith please find an Assignment in respect of the above captioned patent. Please attend to filing this Assignment in the United States Patent and Trademark Office and reporting the filing to us in due course. Thereafter, we will await receipt of the assignment recordal information from you.

    We remain,


    Yours very truly,

    FURMAN & KALLIO



    «Professional name»

    «Compute Initials Line»

    «Doc CCs»

    %%%«Today:06/03/90»,”«Matter ID»”,”PFA4”,«Matter type»,”Attendance on file regarding preparation of correspondence to associate with Assignment for recordal in the United States Patent & Trademark Office, reporting same to client”,”«Patent Title:LIKE THIS»”,””,””

Posting Permissions

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



Click Here to Expand Forum to Full Width