Solved

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

Posted on 2012-03-14
7
427 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 46

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 46

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

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 46

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now