Consulting

Results 1 to 11 of 11

Thread: Asc Char for end of page?

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location

    Asc Char for end of page?

    Ascii for end of paragraph is 13. I was wondering what it is for end of page? Dave

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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
    [VBA] 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
    [/VBA]

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Dave,
    Very suitable for a KB entry.
    Question: How do you ensure the correct order of documents?
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Actually, it is not all that efficient. If you are NOT concerned with empty files - files with just the default paragraph mark:[vba]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 Sub[/vba]will 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.[vba]
    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[/vba]

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

  7. #7
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:[vba]ActiveDocument.Close
    ThisDoc.Activate [/vba] to[vba]WrdApp2.ActiveDocument.Close
    WrdApp2.ThisDoc.Activate [/vba]

  9. #9
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:[vba]Set ThisDoc = ActiveDocument[/vba] 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:[vba]Set ThisDoc = WrdApp2.ActiveDocument[/vba]The instance (WrdApp2) knows what ActiveDocument means.
    The code I posted was XL VBA and does not require a reference to the Word library
    This 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 think[vba]Set Wdapp2 = CreateObject("Word.Application") [/vba]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.

  11. #11
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    836
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •