Solved

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

Posted on 2012-03-14
7
432 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 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 48

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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

717 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