|
-
Oct 8th, 2009, 08:57 AM
#1
Thread Starter
New Member
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
-
Oct 9th, 2009, 05:24 AM
#2
Thread Starter
New Member
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:
Sub Copy()
'
' Copy Macro
' Macro recorded 08/10/2009 by XXXXXXXXXXX'
Dim searchstrings(10) As String
Dim position1 As Integer
Dim position2 As Integer
''Dim position3 As Integer
Dim enddoc As Boolean
searchstrings(1) = "Payment"
searchstrings(2) = "Beneficiary Details Number"
searchstrings(3) = "Country"
searchstrings(4) = "Name"
searchstrings(5) = "Payment Instructions"
searchstrings(6) = "Payment Details"
searchstrings(7) = "Account Number"
searchstrings(8) = "Bank"
searchstrings(9) = "Bank"
searchstrings(10) = "Payment"
Selection.Move wdCharacter, -Selection.Start
Dim offset As Integer
enddoc = False
While Not enddoc
i = 1
offset = 0
finishstring = " "
While i < 10
Selection.Find.ClearFormatting
With Selection.Find
.Text = searchstrings(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
position1 = Selection.End + 1
If i = 8 Then
Selection.MoveStart wdCharacter, 5
Selection.MoveEnd wdCharacter, 9
If UCase(Trim(Selection.Text)) <> "SWIFT ID" Then
finishstring = finishstring & ","
offset = 1
Selection.MoveStart wdCharacter, Len(Selection.Text)
position1 = position1
With Selection.Find
.Text = searchstrings(i + 2)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
enddoc = Selection.Start = 0
position2 = Selection.End
Selection.MoveEnd wdCharacter, -Len(searchstrings(i + 2))
Selection.MoveStart wdCharacter, -(position2 - position1 - Len(searchstrings(i + 2)))
j = 1
While j <= Len(Selection.Text)
If Mid(Selection.Text, j, 1) = vbCrLf Then
Selection.MoveStart wdCharacter, 1
End If
j = j + 1
Wend
finishstring = finishstring & Replace(Trim(Selection.Text), vbCrLf, " ")
Else
Selection.MoveStart wdCharacter, Len(Selection.Text)
position1 = position1 + 9
With Selection.Find
.Text = searchstrings(i + 1)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
enddoc = Selection.Start = 0
position2 = Selection.End
Selection.MoveEnd wdCharacter, -Len(searchstrings(i + 1))
Selection.MoveStart wdCharacter, -(position2 - position1 - Len(searchstrings(i + 1)))
j = 1
While j <= Len(Selection.Text)
If Mid(Selection.Text, j, 1) = vbCrLf Then
Selection.MoveStart wdCharacter, 1
End If
j = j + 1
Wend
finishstring = finishstring & Replace(Trim(Selection.Text), vbCrLf, " ")
End If
Else
Selection.MoveStart wdCharacter, Len(Selection.Text)
With Selection.Find
.Text = searchstrings(i + 1)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
enddoc = Selection.Start = 0
position2 = Selection.End
Selection.MoveEnd wdCharacter, -Len(searchstrings(i + 1))
Selection.MoveStart wdCharacter, -(position2 - position1 - Len(searchstrings(i + 1)))
j = 1
While j <= Len(Selection.Text)
If Mid(Selection.Text, j, 1) = vbCrLf Then
Selection.MoveStart wdCharacter, 1
End If
j = j + 1
Wend
If i = 1 Then
finishstring = finishstring & Replace(Replace(Trim(Selection.Text), "Accepted", ""), vbCrLf, " ")
Else
finishstring = finishstring & Replace(Trim(Selection.Text), vbCrLf, " ")
End If
End If
If i + offset < 9 Then finishstring = finishstring & ","
i = i + 1 + offset
Wend
MsgBox (Replace(Replace(finishstring, vbCr, " "), vbLf, " "))
Wend
End Sub
Last edited by Hack; Oct 9th, 2009 at 05:48 AM.
Reason: Removed OPs Name
-
Oct 9th, 2009, 06:38 AM
#3
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
-
Oct 9th, 2009, 07:13 AM
#4
Thread Starter
New Member
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?
-
Oct 10th, 2009, 04:03 AM
#5
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
-
Oct 12th, 2009, 03:04 AM
#6
Thread Starter
New Member
Re: Word Macro
cheers West, was hoping to build a single flat file from all the results of the macro though, any recommendations there?
-
Oct 12th, 2009, 03:21 AM
#7
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
-
Oct 12th, 2009, 03:22 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 12th, 2009, 03:33 AM
#9
Thread Starter
New Member
Re: Word Macro
sorry, didn't want to clutter up your boards.
will look into it.
-
Oct 12th, 2009, 03:45 AM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|