Results 1 to 10 of 10

Thread: Word Macro

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    8

    Word Macro

    Hello, I have never even come accross word macros before so would
    really appreciate some help please??

    I have a Word Document filled with payment information. I want to
    extract all the information marked with an "X" using the header
    information as markers, assuming this is the best way? The "X"
    information can be varying in length of characters in any of the fields.

    I guess all I need is a macro to use on one set of information that
    can they be applied to all the sets within the doc.

    here is an example.....


    Payment XXXX Accepted Beneficiary Details Number XXXX
    Country XXXXXXXXX
    Name XXXXX XXXXXXXX XXX XXXXX
    XX XXXXX XX
    XXXXX XX XXXXXX XXX XXXXX XXXXX
    Payment Instructions ADVISE & CREDIT
    Payment Details XXXXXXXX XX XXXX XXX XXXXXXXXXXX
    Account Number XXXXXXXXX
    Bank SWIFT ID XXXXXXXX
    Bank XXXX XXXX XXX XXXXXX
    XXX XXXXX XXXXXX XXX XXXX XX XXXXX XXXXXX XXXXXX

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    8

    Re: Word Macro

    Great success!!

    I have managed to create the following, albeit a bit crude but does the job.....

    Next problem is, each time the macro fires I want it to capture the results and if possible build a csv containing all the results from each firing.... is that possible???
    vb Code:
    1. Sub Copy()
    2. '
    3. ' Copy Macro
    4. ' Macro recorded 08/10/2009 by XXXXXXXXXXX'
    5.  
    6. Dim searchstrings(10) As String
    7.  
    8. Dim position1 As Integer
    9. Dim position2 As Integer
    10. ''Dim position3 As Integer
    11.  
    12. Dim enddoc As Boolean
    13.  
    14. searchstrings(1) = "Payment"
    15.  
    16. searchstrings(2) = "Beneficiary Details Number"
    17.  
    18. searchstrings(3) = "Country"
    19.  
    20. searchstrings(4) = "Name"
    21.  
    22. searchstrings(5) = "Payment Instructions"
    23.  
    24. searchstrings(6) = "Payment Details"
    25.  
    26. searchstrings(7) = "Account Number"
    27.  
    28. searchstrings(8) = "Bank"
    29.  
    30. searchstrings(9) = "Bank"
    31.  
    32. searchstrings(10) = "Payment"
    33.  
    34. Selection.Move wdCharacter, -Selection.Start
    35.  
    36. Dim offset As Integer
    37.  
    38. enddoc = False
    39.  
    40. While Not enddoc
    41.     i = 1
    42.     offset = 0
    43.     finishstring = " "
    44.     While i < 10
    45.         Selection.Find.ClearFormatting
    46.         With Selection.Find
    47.             .Text = searchstrings(i)
    48.             .Replacement.Text = ""
    49.             .Forward = True
    50.             .Wrap = wdFindContinue
    51.             .Format = False
    52.             .MatchCase = False
    53.             .MatchWholeWord = False
    54.             .MatchWildcards = False
    55.             .MatchSoundsLike = False
    56.             .MatchAllWordForms = False
    57.         End With
    58.         Selection.Find.Execute
    59.         position1 = Selection.End + 1
    60.         If i = 8 Then
    61.             Selection.MoveStart wdCharacter, 5
    62.             Selection.MoveEnd wdCharacter, 9
    63.            
    64.             If UCase(Trim(Selection.Text)) <> "SWIFT ID" Then
    65.                 finishstring = finishstring & ","
    66.                 offset = 1
    67.                 Selection.MoveStart wdCharacter, Len(Selection.Text)
    68.                 position1 = position1
    69.                 With Selection.Find
    70.                     .Text = searchstrings(i + 2)
    71.                     .Replacement.Text = ""
    72.                     .Forward = True
    73.                     .Wrap = wdFindContinue
    74.                     .Format = False
    75.                     .MatchCase = False
    76.                     .MatchWholeWord = False
    77.                     .MatchWildcards = False
    78.                     .MatchSoundsLike = False
    79.                     .MatchAllWordForms = False
    80.                 End With
    81.                 Selection.Find.Execute
    82.                 enddoc = Selection.Start = 0
    83.                 position2 = Selection.End
    84.                 Selection.MoveEnd wdCharacter, -Len(searchstrings(i + 2))
    85.                 Selection.MoveStart wdCharacter, -(position2 - position1 - Len(searchstrings(i + 2)))
    86.                 j = 1
    87.                 While j <= Len(Selection.Text)
    88.                     If Mid(Selection.Text, j, 1) = vbCrLf Then
    89.                         Selection.MoveStart wdCharacter, 1
    90.                     End If
    91.                     j = j + 1
    92.                 Wend
    93.                 finishstring = finishstring & Replace(Trim(Selection.Text), vbCrLf, " ")
    94.             Else
    95.                 Selection.MoveStart wdCharacter, Len(Selection.Text)
    96.                 position1 = position1 + 9
    97.                 With Selection.Find
    98.                     .Text = searchstrings(i + 1)
    99.                     .Replacement.Text = ""
    100.                     .Forward = True
    101.                     .Wrap = wdFindContinue
    102.                     .Format = False
    103.                     .MatchCase = False
    104.                     .MatchWholeWord = False
    105.                     .MatchWildcards = False
    106.                     .MatchSoundsLike = False
    107.                     .MatchAllWordForms = False
    108.                 End With
    109.                 Selection.Find.Execute
    110.                 enddoc = Selection.Start = 0
    111.                 position2 = Selection.End
    112.                 Selection.MoveEnd wdCharacter, -Len(searchstrings(i + 1))
    113.                 Selection.MoveStart wdCharacter, -(position2 - position1 - Len(searchstrings(i + 1)))
    114.                 j = 1
    115.                 While j <= Len(Selection.Text)
    116.                     If Mid(Selection.Text, j, 1) = vbCrLf Then
    117.                         Selection.MoveStart wdCharacter, 1
    118.                     End If
    119.                     j = j + 1
    120.                 Wend
    121.                 finishstring = finishstring & Replace(Trim(Selection.Text), vbCrLf, " ")
    122.             End If
    123.         Else
    124.              Selection.MoveStart wdCharacter, Len(Selection.Text)
    125.                 With Selection.Find
    126.                     .Text = searchstrings(i + 1)
    127.                     .Replacement.Text = ""
    128.                     .Forward = True
    129.                     .Wrap = wdFindContinue
    130.                     .Format = False
    131.                     .MatchCase = False
    132.                     .MatchWholeWord = False
    133.                     .MatchWildcards = False
    134.                     .MatchSoundsLike = False
    135.                     .MatchAllWordForms = False
    136.                 End With
    137.                 Selection.Find.Execute
    138.                 enddoc = Selection.Start = 0
    139.                 position2 = Selection.End
    140.                 Selection.MoveEnd wdCharacter, -Len(searchstrings(i + 1))
    141.                 Selection.MoveStart wdCharacter, -(position2 - position1 - Len(searchstrings(i + 1)))
    142.                 j = 1
    143.                 While j <= Len(Selection.Text)
    144.                     If Mid(Selection.Text, j, 1) = vbCrLf Then
    145.                         Selection.MoveStart wdCharacter, 1
    146.                     End If
    147.                     j = j + 1
    148.                 Wend
    149.                 If i = 1 Then
    150.                     finishstring = finishstring & Replace(Replace(Trim(Selection.Text), "Accepted", ""), vbCrLf, " ")
    151.                 Else
    152.                     finishstring = finishstring & Replace(Trim(Selection.Text), vbCrLf, " ")
    153.                 End If
    154.         End If
    155.         If i + offset < 9 Then finishstring = finishstring & ","
    156.         i = i + 1 + offset
    157.     Wend
    158.     MsgBox (Replace(Replace(finishstring, vbCr, " "), vbLf, " "))
    159. Wend
    160.  
    161. End Sub
    Last edited by Hack; Oct 9th, 2009 at 05:48 AM. Reason: Removed OPs Name

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Word Macro

    No this is not what I suggested...

    Did you go thru my last post...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    8

    Re: Word Macro

    sorry, but it works!

    any idea how I can extract it's results and store them somewhere else? either another word or excel file?

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word Macro

    possibly the simplest is to saveAs each time to some generated filename, using one of the variables or datetime value, so each filename will be unique
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    8

    Re: Word Macro

    cheers West, was hoping to build a single flat file from all the results of the macro though, any recommendations there?

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word Macro

    you can append to a text file if that is what you want to do
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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

    Re: Word Macro

    Usually its best to creat one thread for a single topic. It helps when others are searching etc.
    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

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    8

    Re: Word Macro

    sorry, didn't want to clutter up your boards.

    will look into it.

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

    Re: Word Macro

    No please post up all your questions but for the ones that are spearate topics then please create separate threads
    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