|
-
Feb 8th, 2005, 09:55 AM
#1
Thread Starter
New Member
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:
Sub StripBillingInfo()
On Error GoTo errorhandler
'set up variables
Dim SectionNumber As Integer
Dim RangeToSpike As Range
Dim AccumulatedText As String
'loop through sections
For SectionNumber = 1 To ActiveDocument.Sections.Count
'mark the first paragraph of the section, less the paragraph mark
Set RangeToSpike = _
ActiveDocument.Sections(SectionNumber).Range.Paragraphs.First.Range
RangeToSpike.MoveEnd wdCharacter, -1
If RangeToSpike.Start = RangeToSpike.End Then GoTo GetNextSection
If Mid(RangeToSpike.Text, 1, 3) <> "%%%" Then GoTo GetNextSection
'add the first paragraph to the spike, then delete it
RangeToSpike.MoveStart wdCharacter, 3
AccumulatedText = AccumulatedText & RangeToSpike.Text & vbCr
ActiveDocument.Sections(SectionNumber).Range.Paragraphs.First.Range.Delete
GetNextSection:
Next SectionNumber
'create a new document and dump the spike into it
Documents.Open FileName:="e:\billing.txt", ConfirmConversions:=False, Format:=wdOpenFormatText
Selection.EndKey wdStory
If Selection.Paragraphs.First.Range.Characters.Count > 1 Then
Selection.InsertParagraph
Selection.EndKey wdStory
End If
Selection.TypeText AccumulatedText
ActiveDocument.Close wdSaveChanges
errorhandler:
End Sub
Any help is much appreciated
-
Feb 8th, 2005, 01:28 PM
#2
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:
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleNormal
Do While .Execute(FindText:="%%%", Forward:=True, Format:=True) = True
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.Select
.Cut
'Paste into new doc ?
End With
Loop
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 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 
-
Feb 8th, 2005, 01:57 PM
#3
Thread Starter
New Member
Re: MS Word Macro Help
Okay thanks RonDogg, how would I paste the paragraph in a text file at this location e:\billing.txt ?
-
Feb 8th, 2005, 02:14 PM
#4
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 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 
-
Feb 8th, 2005, 04:40 PM
#5
Thread Starter
New Member
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:
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleNormal
Do While .Execute(FindText:="%%%", Forward:=True, Format:=True) = True
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.Select
.Cut
'Paste into new doc ?
.Paste FileName:="e:\billing.txt"
End With
Loop
End With
Thanks
-
Feb 8th, 2005, 05:08 PM
#6
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:
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleNormal
Do While .Execute(FindText:="%%%", Forward:=True, Format:=True) = True
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.Select
.Cut
'Paste into new doc ?
Dim oNewDoc As Word.Document
Set oNewDoc = Documents.Add
oNewDoc.Content.Paste
'.Paste FileName:="e:\billing.txt"
oNewDoc.SaveAs "E:\Billing.txt", wdFormatText
oNewDoc.Close False
Set oNewDoc = Nothing
End With
Loop
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 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
|