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

cmd button to either create new word, or open existing 1

Status
Not open for further replies.

upplepop

IS-IT--Management
Jun 1, 2002
173
US
I need a command button that will either create a new Word document based on a template (recommendation.dot) and save it as the primary key (ie. "7831.doc") or open the file if it was already created. Can anyone get me started or give me some hints.


Here's what I have so far:

Dim docObject As Object
Set docObject = CreateObject("word.application")
docObject.Visible = True
Set Document = docObject.Documents.Add _
("C:/Database/Recommendation.dot")
txtCase.SetFocus
docObject.SaveAs (txtCase.Text + ".doc")



Thanks a lot.
 
Try opening the file for read-only access using VBA. If that fails, then the file doesn't exist and you'll have to create it first. If it succeeds, close the file, and open it in Word.
 
How about checking to see if the file exists first.
In this case txtFileName is the filename without the extension

Dim appWord As New Word.Application

If Dir("C:\Database\" & txtFileName & ".txt") = vbNullString Then
MsgBox "Doesn't Exist"
appWord.Visible = True
appWord.Documents.Add "C:\Database\Recommendation.dot"
Else

MsgBox "Exists"
appWord.Visible = True
appWord.Documents.Open "C:\Database\" & txtFileName & ".doc"

End If

ps. in your path name the / should be
Good luck

 
I tried using the above code, but I'm getting a Got Focus error. How to I give focus to this line:

If Dir("C:\Database\" & txtCase.Text & ".doc") = vbNullString Then
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top