Results 1 to 3 of 3

Thread: [RESOLVED] Delete the first two lines of a word Doc.

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Resolved [RESOLVED] Delete the first two lines of a word Doc.

    I am using a Word Macro that edits an open document by keeping the first 30 lines and last ten lines of a section for as many sections there are in a document then creates a new doc. Each section is marked by a "(" and this is what the code uses to find the sections.

    The problem is that at the beginning of the document I have two lines before the first "(" and this throws off the document by two lines. I want each section to make up 1 page. If I change the numbers two make the first page look all right, the rest of the doc. gets screwed up.

    I would like to delete the first two lines of the ActiveDocument and then the macro to work from there. I have tried ranges and selections to delete the first two lines and can't get it to work.

    Here is the code:

    VB Code:
    1. Sub YoYo()
    2.  
    3. Dim MyFile As Variant
    4. Dim OutputFile As String
    5. Dim count As Integer
    6. Dim LineArray() As String
    7. Dim MyLine As String
    8. Dim arrNum As Long
    9. Dim openfile As Long
    10. Dim temp As String
    11. Dim ALine, DeleteAt As Long
    12.  
    13.  
    14.  
    15. On Error GoTo MyErrorHandler:
    16.  
    17. ReDim LineArray(10000000#)
    18. OutputFile = Mid$(CStr(ActiveDocument), 1, Len(ActiveDocument) - 3) & "doc"
    19. Open ActiveDocument For Input As #1
    20. Open OutputFile For Output As #2
    21.  
    22.  
    23.  
    24.  
    25. Line Input #1, MyLine
    26. While InStr(1, MyLine, "(") = 0
    27. Print #2, MyLine
    28. Line Input #1, MyLine
    29. Wend
    30.  
    31. arrNum = 1
    32. While Not EOF(1)
    33. If InStr(1, MyLine, "(") <> 0 Then
    34. Print #2, MyLine
    35. For i = 1 To 30
    36. Line Input #1, MyLine
    37. Print #2, MyLine
    38. Next i
    39.  
    40. For i = 1 To 6
    41. Print #2,
    42. Next i
    43.  
    44. Line Input #1, MyLine
    45.  
    46.  
    47. Do Until InStr(1, MyLine, "(") <> 0
    48. LineArray(arrNum) = MyLine
    49. arrNum = arrNum + 1
    50. Line Input #1, MyLine
    51. Loop
    52. If InStr(1, MyLine, "(") <> 0 Then
    53. For i = 20 To 1 Step -1
    54. Print #2, LineArray(arrNum - i)
    55. Next i
    56. End If
    57. ReDim LineArray(10000000#)
    58. arrNum = 1
    59. End If
    60. Wend
    61.  
    62. Close #1
    63. Close #2
    64.  
    65. UserForm1.Hide
    66.  
    67. MyErrorHandler:
    68. If Err.number = 62 Then
    69. Close #1
    70. Close #2
    71. 'Set oDoc = Documents.Open(Path & OutputFile)
    72. 'With oDoc
    73. ' .PrintOut
    74. ' .Close SaveChanges:=False
    75. 'End With
    76. 'Set oDoc = Nothing
    77. End If
    78. UserForm1.Hide
    79.  
    80. Exit Sub
    81.  
    82.  
    83. End Sub

    here is a sample of the beginning of the doc:

    %
    O0001
    ( DIAMETER = .5 )
    ( CORNER RADIUS = .031 )
    ( STOCK ALLOWANCE = 0 )
    G91 G28 Z0
    H0 G49
    T1
    M6
    G90
    M3 S2500
    G5 P1
    G61.1
    G0 X0 Y.0785
    G43 Z-1.4 H1 M50
    X.4685 Y-.8716
    Z-1.9
    G1 Z-2.003 F70......this goes on for hundreds of lines

    The second and rest of the sections all start the same way without the "%" and O0001 lines. These are the two lines I would like to delete before the macro runs. The new document does not need to be saved.

    Any help is appreciated.

  2. #2
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brugge, Belgium
    Posts
    208

    Re: Delete the first two lines of a word Doc.

    Hello,

    Why don't you just start reading at line 3

    Just do 2 line input before writing. And indent your code for readability. thanks

    Code:
    Line Input #1, MyLine
    Line Input #1, MyLine 
    ' this part checks for ( not being in the input, but why ?
      While InStr(1, MyLine, "(") = 0 
         Print #2, MyLine
         Line Input #1, MyLine
      Wend

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: Delete the first two lines of a word Doc.

    Thank You that is what I was looking for.Sorry about the format, I am new to this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width