PDA

View Full Version : Asc Char for end of page?



Dave
03-22-2006, 09:21 AM
Ascii for end of paragraph is 13. I was wondering what it is for end of page? Dave

TonyJollans
03-22-2006, 10:42 AM
Normal end of page caused by Word running out of space isn't identified by any character in the text flow.

A hard page break (Ctrl+Enter) is represented by character code 12 - but so is a section break.

Dave
03-22-2006, 11:38 AM
Thanks Tony that works. I was kicking around combining Word docs into 1 doc while still maintaining their page/printout format. This seems fairly efficient. It took about 10 secs to produce a 130 page doc of 78 seperate files in my testing. Thought I would post the code as another member may have some use for it. Have a nice day. Dave
Option Base 1

Sub Collectfiles()
'combines Word docs into one test.doc
'folder directory & C/test.doc must exist
Dim Wdapp As Object, Wdapp2 As Object, Position As String
Dim Bigstring As String, DocArray() As Variant, Cnt As Integer
Dim TempString As String, MyRange As Variant, Foldername As String
Dim Fso As Object, Cnt2 As Integer, Fldr As Object, F As Object

Foldername = "c:\records\" 'folder directory

On Error GoTo Erfix3
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fldr = Fso.GetFolder(Foldername)
For Each F In Fldr.Files
Cnt2 = Cnt2 + 1
If Right(F.Name, 4) = ".doc" Then
ReDim Preserve DocArray(Cnt2)
DocArray(Cnt2) = F.Name
End If
Next F
Set Fso = Nothing

On Error GoTo Erfix
Set Wdapp = CreateObject("Word.Application")
Wdapp.ChangeFileOpenDirectory Foldername

For Cnt = LBound(DocArray) To UBound(DocArray)
Wdapp.Documents.Open Filename:=DocArray(Cnt)
Wdapp.activedocument.Select

Set MyRange = Wdapp.activedocument.Paragraphs(1).Range
MyRange.SetRange Start:=MyRange.Start, _
End:=Wdapp.activedocument.Paragraphs(Wdapp.Selection.Paragraphs.Count).Rang e.End
MyRange.Select
TempString = Wdapp.Selection.Text
Wdapp.activedocument.Close savechanges:=True

If Len(TempString) <> 1 Then 'empty doc has 1 para range

'maintain page format
'add page break to end of range
If Cnt <> 1 Then
TempString = TempString + Chr(12)
End If

Bigstring = Bigstring + TempString
End If

Next Cnt

Wdapp.Quit
Set Wdapp = Nothing

On Error GoTo Erfix2
Set Wdapp2 = CreateObject("Word.Application")
Wdapp2.ChangeFileOpenDirectory "c:\"
Wdapp2.Documents.Open Filename:="test.doc"

With Wdapp2.activedocument
.Range(0, .Characters.Count).Delete
.content.insertafter Bigstring
End With
With Wdapp2.Selection
Wdapp2.Visible = True
End With

Set Wdapp2 = Nothing
Exit Sub

Erfix3:
On Error GoTo 0
MsgBox "You have a folder error"
Set Fso = Nothing
Exit Sub
Erfix2:
On Error GoTo 0
MsgBox "You have a transfer file error"
Wdapp2.Quit
Set Wdapp2 = Nothing
Exit Sub
Erfix:
On Error GoTo 0
MsgBox "You have a source file error"
Wdapp.Quit
Set Wdapp = Nothing
End Sub

mdmackillop
03-22-2006, 11:49 AM
Hi Dave,
Very suitable for a KB entry.
Question: How do you ensure the correct order of documents?
Regards
MD

Dave
03-22-2006, 12:39 PM
I sequentially number the files. With a few minor changes and your inspiration, I don't think it would be that hard to orderly remove the files as directed which would create your correct order of documents in the array. I don't really have much use for this at this time and I hadn't considered the possibility that you might want to print them in some order so you wouldn't have to collate manually. Each file is printed as a seperate range in the above code complete with page break at the end... Thanks again Tony. Have a nice day MD. Dave

fumei
03-22-2006, 06:33 PM
Actually, it is not all that efficient. If you are NOT concerned with empty files - files with just the default paragraph mark:Sub LoadEmUp1()
Dim myFile
Dim myPath As String
myPath = "c:\test\test2\"
myFile = Dir(myPath & "*.doc")
Do While myFile <> ""
With Selection
.InsertFile FileName:=myPath & myFile
.TypeText Text:=Chr(12)
End With
myFile = Dir
Loop
End Subwill append all DOC files in the specified folder into one document, separating them by a page break.

If you ARE concerned with empty files - files with ONLY a empty paragraph, then you have to open the file and check.
Sub LoadEmUp2()
Dim ThisDoc As Word.Document
Dim myFile
Dim myPath As String
myPath = "c:\test\test2\"
myFile = Dir(myPath & "*.doc")
Set ThisDoc = ActiveDocument
Do While myFile <> ""
Documents.Open FileName:=myPath & myFile
' if document is just the default paragraph (empty)
' close myFile and move on
If ActiveDocument.Content = Chr(13) Then
ActiveDocument.Close
myFile = Dir
' otherwise, insert myFile, and add page break
Else
ActiveDocument.Close
ThisDoc.Activate
With Selection
.InsertFile FileName:=myPath & myFile
.TypeText Text:=Chr(12)
End With
myFile = Dir
End If
Loop
Set ThisDoc = Nothing
End Sub

No need for the array, or ranges, or those strings.

Dave
03-22-2006, 07:50 PM
Thanks for the enlightenment Gerry. I should have mentioned that it was XL VBA code that I posted. I could not get your code to work in XL (obj doesn't sup prop/meth error on insert file line of sub 1: activex can't create obj error on line set thisdoc etc of sub2). I tried setting the Word reference but no luck. I assume this is therefore likely Word VBA and I apologise for the confusion. I do like the simplicity of the code you posted but don't exactly understand it. Is the collected file in the same directory and what is the name of the file? Dave

fumei
03-22-2006, 09:36 PM
Huh? What you posted is not XL VBA. You may be running it from Excel, but it ain't "XL VBA". Excuse me, but if you are using ActiveDocument, Paragraph.Range etc. etc...this is Word. Not Excel.

No....of COURSE it is not going to work if you do not use the proper object. You are making a reference to Word, using Wdapp2 (as one of your references...by the way, why on earth are you making multiple instances of Word????).

So unless you USE that instance, as an application object, it is not going to work. It has nothing at all to do with XL. You would get the same problem if you ran it, without proper object references, from Access, or any other VBA compliant application. You need to use the full reference.

So, for example, did you make ALL the changes from:ActiveDocument.Close
ThisDoc.Activate toWrdApp2.ActiveDocument.Close
WrdApp2.ThisDoc.Activate

Dave
03-23-2006, 06:29 AM
Thanks again Gerry. The code I posted was XL VBA and does not require a reference to the Word library... perhaps I don't understand your definition. In the past, the use of 2 seperate instances of Word has removed some error which I could find no other way to get rid of. I trialled the code I posted using only 1 instance and it works fine. I made no changes to the code you provided as I thought it was complete and as I mentioned I don't exactly understand it. I appreciate your time and input to this thread. Dave

fumei
03-23-2006, 08:29 AM
Au contraire. What you posted was VBA. It is run from Excel. But it is just VBA - there is nothing special about Excel. The Excel reference library is of course different, but that is just the objects and properties within that library. The VBA you posted is VBA. Again it is nothing specific to Excel. If you were trying to do this operation from Access, or PowerPoint you would use the same code. It would not be Access VBA, or PowerPoint VBA. VBA is VBA. In fact, if you look at your code there is NO indication that it has anything to do with Excel. That is because it does not have anything (really) to do with Excel. Excel is simply where you are running it FROM.

You make an instance of Word (WrdApp2...or whatever it is). ANY use of the Word library must use THAT instance. So:Set ThisDoc = ActiveDocument will fail. It works for me because I am doing it within Word. So I am using that instance. That instance knows what ActiveDocument means. YOU are using WrdApp2 (or whatever). So it has to be:Set ThisDoc = WrdApp2.ActiveDocumentThe instance (WrdApp2) knows what ActiveDocument means.
The code I posted was XL VBA and does not require a reference to the Word libraryThis is totally incorrect. It is NOT XL VBA. It is VBA that happens to be run from XL. As for not requiring a reference to the Word library....what do you thinkSet Wdapp2 = CreateObject("Word.Application") DOES? It makes a reference to that library! That is what making the instance MEANS. Word.Application means....hey make an instance of this application...and the application is created, and since the application needs its own library TO run...yup, the library is referenced. It would not be able to anything if it did not. So, sorry, you are incorrect. It not only does require a reference to the Word library, it makes one.

Dave
03-23-2006, 01:36 PM
I certainally am learning lots on this thread. I thank you for your kind patience. I guess what I meant was that using late binding does not require the user to manually set the reference. I now have a fuller understanding of the application object and references. Also I think I get the reason for my Word/XL VBA distinction. VBA from Word already has an application object. The syntax/reference differences with no direct mention of the application seemed like the Word/XL VBA were of 2 different breeds. Thank you for this clarity but I have still not been able to adjust your code. I do (hopefully) understand that the destination for the code you posted was your opened word document. I have not been able to adapt your code to operate outside Word by using WrdApp2 (or whatever) to combine multiple docs into another single doc. I will post if I have any success. Have a nice day. Dave