Results 1 to 6 of 6

Thread: MS Word Macro Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2005
    Posts
    8

    Question MS Word Macro Help

    Hello everyone,

    All I need is the VB code at the bottom rewritten so that it will strip the red paragraph out of the following example document and place it in a text file:



    Here is the example document:

    John Doe
    [email protected]
    123-4567
    Febuary 8, 2005

    Jane Doe
    12 Evergreen Rd.
    Calgary, AB
    S4S 5K9

    Dear Jane,

    RE: Document example
    Mark: 46547
    Country: Canada
    Serial: 345Abb7
    __________________________________

    %%%Febuary 8, 2005,”345ABB7”,” TDLR”,Opposition,”preparing correspondence to Jane Doe regarding trademark renewal deadline approaching on March 17, 2005, seeking instructions regarding same”,”46547”,””,””

    We note that there is approximately one month remaining within which to effect a timely renewal of the above captioned trademark registration. Should we not file the necessary renewal documents in advance of the March 17, 2005 deadline,


    Yours very truly,

    FURMAN & KALLIO



    John Doe

    JD
    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. 'loop through sections
    8.        For SectionNumber = 1 To ActiveDocument.Sections.Count
    9. 'mark the first paragraph of the section, less the paragraph mark
    10.            Set RangeToSpike = _
    11.            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

    Any help is much appreciated

  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

    I think this logic may be easier to implement. Its just an example, but try playing with it.
    VB Code:
    1. With ActiveDocument.Content.Find
    2.         .ClearFormatting
    3.         .Style = wdStyleNormal
    4.         Do While .Execute(FindText:="%%%", Forward:=True, Format:=True) = True
    5.             With .Parent
    6.                 .StartOf Unit:=wdParagraph, Extend:=wdMove
    7.                 .Select
    8.                 .Cut
    9.                 'Paste into new doc ?
    10.                
    11.             End With
    12.         Loop
    13.     End With
    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

    Smile Re: MS Word Macro Help

    Okay thanks RonDogg, how would I paste the paragraph in a text file at this location e:\billing.txt ?

  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

    You would need to write the contents of the Windows Clipboard to the file.
    Lets search for some code to do that.

    Its only text, correct?

    Ps, who's RonDogg
    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

    Sorry RobDog888,

    Yes it is only text, I just want to cut the red paragraph out of the document and past it in a text file when the macro is executed.

    I've been looking for the code to paste it in a text file, would it look something like this:

    VB Code:
    1. With ActiveDocument.Content.Find
    2.     .ClearFormatting
    3.     .Style = wdStyleNormal
    4.     Do While .Execute(FindText:="%%%", Forward:=True, Format:=True) = True
    5.         With .Parent
    6.             .StartOf Unit:=wdParagraph, Extend:=wdMove
    7.             .Select
    8.             .Cut
    9.             'Paste into new doc ?
    10.             .Paste FileName:="e:\billing.txt"
    11.         End With
    12.     Loop
    13. End With

    Thanks

  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

    Looks like there is now parameters for the .Paste method. Same for the PasteSpecial,
    but not the one we need. This should do the job anyways.

    VB Code:
    1. With ActiveDocument.Content.Find
    2.     .ClearFormatting
    3.     .Style = wdStyleNormal
    4.     Do While .Execute(FindText:="%%%", Forward:=True, Format:=True) = True
    5.         With .Parent
    6.             .StartOf Unit:=wdParagraph, Extend:=wdMove
    7.             .Select
    8.             .Cut
    9.             'Paste into new doc ?
    10.             Dim oNewDoc As Word.Document
    11.             Set oNewDoc = Documents.Add
    12.             oNewDoc.Content.Paste
    13.             '.Paste FileName:="e:\billing.txt"
    14.             oNewDoc.SaveAs "E:\Billing.txt", wdFormatText
    15.             oNewDoc.Close False
    16.             Set oNewDoc = Nothing
    17.         End With
    18.     Loop
    19. End With
    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

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