Solved

Script to extract the first 30 characters of each line from a file

Posted on 2012-03-14
7
426 Views
Last Modified: 2012-06-06
Hello,

I have a word document that is 145 pages.  I only need the first 30 characters of each line.  Is there any way to have a script scan the file and either extract the first 30 characters of each line and create a new file or delete the rest from each line?

Thank you
0
Comment
Question by:cbones
  • 4
  • 2
7 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37721984
Try this but it may not work because of the encoding that Word does. If you go into Word and save the doc as a text file I think it will work.


Dim FF As Integer
Dim FF2 As Integer
Dim strLine As String

FF = FreeFile
Open "C:\temp\test.doc" For Input As #FF

FF2 = FreeFile
Open "C:\temp\temp.doc" For Output As #FF2

Do While Not EOF(FF)
    Line Input #FF, strLine
    If Len(strLine) > 29 Then
        strLine = Left$(30, strLine)
    End If
    Print #FF2, strLine
Loop

Close
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 37722324
I have a word document that is 145 pages.  I only need the first 30 characters of each line.

Define "line".  There is no such object in Word.

Indeed, since different print drivers can render printed text differently, the whole concept of a "line" of text does not make much sense.

Did you perhaps mean "I only need the first 30 characters of each paragraph"?
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 400 total points
ID: 37722433
This converts the doc before reading it. You'll need to add a Reference to Word.

Dim FF As Integer
Dim FF2 As Integer
Dim strLine As String
Dim appWord As Word.Application
Dim wrdDoc As Word.Document

Set appWord = New Word.Application
Set wrdDoc = appWord.Documents.Open("C:\temp\test.doc")

appWord.ChangeFileOpenDirectory "C:\temp\"

appWord.ActiveDocument.SaveAs FileName:="temp.txt", FileFormat:=wdFormatText, _
        LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False, Encoding:=1252, InsertLineBreaks:=False, AllowSubstitutions:=False _
        , LineEnding:=wdCRLF

wrdDoc.Close False
appWord.Quit
Set wrdDoc = Nothing
Set appWord = Nothing

FF = FreeFile
Open "C:\temp\temp.txt" For Input As #FF

FF2 = FreeFile
Open "C:\temp\temp.doc" For Output As #FF2

Do While Not EOF(FF)
    Line Input #FF, strLine
    If Len(strLine) > 29 Then
        strLine = Left$(strLine, 30)
    End If
    Print #FF2, strLine
Loop

Close
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:cbones
ID: 37804207
I am not able to get this script to work.  I will try it again.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37804238
What happens when you try it?
0
 

Author Comment

by:cbones
ID: 37865616
I recieve

line 2
char 8
expected end of statement
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37865811
Please attach the file that you are trying to read.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now