Solved

Accessing a word document

Posted on 2000-04-20
10
248 Views
Last Modified: 2010-05-02
This is what I want to do...

I made a web site in ASP where users can log on and transmit information, which used to be in a Word Document.  I need to make a VB program to

1. Open a word document
2. Get the information needed
3. Submit it to a SQL database

I assume that the submitting of information is virtually the same as what is done in VBScript, which could probably be easily found on the internet somewhere, but what is the code to open a word document and then atleast get me started on searching the document for key fields?

Thanks
thunderchicken
0
Comment
Question by:thunderchicken
  • 6
  • 3
10 Comments
 
LVL 1

Expert Comment

by:Gotcha007
ID: 2735003
Go to the ProjectMenu choose Reference  and add a reference to Microsoft Word 8 object library.


Create a class module with the Name =  EventClassModule and put this code inside:

Public WithEvents AppWord As Word.Application

Private Sub Class_Initialize()

   'Instantiate a new instance of Word and add
   'a new document
   Set AppWord = New Word.Application
End Sub



in your form :

Set X = New EventClassModule
X.AppWord.Documents.Open "c:\Mydoc.doc"

for any help using This object you can go to the word help and choose VB Chapter.


Ludo


0
 
LVL 43

Expert Comment

by:TimCottee
ID: 2735012
thunderchicken:

Private Sub Command1_Click()
    On Error GoTo errWordNotOpen
    Dim appWord As Word.Application
    Dim docWord As Word.Document
    Dim rngWord As Word.Range
    Set appWord = GetObject(, "Word.Application")
    If appWord Is Nothing Then
        Set appWord = CreateObject("Word.Application")
    End If
    appWord.Visible = True
    Set docWord = appWord.Documents.Open("<full path to document>")
    ' Code for finding fields etc.,
    docWord.Close
    Set docWord = Nothing
    appWord.Quit
    Set appWord = Nothing
    Exit Sub
   
errWordNotOpen:
    If Err.Number = 429 Then
        Resume Next
    Else
        MsgBox Err.Description & vbLf & CStr(Err.Number), vbCritical
    End If
End Sub

Will get you started by opening a word document, you then need to find the appropriate fields in the document. Depending on what you are looking for and how the document is formatted there may be many ways to do this.
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 2735111
On both codes, I get the "User-defined type not defined" error.  I have only wrote "basic" BASIC programs and have not really gotten deep into these aspects, can either of you write out step by step what I need to do to atleast be able to access the file?  Right now I have a file1 and drive1 so the user can search for the file, then click a button, from there all I need to do is have the program search the document, since the format will be the same it will be easy to give error messages, and with that I can directly take out the data I need, since the word document is set up and users just click on where to get the data.  Any additional help is helpful!
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 43

Expert Comment

by:TimCottee
ID: 2746552
gotcha007 made the point that you need to add a reference to Word automation objects to the project.

On the "Project" menu, choose "References", scroll down until you see something like:

"Microsoft Word 8.0 Object Library"

check the box next to this. This will add references to the OLE automation objects within word to the project, the undefined type errors should then disappear.

It would help if you could give some more information as to how the documents are set out, if for example there are bookmarks in the document which can be used to navigate through, or whether the information you want is at specific points in the text. If so how to identify which portions of the content to return.
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 2747254
okay, i'll check it out today, thanks
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 2748419
Thanks Tim, I used your code, but one last question and I'll give you the points, can you give me direction on how to start to open the file and sort the information?  Maybe a key would be in the field Name: on page 2 and I'll need the name after that.  Just an example, there are about 2000 that I need, but it will get me started.  Thanks in advance
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 2748427
or split the points, whoever can help me out first
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 2750628
thunderchicken, here is a start, replace "code for finding fields" with:

    Set rngWord = docWord.GoTo(What:=wdPage, Which:=wdGoToAbsolute, Count:=2)
    With rngWord.Find
        .Execute "Telxon", True, True
        MsgBox .Found & vbLf & rngWord.Start & vbLf & rngWord.End
        rngWord.EndOf Unit:=wdParagraph, Extend:=wdExtend
        MsgBox rngWord.Text
        rngWord.Bold = True
        rngWord.Font.Size = 72
    End With

The first line sets a range starting at the beginning of page 2. Finds the word Telxon, extends the range to the end of the paragraph, then sets the text in this range to bold & size 72 points.

It should help to give you an idea of how to find bits of the document and manipulate the formatting. The help is a bit awkward sometimes to understand but does have everything you need.
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 2751130
Thanks for your help, that will get me started on the task.
0
 
LVL 11

Author Comment

by:thunderchicken
ID: 2766561
http://www1.experts-exchange.com/bin/Q.10334457

150 more points if interested
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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