Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Word file split

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2714
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Word file split
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Word file split - 4/16/2005 5:03:48 AM   
  TNO


Posts: 1075
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Good Morning/Evening,

I'm currently working on a project to take a 500 page text file and split it up into multiple files formatted in Microsoft Word format. I've completed most of what I need, but I'm having difficulty in the most important part. Splitting and naming the files.

Heres what I start with:

filename.txt

Here's what the result needs to be:

variableName1.doc
variableName2.doc
variableName3.doc
variableName4.doc
etc...


Each "page" in the text file begins with the pagebreak " " symbol and ends with the page number (last 5 digits).

The variable filenames should be defined from Line 7, characters 43-48 of each "page" in the text file.

The number of pages of each section and are variable but consecutive.
Example:(1-4,1-3,1,1-65).

So the Result should be

YNAME1.doc (pages 1-4)
YNAME2.doc (pages 1-3)
YNAME3.doc (1 page)
YNAME4.doc (pages 1-65)
etc...


If you could provide any assistance it would be greatly appreciated.

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch
 
 
Post #: 1
 
 Re: Word file split - 4/16/2005 4:19:25 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Not really sure what you wanted to do.

(in reply to TNO)
 
 
Post #: 2
 
 Re: Word file split - 4/16/2005 4:54:00 PM   
  TNO


Posts: 1075
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
I'd like to take the text file and split it into multiple word documents. There are pages predefined in the text file. Are so are the desired file names.

(in reply to TNO)
 
 
Post #: 3
 
 Re: Word file split - 4/16/2005 10:05:44 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Do you have a sample text file that contains the exact content said here ?

If anyone else fully understood what he wanted, please elaborate for me as I'm still not sure what exactly he wanted do.

Also, could you post the script you've done so far ? Maybe that will explain the entire situation a little bit better.

Furthermore, do you want to actually convert the original text to actual DOC format or just rename them to *.DOC files ?

My understanding so far is this:

You have a .TXT file that worths approximately 500 pages. These 500 pages contains N number of SECTIONS in which each such section is deterined by the pagebreak symbol. You want to re-write each of this section to its own file using some arbitrary names defined at line 7, characters 43-48 of each section. At the end of each section (just before the next pagebreak), there exist a number that denotes the number of pages this specific section has.

(in reply to TNO)
 
 
Post #: 4
 
 Re: Word file split - 4/16/2005 11:14:07 PM   
  TNO


Posts: 1075
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Sample Text file:

http://www.geocities.com/owlman5000/CMR.txt

Heres the code thus far:


<HTML>
<HEAD>
<TITLE>CMR Split</title>

<HTA:APPLICATION ID = "CMR Split"

APPLICATIONNAME = "CMR Split"

BORDER = "thin"

CAPTION = "yes"

CONTEXTMENU="yes"

NAVIGABLE="yes"

SELECTION = "yes"

SHOWINTASKBAR = "yes"

SINGLEINSTANCE = "yes"

SYSMENU = "yes"

VERSION = "1.00"

WINDOWSTATE = "maximize"

>

<SCRIPT LANGUAGE="JavaScript">
<!--

function blockError(){return true;}
window.onerror = blockError;

function whatFile() {
var ForReading = 1;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var A = document.form1.cmuds.value;
var f = fso.OpenTextFile(A, ForReading);
var r = f.ReadAll();
f.close();
window.ME.value= r;
}

function Progress() {
window.BAR.location= 'about:<html><head></head><body bgcolor="black" text="green" oncontextmenu="return false" onselectstart="return false" ondragstart="return false"><div align="center"><h3>Working<marquee direction="right" width=5%>...</marquee></h3></div></body></html>';
}

function Complete() {
window.BAR.location= 'about:<html><head></head><body bgcolor="black" text="green" oncontextmenu="return false" onselectstart="return false" ondragstart="return false"><div align="center"><h3>Done!</h3></div></body></html>';
}

function Clear() {
window.BAR.location= 'about:<html><head></head><body bgcolor="black" text="green" oncontextmenu="return false" onselectstart="return false" ondragstart="return false"></body></html>';
}

// End -->

</script>

<SCRIPT LANGUAGE="VBScript">
<!--

dim filename
filename = "CMR"

'Browse for folder

Function GetFolder()

Const BIF_EDITBOX = &H10
Const BIF_NEWDIALOGSTYLE = &H40
Dim sa
Set sa = CreateObject("Shell.Application")
Dim oF
Set oF = sa.BrowseForFolder(0, "My Computer:", BIF_EDITBOX Or BIF_NEWDIALOGSTYLE)
Dim fi
Set fi = oF.Items.Item

document.form2.FOLDER.value= fi.Path

End Function

'Convert to Word Document 8pt Courier New

Function Go()

Const ForReading = 1

dim fso
set fso = createobject("scripting.filesystemobject")

dim CMR
dim CMRTEXT
Set CMR = fso.OpenTextFile(window.form1.cmuds.value, ForReading)
CMRTEXT = CMR.ReadAll

''''''''Split according to " " (Split by page)
dim CMRArray
CMRArray = Split(CMRTEXT," ")
dim MAXPAGE
MAXPAGE = UBound(CMRArray)


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''For Loop here?''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim MyDoc
Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = False

' Set the font properties
MyDoc.content.Font.Bold = False
MyDoc.content.Font.Italic = False
MyDoc.content.Font.Underline = False
MyDoc.content.Font.Name = "Courier New"
MyDoc.content.Font.Size = 8

'MARGINS
With MyDoc.Application.ActiveDocument.PageSetup
.LeftMargin = MyDoc.Application.InchesToPoints(0.7)
.RightMargin = MyDoc.Application.InchesToPoints(0.7)
End With

''''''''Insert CMR, but how to split?
MyDoc.content.insertAfter join(CMRArray," ")
MyDoc.content.InsertParagraphAfter

'''''''''How to save each individual section?
MyDoc.SaveAs(document.form2.FOLDER.value + "\" + filename + ".doc")

MyDoc.Close

Set MyDoc = Nothing

set fso = nothing

Complete()

End Function

-->
</SCRIPT>


</HEAD>

<BODY bgcolor="black" text="green" oncontextmenu="return true" onload="Clear()">
<center>
<li>Error Catching needed....
</center>

<div align="center">
<p>
<form name=form1>

(in reply to TNO)
 
 
Post #: 5
 
 Re: Word file split - 4/17/2005 12:31:45 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
he'd like to split 1 doc into several documents....

(in reply to TNO)
 
 
Post #: 6
 
 Re: Word file split - 4/17/2005 4:20:36 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
Are you looking for this property?

PageBreak Object

(in reply to TNO)
 
 
Post #: 7
 
 Re: Word file split - 4/17/2005 9:11:43 AM   
  TNO


Posts: 1075
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
The biggest thing I am having trouble with is figuring out how to keep each section with one another. YARM with YARM in one Doc, YADJ with YADJ, and YBAS with YBAS. Not each array element in its own file.

(in reply to TNO)
 
 
Post #: 8
 
 Re: Word file split - 4/17/2005 12:14:05 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
heh.. I still can't understand exactly what he wanted :\

(in reply to TNO)
 
 
Post #: 9
 
 Re: Word file split - 4/17/2005 10:09:21 PM   
  TNO


Posts: 1075
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Thanks for the help.

I'm close but no cigar:

Function Go()

Const ForReading = 1

dim fso
set fso = createobject("scripting.filesystemobject")

dim CMR
dim CMRTEXT
Set CMR = fso.OpenTextFile(window.form1.cmuds.value, ForReading)
CMRTEXT = CMR.ReadAll

''''''''Split according to " " (Split by page)
dim CMRArray
CMRArray = Split(CMRTEXT," ")

dim SectionName, Page, PageNum, PrevPageNum
''''''''Iterate through

For Each Page In CMRArray
SectionName = Mid(Page,497,6)
PageNum = Trim(Right(Page,5))

''''''''Create Word Document

Dim MyDoc
Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = False

' Set the font properties
MyDoc.content.Font.Bold = False
MyDoc.content.Font.Italic = False
MyDoc.content.Font.Underline = False
MyDoc.content.Font.Name = "Courier New"
MyDoc.content.Font.Size = 8

'SETUP MARGINS
With MyDoc.Application.ActiveDocument.PageSetup
.LeftMargin = MyDoc.Application.InchesToPoints(0.7)
.RightMargin = MyDoc.Application.InchesToPoints(0.7)
End With

''''''''Insert Pages

If PageNum > PageNumPrev Then

MyDoc.content.insertAfter Page & " "
MyDoc.content.InsertParagraphAfter

PageNumPrev = PageNum

Else
'''''''''Save:
MyDoc.SaveAs(document.form2.FOLDER.value + "\" + SectionName + ".doc")

MyDoc.Close

Set MyDoc = Nothing

PageNumPrev = PageNum

End If

Next


set fso = nothing

Complete()

End Function

I can create all the correct file names in the array, but my logic is obviously off so I can't insert any content.

(in reply to TNO)
 
 
Post #: 10
 
 Re: Word file split - 4/18/2005 6:48:34 AM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
open and close several instances of Word (minimized ofcourse) and thus emptying the array into the files...

Something like...
      



[edit]spell check[/edit]

(in reply to TNO)
 
 
Post #: 11
 
 Re: Word file split - 4/19/2005 12:55:27 AM   
  TNO


Posts: 1075
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
The Project was a success. Thank you for pointing me in the right direction.

<SCRIPT LANGUAGE="VBScript">
<!--

dim filename
filename = "CMR"

'Browse for folder

Function GetFolder()
Const BIF_EDITBOX = &H10
Const BIF_NEWDIALOGSTYLE = &H40
Dim sa
Set sa = CreateObject("Shell.Application")
Dim oF
Set oF = sa.BrowseForFolder(0, "My Computer:", BIF_EDITBOX Or BIF_NEWDIALOGSTYLE)
Dim fi
Set fi = oF.Items.Item
document.form2.FOLDER.value= fi.Path
End Function

Function Go()
Const ForReading = 1
dim fso
set fso = createobject("scripting.filesystemobject")
dim CMR
dim CMRTEXT
Set CMR = fso.OpenTextFile(window.form1.cmuds.value, ForReading)
CMRTEXT = CMR.ReadAll
'Split according to " " (Split by page)
dim CMRArray
CMRArray = Split(CMRTEXT," ")
dim SectionName, Page, PageNum, PrevPageNum
PrevPageNum = 0

dim MyDoc
Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = TRUE
''''''''Iterate through

For Each Page In CMRArray

PageNum = Trim(Right(Page,5))

If PageNum = "" Then
PageNum = 0
End If

window.form1.WORKME.value = PageNum

PageNum = Int(window.form1.WORKME.value)

''''''''Insert Pages

If PageNum = 1 And PrevPageNum = 0 Then

SectionName = Mid(Page,497,6)

With MyDoc.content.Font
.Bold = False
.Italic = False
.Underline = False
.Name = "Courier New"
.Size = 8
End With
With MyDoc.Application.ActiveDocument.PageSetup
.LeftMargin = MyDoc.Application.InchesToPoints(0.7)
.RightMargin = MyDoc.Application.InchesToPoints(0.7)
End With

With MyDoc.content
.InsertAfter Page & " "
.InsertParagraphAfter
End With

PrevPageNum = PageNum

ElseIf PageNumPrev = 1 And PageNum = 1 Then

MyDoc.SaveAs(document.form2.FOLDER.value + "\" + Trim(SectionName) + ".doc")
MyDoc.Close
Set MyDoc = Nothing

SectionName = Mid(Page,497,6)

Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = True

With MyDoc.content.Font
.Bold = False
.Italic = False
.Underline = False
.Name = "Courier New"
.Size = 8
End With
With MyDoc.Application.ActiveDocument.PageSetup
.LeftMargin = MyDoc.Application.InchesToPoints(0.7)
.RightMargin = MyDoc.Application.InchesToPoints(0.7)
End With

With MyDoc.content
.InsertAfter Page & " "
.InsertParagraphAfter
End With

PrevPageNum = PageNum

ElseIf PageNum > PageNumPrev Then

With MyDoc.content
.InsertAfter Page & " "
.InsertParagraphAfter
End With

PrevPageNum = PageNum

ElseIf PageNum < PageNumPrev Then

MyDoc.SaveAs(document.form2.FOLDER.value + "\" + Trim(SectionName) + ".doc")
MyDoc.Close
Set MyDoc = Nothing

SectionName = Mid(Page,497,6)

Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = True

With MyDoc.content.Font
.Bold = False
.Italic = False
.Underline = False
.Name = "Courier New"
.Size = 8
End With
With MyDoc.Application.ActiveDocument.PageSetup
.LeftMargin = MyDoc.Application.InchesToPoints(0.7)
.RightMargin = MyDoc.Application.InchesToPoints(0.7)
End With

With MyDoc.content
.InsertAfter Page & " "
.InsertParagraphAfter
End With

PrevPageNum = PageNum

ElseIf PageNum = 0 Then

MyDoc.SaveAs(document.form2.FOLDER.value + "\" + Trim(SectionName) + ".doc")
MyDoc.Close
Set MyDoc = Nothing

SectionName = Mid(Page,497,6)

Else
MsgBox(SectionName & " This page:" & PageNum & "is something not covered. The last page:" & PageNumPrev)

End If

PageNumPrev = PageNum

Next

MyDoc.Application.Quit False
Set MyDoc = Nothing

set fso = nothing

End Function

-->
</SCRIPT>

(in reply to TNO)
 
 
Post #: 12
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Word file split Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts