Results 1 to 8 of 8

Thread: word automation

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    word automation

    Hey,

    How can I read a word document line by line using automation?
    Links to examples would be great.

    thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: word automation

    Here is some pseudo code:
    Add a Reference to Word Library, and make the appropriate changes (ie Application will become your Word Object reference)

    VB Code:
    1. Sub ReadWord()
    2. Dim strArr() As String
    3. Dim intCount As Integer
    4. Dim intIdx As Integer
    5.  
    6.     'Capture the Word Text
    7.     ActiveDocument.Select
    8.  
    9.     'Load an Array with each line
    10.     strArr() = Split(Selection.Text, vbCr)
    11.  
    12.     intCount = UBound(strArr) - 1
    13.  
    14.     For intIdx = 0 To intCount
    15.         MsgBox strArr(intIdx)
    16.     Next
    17.  
    18. End Sub
    Last edited by Bruce Fox; Dec 14th, 2005 at 07:14 PM.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: word automation

    Here is a working version that dosn't require a Library Reference:

    (Just have the instance of Word open)

    VB Code:
    1. Option Explicit
    2.  
    3. 'Note: NO reference to MS Word Object Library x.x is required in this example
    4.  
    5. Private Sub Form_Load()
    6. Dim objWord As Object
    7. Dim strArr() As String
    8. Dim intCount As Integer
    9. Dim intIdx As Integer
    10.  
    11. On Error GoTo Err_Handler
    12.  
    13.     Set objWord = GetObject(, "Word.Application")
    14.  
    15.     'Capture the Word Text
    16.     objWord.ActiveDocument.Select
    17.  
    18.     'Load an Array with each line
    19.     strArr() = Split(objWord.Selection.Text, vbCr)
    20.  
    21.     intCount = UBound(strArr) - 1
    22.  
    23.     For intIdx = 0 To intCount
    24.         MsgBox strArr(intIdx)
    25.     Next
    26.  
    27.     Set objWord = Nothing
    28.  
    29. Exit Sub
    30.  
    31. Err_Handler:
    32.     If Not (objWord Is Nothing) Then Set objWord = Nothing
    33.    
    34.     MsgBox "Number: " & Err.Number & vbCrLf & _
    35.     "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!"
    36. End Sub
    Last edited by Bruce Fox; Dec 14th, 2005 at 07:14 PM.

  4. #4

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: word automation

    Thanks a lot.

    How about going through each page in the document?
    Don't anthropomorphize computers -- they hate it

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: word automation

    Are the pages seperated by Page Breaks?

  6. #6

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: word automation

    yes, each page is separated by a page break.
    Don't anthropomorphize computers -- they hate it

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: word automation

    You might be able to get some ideas here:
    http://support.microsoft.com/?kbid=293861

  8. #8

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: word automation

    I've seen this article. I don't have any section breaks. I only have page breaks in my document. I have no control over the creation of the document, I just have to import the data.
    Don't anthropomorphize computers -- they hate it

Posting Permissions

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



Click Here to Expand Forum to Full Width