Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help creating a macro

Status
Not open for further replies.

wooonelly

IS-IT--Management
Feb 23, 2006
19
0
0
US
I have a document that looks like this:

%
O0001
( DIAMETER = 4. )
( CORNER RADIUS = .125 )
( STOCK ALLOWANCE = .03 )
G91 G28 Z0
H0 G49
T1
M6
G90
M3 S2500
G5 P1
G61.1
G0 X8.6251 Y-2.1484
G43 Z-1.4 H1 M50
Z-1.9
G1 Z-1.93 F150.
Y8.0004 Z-1.9831
X2.5551 Z-2.0149........say 10 more PAGES of this.
N200 ( T2 )
( DIAMETER = 1.5 )
( CORNER RADIUS = .125 )
( STOCK ALLOWANCE = .03 )
G91 G28 Z0
H0 G49
T2
M6
G90
M3 S2500
G5 P1
G61.1
X-5.875 Y.1278
G43 Z-1.4 H2 M50
Z-1.9
G1 Z-2.0349.....another 10 pages of this

This repeats many more times. The parenthesis sections are the start of a new toolpath and there could be up to 25 in one document with the document being anywhere from 10-1000 pages long.

I am looking for help creating a macro that would print the first 30 lines, starting with the parenthesis section, then have a couple blank lines, then the last ten lines before the next section. This would be page 1. Then page two would start with the second parenthesis section and 20 lines, # of blank lines, last ten lines to make up page two. i would like this to go on for as many toolpaths that are on the document.

I would like the output to look like this:

( DIAMETER = 4. )
( CORNER RADIUS = .125 )
( STOCK ALLOWANCE = .03 )
G91 G28 Z0
H0 G49
T1
M6
G90
M3 S2500
G5 P1
G61.1
G0 X8.6251 Y-2.1484
G43 Z-1.4 H1 M50.. (I would like the first 30 lines)

(4 carriage returns to represent a break)

X8.6251 Y-.1012 (last ten lines of this section)
Y8.0004
X1.375
G3 X.7405 Y6.4686 R.8973
G2 X1.375 Y4.9369 R2.1663
G0 Z-1.4
X-5.875 Y.1278
G5 P0
M01
-----------(page break here)
N200 ( T2 )
( DIAMETER = 1.5 )
( CORNER RADIUS = .125 )
( STOCK ALLOWANCE = .03 )
G91 G28 Z0
H0 G49
T2
M6
G90
M3 S2500
G5 P1
G61.1
X-5.875 Y.1278(up to 30 lines)

X8.6251 Y-.1012(last ten lines)
Y8.0004
X1.375
G3 X.7405 Y6.4686 R.8973
G2 X1.375 Y4.9369 R2.1663
G0 Z-1.4
X-5.875 Y.1278
G5 P0
M01
-----------(page break here)etc.

I have an excel macro that does this but would like it in word and can't get it to work in word. I asked someone else about this and they said the excel macro is totally different so I figured I wouldn't post it. If anyone would like to see it just ask.

If anyone could help, it would be greatly appreciated.
 
Hi,

Hey, you have something that works in Excel, so what's the business case for MS Word? (BTW, I'd like a Hummer! ;-) )

So what Word code have you written? Please post with an explanation of what doesn't work.

Skip,
[sub]
[glasses] [red]Be Advised![/red] A man who jumps from a bridge in Paris, is…
INSANE! [tongue][/sub]
 
It is not for me. An employee complained because in excel, the macro asks for a file, when you find it, it creates a word doc the way we want it. But it is too much work to have to open the word doc and print it. I think it is stupid but I gotta do it.

The excel code opens a form when you open the specific excel doc. There is a command button to pick a file, you browse for it and when you find it a word doc is created.

Here it is:

Private Sub CommandButton1_Click()
filopn1 = Application.GetOpenFilename("Text Files (*.prg), *.prg*")
If filopn1 = "" Then
MsgBox "Please select a file"
Exit Sub
End If

If UCase(Right(filopn1, 3)) <> "PRG" Then
MsgBox "This app only good for text files"
Exit Sub
End If

TextBox1.Text = filopn1
Call Make_Sections(filopn1)

End Sub
Sub Make_Sections(MyFile As Variant)
Dim OutputFile As String
Dim count As Integer
Dim LineArray() As String
Dim MyLine As String
Dim arrNum As Long

On Error GoTo MyErrorHandler:

ReDim LineArray(10000) 'This redimensions this array...I am assuming there is less than
'10000 lines between tool sets.
OutputFile = Mid$(CStr(MyFile), 1, Len(MyFile) - 3) & "doc"
Open MyFile For Input As #1
Open OutputFile For Output As #2

'This first loop gets you past the header info before the first parentheses (
Line Input #1, MyLine ' Initialize a line
While InStr(1, MyLine, "(") = 0 'Until you find a ( just keep kicking out lines)
Print #2, MyLine
Line Input #1, MyLine
Wend

arrNum = 1
While Not EOF(1)
If InStr(1, MyLine, "(") <> 0 Then 'The first instance may or may not have lines
Print #2, MyLine 'above it...this should be o.k.
For i = 1 To 30 'Print 30 lines after the (
Line Input #1, MyLine
Print #2, MyLine
Next i

For i = 1 To 6
Print #2,
Next i

Line Input #1, MyLine


Do Until InStr(1, MyLine, "(") <> 0 'Until find another (, read lines into array
LineArray(arrNum) = MyLine
arrNum = arrNum + 1
Line Input #1, MyLine
Loop
If InStr(1, MyLine, "(") <> 0 Then
For i = 20 To 1 Step -1
Print #2, LineArray(arrNum - i)
Next i
End If
ReDim LineArray(10000) 'This just empties out the lines in the array
arrNum = 1
End If
Wend

Close #1
Close #2
UserForm1.Hide

MyErrorHandler:
If Err.Number = 62 Then
Close #1
Close #2
UserForm1.Hide
Exit Sub
End If


End Sub

I hope this helps.
 
wooonelly said:
But it is too much work to have to open the word doc and print it
Would it be acceptable to have the code in Excel just print the Word document?

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Try something like:
Code:
Sub test()
Dim oWrd As Object
Set oWrd = CreateObject("Word.Application")
With oWrd
    .Documents.Open ("C:\test.doc")
    .ActiveDocument.PrintOut Background:=False
    .ActiveDocument.Close
    .Quit
End With
Set oWrd = Nothing
End Sub
at the end of your existing Excel code.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top