[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Accessing a word document

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
thunderchicken
Asked:
thunderchicken
  • 6
  • 3
1 Solution
 
Gotcha007Commented:
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
 
TimCotteeCommented:
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
 
thunderchickenAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimCotteeCommented:
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
 
thunderchickenAuthor Commented:
okay, i'll check it out today, thanks
0
 
thunderchickenAuthor Commented:
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
 
thunderchickenAuthor Commented:
or split the points, whoever can help me out first
0
 
TimCotteeCommented:
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
 
thunderchickenAuthor Commented:
Thanks for your help, that will get me started on the task.
0
 
thunderchickenAuthor Commented:
http://www1.experts-exchange.com/bin/Q.10334457

150 more points if interested
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now