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!

Converting Excel macro to word macro

Status
Not open for further replies.

wooonelly

IS-IT--Management
Feb 23, 2006
19
0
0
US
Here is my code:

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 MyLine As String

OutputFile = Mid$(CStr(MyFile), 1, Len(MyFile) - 3) & "doc"
Open MyFile For Input As #1
Open OutputFile For Output As #2

While Not EOF(1)
Line Input #1, MyLine
If InStr(1, MyLine, "(" - 10) <> 0 Then
Print #2, MyLine
For i = 1 To 56
Line Input #1, MyLine
Print #2, MyLine
Next i
End If
Wend

Close #1
Close #2
UserForm1.Hide

End Sub

This starts automatically when I start a certain excel file, has an "open file" button from which I select a document, it creates a word doc. in the format I want it. The original document, which varies, is upwards of a few hundred pages long. This code shortens it to a few pages with certain info. that is needed.

I would like to put this in Word and use it as a macro for the current open document but can't get it to work. Could someone please help me.
 
You need to spec it out in detail. You state you want to use on the current open document. The current open document is: ActiveDocument - so that is what you will be working with.
This code shortens it to a few pages with certain info.
"Certain info" has to be made, uh, somewhat more explicit.

You state it creates a word doc in the format you want. Does that mean, while in the current word doc, you want to create a new one? I see nothing to define any format. What format do you want?

Gerry
 
I want to create a reference document from an existing document. The existing document starts 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
X2.4967 Y7.9985 Z-2.0152
X2.4362 Y7.9925 Z-2.0155
X2.3737 Y7.9819 Z-2.0158
X2.3098 Y7.9663 Z-2.0162
X2.245 Y7.9451 Z-2.0165
X2.1798 Y7.9182 Z-2.0169
X2.115 Y7.8851 Z-2.0173
X2.0513 Y7.8457 Z-2.0177
X1.9897 Y7.7999 Z-2.0181........................

It is a tooling path that goes on for many pages. The part with the "(" is the start of a toolpath. There could be 20 toolpaths making the document hundreds of pages. This code takes the program, which is a .prg file(text), and keeps the first twenty lines of each toolpath and the last 10 lines. This fills up one page. Then page two starts with another toolpath, keeps the first 20 lines and last ten, and so on for as many toolpaths that there are. It works fine in excel, which was already written by a former employee, but I would like it to work in Word as a macro.

I don't need a new document necessarily, I just want to print the existing doc with the lines I don't need removed.

I hope this helps.
 
Sorry the first code is a previous version. This is the one I am using now.

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
 
1. The recent code is essentially the same as the previous code you posted - so I don't know why you posted it.

2. I have no idea what a toolpath is, or does, or if it is even relevant. Nor do I know what a toolset is, or does, or if it is even relevant.

3. You have mutually exclusive statements. The first is:
I want to create a reference document from an existing document
The second is:
I just want to print the existing doc with the lines I don't need removed.
So.....which is it? Create a reference document, or print the existing doc with lines removed.

I will repeat myself. You have to spec this out. Your code has LineInput, Print (etc etc)...that is fine if you are outputing a text file...but I am - still - unclear if what you want is to output a text file (then use Print), have a Word document (which it seemed you wanted), or if you want to print something.

Please do not write further code that - at this point - is not relevant...oh, and please use TGML for posting code. Tell me, in pseudo code what it is you want. State the logic of what you want to do. If sounds like what you want to do is something like:

1. Search for a string starting with "( "

as in:
O0001
( DIAMETER = 4. )
( CORNER RADIUS = .125 )
( STOCK ALLOWANCE = .03 )
G91 G28 Z0
H0 G49
T1
......

2. Mark that location.

3. Search for the next string starting with "( " and mark THAT location.

4. Extract the first 20 lines - and is it 20 lines, or 20 paragraphs?; AND the last 10 lines between those locations.

5. Repeat through the document.

Then.....what? Print it? Create a new document...although you state that is not what you want...I am not really sure.

Again, please spec it out.

Gerry
 
I guess what I am also trying to spec is that your subject line may be misleading. You are not converting an Excel macro. You have a tesk you need to do, and you want to do it in Word. Which is fine.

Gerry
 
Your macro creates regular text file with 'doc' extension using standard VBA library, except of usage of excel 'open' dialog to pick the source file. To make your code work in word in the same way, you need to change excel part of code:
filopn1 = Application.GetOpenFilename("Text Files (*.prg), *.prg*")
by word vba method (or, if you work with office xp+, use common office FileDialog object):

1. with word library (basic structure):
With Application.Dialogs(wdDialogFileOpen)
.Name = "*.xls"
If .Display = -1 Then
fopn1 = .Name ' assigns name only
MsgBox fopn1 & vbCrLf & CurDir
Else
MsgBox "Please select a file"
Exit Sub
End If
End With

2. with office library (office xp+):
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Text Files", "*.prg"
If .Show = -1 Then
fopn1 = .SelectedItems(1) ' assigns full path & name
MsgBox fopn1
Else
MsgBox "Please select a file"
Exit Sub
End If
End With

combo
 
Just so you know, I didn't write the code and it is in excel right now. I would just like it in word. I was hoping there was just a few different changes to make it work in word. And you are right that I want the first 20 lines and last 10 lines and repeated through the document. Sorry for confusing you about the reference/print thing. It would just be easier, for me, to print the document without the lines I don't need. I don't need a new document on the computer, Just need a hard copy. We have other macros that perform their task then print, close the document, and then close word without saving a new document. That is fine. Sorry for the confusion.

I appreciate the time and consideration you are giving this and hope I am giving you the info you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top