Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check MS Word from MS Access 1

Status
Not open for further replies.

labanTek

Instructor
Dec 3, 2004
37
Hi all I would like to create an app to assess my students in MS Word. I would like them to perform tasks in Word and I would like an MS Access app to check if the tasks were done correctly. Is this possible and can I get an idea how to do this? Thanks in advance.
 
I've seen this done with commercial software, but can't seem to figure out how to check the word document.
 
labanTek,
Using automation you can connect to a Word document with Access. This will expose everything in the documnent, just as if you had opened the document manually.

You question is pretty general (there are a lot of things you could be checking) so here is a general answer. This will require a fair amount of coding, and before you start I would recommend adding a reference to Microsoft Word x.x Object Library (MSWORDx.OLB) so that the Word object model is exposed in Access.

You can open your students documents using the [tt]GetObject()[/tt] function by passing the file name of the students document.

If you can post an example of the things you are checking for it would be easier to give you a more precise answer.

Hope this helps,
CMP
Code:
Dim appWord as Object
Set appWord = GetObject("[i]C:\Folder\FileName.doc[/i]")
Once you have a pointer to the document you can use code to review everything that document contains.

(GMT-07:00) Mountain Time (US & Canada)
 
The idea given sounds great CautionMP. I have a word document that I want the students to for example, apply font size 16, font color blue, font type Arial to the first heading of the document. How exactly can I automatedly let an MS Access app check these attributes and apply a mark for the task.
Thanks in advance.
 
labanTek,
The following routine will check what you specified and runs from Access (2000) WITHOUT a reference to the Word x.x Object Library. If your situation allows, add a reference to your project since it will make developing and debugging a lot easier.

I assumed that the first heading was the first paragraph, and declared all the pieces and parts (Document, Paragrapgh, Range...) that I needed to check the values.

You will need to change the document name and path in the test function to match an actual document.
Code:
Sub TestFunction()
'This is for testing the following function
Dim strComment As String
Debug.Print Test_Heading_1_Attributes("[i]Enter_your_source_filename/path here[/i]", strComment), strComment
End Sub

Function Test_Heading_1_Attributes(DocumentName As String, FailComments As String) As Boolean
On Error GoTo Test_Heading_1_Attributes_Error
Const DefaultFontColor As Long = -16777216
'Declarations for the OLE object
Dim MyWordApplication As Object
Dim MyWordDocument As Object 'Document
Dim MyWordParagraph As Object 'Paragraph
Dim MyWordRange As Object 'Range
Dim MyWordFont As Object 'Font

'Get the word container
Set MyWordApplication = CreateObject("Word.Application")
'Open the document feed to the function
Set MyWordDocument = MyWordApplication.Documents.Open(DocumentName, , True)
'Get the rest of the objects
Set MyWordParagraph = MyWordDocument.Paragraphs(1) 'First paragraph
Set MyWordRange = MyWordParagraph.Range
Set MyWordFont = MyWordRange.Font

With MyWordFont
  'Test for Arial font face
  If .Name = "Arial" Then
    Test_Heading_1_Attributes = True
  ElseIf .Name = "" Then
    Test_Heading_1_Attributes = False
    FailComments = FailComments & "Font Name is mixed. "
  End If
  'Test for font size 16
  If .Size = 16 Then
    Test_Heading_1_Attributes = Test_Heading_1_Attributes And True
  ElseIf .Size = 9999999 Then
    Test_Heading_1_Attributes = False
    FailComments = FailComments & "Font Size is mixed. "
  End If
  'Test for font color Blue
  If .Color = vbBlue Then
    Test_Heading_1_Attributes = Test_Heading_1_Attributes And True
  ElseIf .Color = DefaultFontColor Then
    Test_Heading_1_Attributes = False
    FailComments = FailComments & "Font color is set to default. "
  ElseIf .Color = 9999999 Then
    Test_Heading_1_Attributes = False
    FailComments = FailComments & "Font color is mixed. "
  End If
End With

Clean_Up:
Set MyWordFont = Nothing
Set MyWordRange = Nothing
Set MyWordParagraph = Nothing
MyWordDocument.Close
Set MyWordDocument = Nothing
MyWordApplication.Quit
Set MyWordApplication = Nothing
Exit Function

Test_Heading_1_Attributes_Error:
Debug.Print Err.Number, Err.Description
Resume Clean_Up
End Function


Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
P.S. I made a mistake in the above code, it always returns False. [tt]Test_Heading_1_Attributes[/tt] needs to be initialized before the tests are performed.
Code:
...
Set MyWordFont = MyWordRange.Font

[b]Test_Heading_1_Attributes = True[/b]
With MyWordFont
...

CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Thanks so much for all your help CMP. I'm going to try the code in the on-click event of a form to see how it works. By the way can the Range be specified as a line and column parameter?
Thanks again.
labanTek
 
labanTek,
[tt]Range[/tt] can represent anywhere you place the cursor, or text that you highlight, so yes the range can be set with a line and column parameter.

Here's the rub. Word does have a [tt]Columns[/tt] collection and [tt]Column[/tt] class that you can navigate to using the same method I used for [tt]Paragraph[/tt]. In my experience* Line does not exist in Word (at least in the context you mean). Depending on what you are trying to locate you might look at [tt]Sentences[/tt] property of the [tt]Range[/tt] class.

*I do not have that much experience with macros in Word. I have done a couple of projects moving data from Access or Excel to Word and that's about it.

I would recommend spending some quality time with Object Browser if you don't already. This is my main tool when developing Word macros (unfortunately the help files and MSDN are lacking in the Word arena.)

If you get stuck on a specific problem and need help, definitely post the question on Tek-Tips, we're here to help. For items dealing with Word macros you might want to post in [navy]VBA Visual Basic for Applications (Microsoft) Forum[/navy] forum707 since it isn't Access specific.

Hope this helps,
CMP

(GMT-07:00) Mountain Time (US & Canada)
 
Get a copy of the book "Writing Word Macros" by Steven Roman (O'Reilly ISBN 1-56592-725-7)

This book exposes the Word Object Model and will step you through the basics on how to program Word. You can, as the above code example shows, control the Word application from Access. I use this technique to capture screen images and paste them into Word then, from Access, I distill the information and process it into database tables.

AvGuy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top