MS Word Macro with Loop

T

TriEssent

Hello VBA World,
I am trying to reformat the code below to a CSV record format for importing
into MS Access. I am able to clean the file up (180 pages) and remove the
uninteresting characters. I am able to put a comma before the port numbers
but then I need to put the port numbers (and their description) up on the
same line as the IP address/host name (remove the carriage return). The last
port number of course should have a carriage return after it. Help is
appreciated.

* + 160.32.1.10 mvsgw.santeecooper.com
|___ 1025 network blackjack
* + 160.32.3.254 ncc.santeecooper.com
|___ 135 DCE endpoint resolution
|___ 139 NETBIOS Session Service
|___ 445 Microsoft-DS
|___ 1025 network blackjack
|___ 5168 TrendMicroNormalServer
* + 160.32.4.244 h48mv31.santeecooper.com
|___ 135 DCE endpoint resolution
|___ 445 Microsoft-DS
|___ 2967 Symantec Antivirus
* + 160.32.4.245 cw2k.santeecooper.com
|___ 135 DCE endpoint resolution
|___ 139 NETBIOS Session Service
|___ 445 Microsoft-DS
|___ 1025 network blackjack
|___ 2967 Symantec Antivirus
* + 160.32.4.246 certman.santeecooper.com
|___ 135 DCE endpoint resolution
|___ 139 NETBIOS Session Service
|___ 445 Microsoft-DS
|___ 1025 network blackjack
 
J

Jonathan West

Can you show us how the same data should look after it has been processed?
That will make it much easier to understand what you are trying to achieve.

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
T

TriEssent

Here it is Jonathan, Thanks for your response.

144.32.1.10 mvsgw.MyCompany.com,1025 network blackjack
144.32.3.254 ncc.MyCompany.com,135 DCE endpoint resolution,139 NETBIOS
Session Service,445 Microsoft-DS,1025 network blackjack,5168
TrendMicroNormalServer
144.32.4.244 h48mv31.MyCompany.com,135 DCE endpoint resolution,445
Microsoft-DS,2967 Symantec Antivirus
144.32.4.245 cw2k.MyCompany.com,135 DCE endpoint resolution,139 NETBIOS
Session Service,445 Microsoft-DS,1025 network blackjack,2967 Symantec
Antivirus
144.32.4.246 certman.MyCompany.com,135 DCE endpoint resolution,139
NETBIOS Session Service,445 Microsoft-DS,1025 network blackjack
 
T

TriEssent

This is what I came up with and it works. How do I put the cursor at the top
of the document to insert a header file. For example:

IP-HOST, Port1, Port2, Port3, Port4, Port5

I want to put the line above at the top of my document.

Thanks

Dim Counter
Counter = 0

Do While Counter < 2
Counter = Counter + 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "* + "
.Replacement.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t|___ "
.Replacement.Text = ","
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", 135"
.Replacement.Text = ",135"
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", 139"
.Replacement.Text = ",139"
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", 445"
.Replacement.Text = ",445"
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll

Loop

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "160"
.Replacement.Text = "^p" & "160"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
C

Cindy M.

Hi =?Utf-8?B?VHJpRXNzZW50?=,
This is what I came up with and it works. How do I put the cursor at the top
of the document to insert a header file. For example:

IP-HOST, Port1, Port2, Port3, Port4, Port5
There's more than one way to do it, but here's how I'd approach it:

Dim rng as Word.Range

Set rng = ActiveDocument.Content
rng.Collapse wdCollapseStart

rng.Text = "IP-HOST, Port1, Port2, Port3, Port4, Port5"

This inserts the text without moving the insertion point in the document, which
is generally faster and you get less screen flicker.

Another way to go about it is what you'd get if you record pressing Ctrl+Home in
a macro:

Selection.HomeKey wdStory
I want to put the line above at the top of my document.

Thanks

Dim Counter
Counter = 0

Do While Counter < 2
Counter = Counter + 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "* + "
.Replacement.Text = ""
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t|___ "
.Replacement.Text = ","
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", 135"
.Replacement.Text = ",135"
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", 139"
.Replacement.Text = ",139"
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ", 445"
.Replacement.Text = ",445"
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
End With
Selection.Find.Execute
Selection.Find.Execute Replace:=wdReplaceAll

Loop

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "160"
.Replacement.Text = "^p" & "160"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top