Solved

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

Posted on 2012-03-14
7
430 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 47

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 47

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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 47

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

713 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