Solved

Accessing a word document

Posted on 2000-04-20
10
245 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now