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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

when creating a new word document, it doesn't (from access) 1

Status
Not open for further replies.

easycode

Programmer
Jan 28, 2005
195
0
0
US
Hello all,

I am trying to open an existing word document in access that works fine, but then if the word document does not exist i want to create a word document with a name assigned in a field called instructiosnID. The problem is that when the files does not exist and try to create a new one does not create it what it does is only opens words with no document at all, then if am trying to save with the name in Instructions ID it gives me an error saying that there is no active document. All i want is when open word also creates the blank word document with a name "1234.doc". Thank you very much in advance

Here is the code:


Private Sub cmdowd_Click() 'Open Word Document
On Error GoTo handlerror
Dim objWord As Object, objWord2 As Object
Dim ffile As String
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
ffile = Application.CurrentProject.Path & "\" & Me.InstructionID & ".doc"
objWord.documents.Open ffile
objWord.Visible = True

exiterror:
Exit Sub

handlerror:
If Err.Number = 5174 Then 'if word document does not exist, create one with the name in InstructionsID
Set objWord2 = New Word.Application
objWord2.Visible = True
objWord2.ActiveDocument.SaveAs Application.CurrentProject.Path & "\" & Me.InstructionID & ".doc"
Else
MsgBox Err.Description
End If
Resume exiterror
End Sub
 
A starting point:
Private Sub cmdowd_Click()
Dim objWord As Object
Dim ffile As String
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
ffile = Application.CurrentProject.Path & "\" & Me.InstructionID & ".doc"
If Dir(ffile) <> "" Then
objWord.Documents.Open ffile
Else
objWord.Documents.Add
objWord.ActiveDocument.SaveAs ffile
End If
Set objWord = Nothing
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How about:

Code:
ffile = Application.CurrentProject.Path & "\" & Me.InstructionID & ".doc"

If Dir(ffile)<>"" Then
   FollowHyperlink ffile
Else
   Set objWord = CreateObject("Word.Application")
   objWord.Visible = True

   objWord.Documents.Add
   With objWord.ActiveDocument
      .SaveAs ffile
   End With
End If
 
Thank you very much to both of you.

PHV that is exactly what i wanted. Thanks (here is your start)

REMOU the solution PHV gave is what i was looking for so i did not bother trying yours. But i appreciate your help. Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top