I have this code on a command button that checks for a Word document on a networked computer that serves the Database. If the document is found, it brings it up. If it's not found, it opens a template, fills in the bookmarks with datbase info, and saves it on the database server.
The problem I am having is I occasionally get an error stating: "The remote server machine does not exist or is unavailable" after it creates a new document and tries to save it. This does not happen right after they open the database and press the button, but if they press it again in the same session, they get the error. Can you guys figure out what's wrong?
Private Sub cmdReport_Click()
'Checks in recommendations directory if a recommendation already
'exists. If not, it creates a new document based on the recommendation
'template. It saves/opens based on the case number (i.e. 2347.doc)
On Error GoTo Err_cmdReport_Click
Dim appWord As New Word.Application
Dim CaseNumber
Dim strCourtDate As String
Dim strDept As String
Dim strFirst As String
Dim strMiddle As String
Dim strLast As String
Dim strDOB As String
Dim strCaseNum1 As String
Dim strCaseNum2 As String
Dim strCaseNum3 As String
Dim strAttorney As String
Dim strASW As String
txtCase.SetFocus
CaseNumber = txtCase.Text
'If document doesn't exist, generate new report.
If Dir("//ALTSP1/Database/Recommendations/" & CaseNumber & ".doc"
= vbNullString Then
MsgBox "Report doesn't exist, click OK to generate new report"
appWord.Visible = True
appWord.Documents.Add "//ALTSP1/Database/Recommendation.dot"
'Put the Database information into variables
Me.txtCourt.SetFocus
strCourtDate = txtCourt.Text
Me.txtDept.SetFocus
strDept = txtDept.Text
Me.txtFirst.SetFocus
strFirst = txtFirst.Text
Me.txtMiddle.SetFocus
strMiddle = txtMiddle.Text
Me.txtLast.SetFocus
strLast = txtLast.Text
Me.txtDOB.SetFocus
strDOB = txtDOB.Text
Me.txtCourtCase1.SetFocus
strCaseNum1 = txtCourtCase1.Text
Me.txtCourtCase2.SetFocus
strCaseNum2 = txtCourtCase2.Text
Me.txtCourtCase3.SetFocus
strCaseNum3 = txtCourtCase3.Text
Me.cmbAttorney.SetFocus
strAttorney = cmbAttorney.Text
Me.cmbCaseworker.SetFocus
strASW = cmbCaseworker.Text
'Paste into Template's bookmarks
appWord.Application.Visible = True
ActiveDocument.Bookmarks("CourtDate"
.Select
appWord.Selection.Text = strCourtDate
ActiveDocument.Bookmarks("Dept"
.Select
appWord.Selection.Text = strDept
ActiveDocument.Bookmarks("First"
.Select
appWord.Selection.Text = strFirst
ActiveDocument.Bookmarks("Middle"
.Select
appWord.Selection.Text = strMiddle
ActiveDocument.Bookmarks("Last"
.Select
appWord.Selection.Text = strLast
ActiveDocument.Bookmarks("DOB"
.Select
appWord.Selection.Text = strDOB
ActiveDocument.Bookmarks("Case1"
.Select
appWord.Selection.Text = strCaseNum1
ActiveDocument.Bookmarks("Case2"
.Select
appWord.Selection.Text = strCaseNum2
ActiveDocument.Bookmarks("Case3"
.Select
appWord.Selection.Text = strCaseNum3
ActiveDocument.Bookmarks("Attorney"
.Select
appWord.Selection.Text = strAttorney
ActiveDocument.Bookmarks("ASW"
.Select
appWord.Selection.Text = strASW
appWord.Application.Visible = True
appWord.ActiveDocument.SaveAs "//ALTSP1/Database/Recommendations/" & CaseNumber + ".doc"
Me.txtReport.SetFocus
txtReport.Text = Date
Else
MsgBox "Report exists, click OK to open file"
appWord.Visible = True
appWord.Documents.Open "//ALTSP1/Database/Recommendations/" & CaseNumber & ".doc"
End If
On Error Resume Next
docObject.UserControl = True
Exit_cmdReport_Click:
Exit Sub
Err_cmdReport_Click:
MsgBox Err.Description
Resume Exit_cmdReport_Click
End Sub
The problem I am having is I occasionally get an error stating: "The remote server machine does not exist or is unavailable" after it creates a new document and tries to save it. This does not happen right after they open the database and press the button, but if they press it again in the same session, they get the error. Can you guys figure out what's wrong?
Private Sub cmdReport_Click()
'Checks in recommendations directory if a recommendation already
'exists. If not, it creates a new document based on the recommendation
'template. It saves/opens based on the case number (i.e. 2347.doc)
On Error GoTo Err_cmdReport_Click
Dim appWord As New Word.Application
Dim CaseNumber
Dim strCourtDate As String
Dim strDept As String
Dim strFirst As String
Dim strMiddle As String
Dim strLast As String
Dim strDOB As String
Dim strCaseNum1 As String
Dim strCaseNum2 As String
Dim strCaseNum3 As String
Dim strAttorney As String
Dim strASW As String
txtCase.SetFocus
CaseNumber = txtCase.Text
'If document doesn't exist, generate new report.
If Dir("//ALTSP1/Database/Recommendations/" & CaseNumber & ".doc"
MsgBox "Report doesn't exist, click OK to generate new report"
appWord.Visible = True
appWord.Documents.Add "//ALTSP1/Database/Recommendation.dot"
'Put the Database information into variables
Me.txtCourt.SetFocus
strCourtDate = txtCourt.Text
Me.txtDept.SetFocus
strDept = txtDept.Text
Me.txtFirst.SetFocus
strFirst = txtFirst.Text
Me.txtMiddle.SetFocus
strMiddle = txtMiddle.Text
Me.txtLast.SetFocus
strLast = txtLast.Text
Me.txtDOB.SetFocus
strDOB = txtDOB.Text
Me.txtCourtCase1.SetFocus
strCaseNum1 = txtCourtCase1.Text
Me.txtCourtCase2.SetFocus
strCaseNum2 = txtCourtCase2.Text
Me.txtCourtCase3.SetFocus
strCaseNum3 = txtCourtCase3.Text
Me.cmbAttorney.SetFocus
strAttorney = cmbAttorney.Text
Me.cmbCaseworker.SetFocus
strASW = cmbCaseworker.Text
'Paste into Template's bookmarks
appWord.Application.Visible = True
ActiveDocument.Bookmarks("CourtDate"
appWord.Selection.Text = strCourtDate
ActiveDocument.Bookmarks("Dept"
appWord.Selection.Text = strDept
ActiveDocument.Bookmarks("First"
appWord.Selection.Text = strFirst
ActiveDocument.Bookmarks("Middle"
appWord.Selection.Text = strMiddle
ActiveDocument.Bookmarks("Last"
appWord.Selection.Text = strLast
ActiveDocument.Bookmarks("DOB"
appWord.Selection.Text = strDOB
ActiveDocument.Bookmarks("Case1"
appWord.Selection.Text = strCaseNum1
ActiveDocument.Bookmarks("Case2"
appWord.Selection.Text = strCaseNum2
ActiveDocument.Bookmarks("Case3"
appWord.Selection.Text = strCaseNum3
ActiveDocument.Bookmarks("Attorney"
appWord.Selection.Text = strAttorney
ActiveDocument.Bookmarks("ASW"
appWord.Selection.Text = strASW
appWord.Application.Visible = True
appWord.ActiveDocument.SaveAs "//ALTSP1/Database/Recommendations/" & CaseNumber + ".doc"
Me.txtReport.SetFocus
txtReport.Text = Date
Else
MsgBox "Report exists, click OK to open file"
appWord.Visible = True
appWord.Documents.Open "//ALTSP1/Database/Recommendations/" & CaseNumber & ".doc"
End If
On Error Resume Next
docObject.UserControl = True
Exit_cmdReport_Click:
Exit Sub
Err_cmdReport_Click:
MsgBox Err.Description
Resume Exit_cmdReport_Click
End Sub