Open or Create word document from within Access

R

richardb

I have attempted to write a routine in Access that will test if a Word file
exists; create if it does not exist; then open it for editing. I can't quite
get it. I get the error message "Invalid use of New keyword." (I attached the
Word 11 Object Library). I would appreciate if someone wouldn't mind
debugging this for me.

Public Function OpenFile(strFileName As String)

Dim wrd As Word.Application
Dim doc As Document
Dim strFilePath As String
Dim strDocument As String

strFilePath = Trim(varLookup("Lookup", "tlkpStoredStrings", "VariableName =
'DocDir'")) 'varlookup is a replacement for DLookup.
strDocument = strFilePath & "/" & strFileName & ".DOC"
On Error GoTo Err_Exit

' make sure both files exist
If (Not bolFileExists(strDocument)) Then
' create new empty document
Set doc = New Document
doc.SaveAs FileName:=strDocument
Else
Set doc = strDocument
End If
doc.Open

Exit_Proc:
Set doc = Nothing
Exit Function

Err_Exit:
MsgBox "Procedure: AppendFile" & vbCrLf & "Error Number: " & Err.Number
& vbCrLf & Err.Description, vbCritical, "UNEXPECTED ERROR"
OpenFile = False
Resume Exit_Proc

End Function
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?cmljaGFyZGI=?=,
I have attempted to write a routine in Access that will test if a Word file
exists; create if it does not exist; then open it for editing. I can't quite
get it. I get the error message "Invalid use of New keyword." (I attached the
Word 11 Object Library). I would appreciate if someone wouldn't mind
debugging this for me.
I'm inserting the commment 'HERE where I'd change things

Public Function OpenFile(strFileName As String)

Dim wrd As Word.Application
Dim doc As Word.Document ''HERE
Dim strFilePath As String
Dim strDocument As String

strFilePath = Trim(varLookup("Lookup", "tlkpStoredStrings", "VariableName =
'DocDir'")) 'varlookup is a replacement for DLookup.
strDocument = strFilePath & "/" & strFileName & ".DOC"
On Error GoTo Err_Exit

'HERE
Set wrd = New Word.Application

' make sure both files exist
If (Not bolFileExists(strDocument)) Then
' create new empty document
'HERE
'You first need to start up the Word application
'that's where you use the NEW keyword.
'To create a new document, use the .Add method
'of the Documents collection
Set doc = wrd.Documents.Add
doc.SaveAs FileName:=strDocument
Else
'HERE
'use the Open method for an existing file
Set doc = wrd.Documents.Open(strDocument)
End If

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?cmljaGFyZGI=?=,
When I try this I get the error "User-defined type not defined" on the line:
Dim wrd As Word.Application

I do have the Office Object library attached as a reference.
You need to reference the WORD library. Office is a separate one, containing
things common to all the Office apps.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
P

Pranav Wagh

Or just type
Dim wrd As Object
in place of
Dim wrd As Word.Application
in fact late binding is a prefered way (performance !!) in OFFICE (only in
case of OFFICE )
--
Thank You..
Pranav Wagh, MCP, MCSD
Developer Support - Office Integration
Microsoft India(R & D) Pvt. Ltd
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UHJhbmF2IFdhZ2g=?=,

I think it would be more useful if you would answer the original poster, who
has the problem, rather than my post. It's not certain the person who needs
help will see the information you offer if it's posted as an answer to my
replies...
in fact late binding is a prefered way (performance !!) in OFFICE (only in
case of OFFICE )
I've read something along those lines, but always in reference to automating
from something like C++ or .NET. Are you certain this is also the case when
automating Office from within another Office application?

When VBA was first introduced into Office, we were explicitly told
early-binding was more efficient. It is difficult to break ingrained habits
:)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top