?
Solved

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

Posted on 2012-03-14
7
Medium Priority
?
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 48

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 93

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 48

Accepted Solution

by:
Martin Liss earned 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

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 48

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

771 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