The remote server machine does not exist or is unavailable (Error 462)

V

Vítor

Hi! I'm a newbie at VBA and I'm having some difficulty to solve a bug
in my code. I've read many web forums about this error, but I just
can't fix it. So I'm asking for help...

PS: I'm portuguese. That's why there's some strange words...

***************Start Code***************************
Option Compare Database
Public oWord As Word.Application
Public oDoc As Word.Document
Public oRange As Word.Range
Public oSelection As Word.Selection
Public intLocalID As Integer
------------------------------------------------------------------------------------
Private Sub cmdExportarParaWord_Click()
'On Error GoTo cmdExportarParaWord_Click_ErrorHandler
Dim Response


Response = MsgBox("Tem a certeza que deseja exportar os dados sobre" &
vbCrLf & "formação para um novo documento do MS Word?", vbYesNo,
"Exportar dados para o MS Office Word...")
If Response = vbYes Then

Set oWord = CreateObject("Word.Application")
With oWord
.Visible = True
.Activate
.WindowState = wdWindowStateNormal
End With
Set oDoc = oWord.Documents.Add
Set oSelection = oWord.Selection
oWord.Windows(1).View.Type = wdNormalView

'Exportar dados da formação profissional como formando
oSelection.TypeText Text:="Formação profissional" & vbCrLf & "Como
Formando" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoProfComoFormando")

'Exportar dados da formação profissional como formador
oSelection.TypeText Text:="Formação profissional" & vbCrLf & "Como
Formador" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoProfComoFormador")

'Exportar dados da formação em serviço como formando
oSelection.TypeText Text:="Formação em serviço" & vbCrLf & "Como
Formando" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoServComoFormando")

'Exportar dados da formação em serviço como formador
oSelection.TypeText Text:="Formação em serviço" & vbCrLf & "Como
Formador" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoServComoFormador")
Else
Exit Sub
End If

Set oRange = Nothing
oDoc.Close
Set oDoc = Nothing
oWord.Quit
Set oWord = Nothing

cmdExportarParaWord_Click_ErrorHandler:
Exit Sub

End Sub
------------------------------------------------------------------------------------
Function ExportarDadosParaTabela(strRecordSet As String)
'On Error GoTo ExportarDadosParaTabela_ErrorHandler

Dim tbl As Word.Table
Dim db As DAO.Database
Dim rstFormação As DAO.Recordset
Dim rst_datas As DAO.Recordset
Dim fld0, fld1, fld2, fld3, fld4, fld1_datas, fld2_datas As Field
Dim strTítulo As String
Dim StrOrganização As String
Dim strLocal As String
Dim strDuração As String
Dim strData As String
Dim i As Integer
Dim strDatas As String

Set db = CurrentDb()
Set rstFormação = db.OpenRecordset(strRecordSet)
Set rst_datas = db.OpenRecordset("_datas")

With rstFormação
Set fld0 = .Fields(0)
Set fld1 = .Fields(1)
Set fld2 = .Fields(2)
Set fld3 = .Fields(3)
Set fld4 = .Fields(4)
End With

With rst_datas
Set fld1_datas = .Fields(1)
Set fld2_datas = .Fields(2)
End With

strTítulo = Nz(fld1.Value, "")
StrOrganização = Nz(fld2.Value, "")
strLocal = Nz(fld3.Value, "")
strDuração = Nz(fld4.Value, "")
strData = Nz(fld2_datas.Value, "")

Do Until rstFormação.EOF

Set oRange = oWord.ActiveDocument.Range(Start:=Selection.End,
End:=Selection.End)
Set tbl = oDoc.Tables.Add(oRange, 5, 2, , wdAutoFitContent)

tbl.Cell(1, 1).Range.Text = "Título"
tbl.Cell(1, 2).Range.Text = strTítulo

tbl.Cell(2, 1).Range.Text = "Organização"
tbl.Cell(2, 2).Range.Text = StrOrganização

tbl.Cell(3, 1).Range.Text = "Local"
tbl.Cell(3, 2).Range.Text = strLocal

tbl.Cell(4, 1).Range.Text = "Duração"
tbl.Cell(4, 2).Range.Text = strDuração

tbl.Cell(5, 1).Range.Text = "Data"

strDatas = ""

Do Until rst_datas.EOF

If fld1_datas.Value = fld0.Value Then
strDatas = strDatas & strData
i = 1 + i
If i = 1 Then
tbl.Cell(5, 2).Range.Text = strDatas
strDatas = strDatas & "; "
Else
tbl.Cell(5, 2).Range.Text = strDatas
End If
End If
i = 0
rst_datas.MoveNext

Loop

rst_datas.MoveFirst
rstFormação.MoveNext

oSelection.EndKey Unit:=wdStory
oSelection.TypeText Text:=vbCrLf

Loop

Set tbl = Nothing

Exit Function
********************End code********************
 
V

Vítor

Sorry, I've forgotten to mention that the error occurs here:

Set oRange = oWord.ActiveDocument.Range(Start:=Selection.End,
End:=Selection.End)


I've already tried this:

Set oRange = oDocRange(Start:=Selection.End, End:=Selection.End)


But I get the error too. As you may already imagine, this just happens
the second time this code is executed. The first it's OK.
 
V

Vítor

Finally I got it!!!!

This is the final code:

******************************CODE START******************************
Private Sub cmdExportarParaWord_Click()
On Error GoTo cmdExportarParaWord_Click_ErrorHandler
Dim Response

Response = MsgBox("Tem a certeza que deseja exportar os dados sobre" &
vbCrLf & "formação para um novo documento do MS Word?", vbYesNo,
"Exportar dados para o MS Office Word...")
If Response = vbYes Then
Set oWord = CreateObject("Word.Application")
With oWord
.Visible = True
.Activate
.WindowState = wdWindowStateNormal
End With
Set oDoc = oWord.Documents.Add
Set oSelection = oWord.Selection
oWord.Windows(1).View.Type = wdNormalView

'Exportar dados da formação profissional como formando
oSelection.TypeText Text:="Formação profissional" & vbCrLf & "Como
Formando" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoProfComoFormando")

'Exportar dados da formação profissional como formador
oSelection.TypeText Text:="Formação profissional" & vbCrLf & "Como
Formador" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoProfComoFormador")

'Exportar dados da formação em serviço como formando
oSelection.TypeText Text:="Formação em serviço" & vbCrLf & "Como
Formando" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoServComoFormando")

'Exportar dados da formação em serviço como formador
oSelection.TypeText Text:="Formação em serviço" & vbCrLf & "Como
Formador" & vbCrLf & vbCrLf
Call ExportarDadosParaTabela("qryFormaçãoServComoFormador")
Else
Exit Sub
End If

Set oRange = Nothing
oDoc.Close
Set oDoc = Nothing
oWord.Quit
Set oWord = Nothing

cmdExportarParaWord_Click_ErrorHandler:
MsgBox Err.Number
Exit Function

End Sub
--------------------------------------------------------------------------------------------------------------
Function ExportarDadosParaTabela(strRecordSet As String)
On Error GoTo ExportarDadosParaTabela_ErrorHandler

Dim tbl As Word.Table
Dim db As DAO.Database
Dim rstFormação As DAO.Recordset
Dim rst_datas As DAO.Recordset
Dim fld0, fld1, fld2, fld3, fld4, fld1_datas, fld2_datas As Field
Dim strTítulo As String
Dim StrOrganização As String
Dim strLocal As String
Dim strDuração As String
Dim strData As String
Dim i As Integer
Dim strDatas As String

Set db = CurrentDb()
Set rstFormação = db.OpenRecordset(strRecordSet)
Set rst_datas = db.OpenRecordset("_datas")

With rstFormação
Set fld0 = .Fields(0)
Set fld1 = .Fields(1)
Set fld2 = .Fields(2)
Set fld3 = .Fields(3)
Set fld4 = .Fields(4)
End With

With rst_datas
Set fld1_datas = .Fields(1)
Set fld2_datas = .Fields(2)
End With

strTítulo = Nz(fld1.Value, "")
StrOrganização = Nz(fld2.Value, "")
strLocal = Nz(fld3.Value, "")
strDuração = Nz(fld4.Value, "")
strData = Nz(fld2_datas.Value, "")

Do Until rstFormação.EOF

'NEXT TWO LINES SOLVED MY PROBLEM

Set oRange = oDoc.Content
oRange.Collapse Direction:=wdCollapseEnd


Set tbl = oDoc.Tables.Add(oRange, 5, 2, , wdAutoFitContent)

tbl.Cell(1, 1).Range.Text = "Título"
tbl.Cell(1, 2).Range.Text = strTítulo

tbl.Cell(2, 1).Range.Text = "Organização"
tbl.Cell(2, 2).Range.Text = StrOrganização

tbl.Cell(3, 1).Range.Text = "Local"
tbl.Cell(3, 2).Range.Text = strLocal

tbl.Cell(4, 1).Range.Text = "Duração"
tbl.Cell(4, 2).Range.Text = strDuração

tbl.Cell(5, 1).Range.Text = "Data"

strDatas = ""

Do Until rst_datas.EOF

If fld1_datas.Value = fld0.Value Then
strDatas = strDatas & strData
i = 1 + i
If i = 1 Then
tbl.Cell(5, 2).Range.Text = strDatas
strDatas = strDatas & "; "
Else
tbl.Cell(5, 2).Range.Text = strDatas
End If
End If
i = 0
rst_datas.MoveNext

Loop

rst_datas.MoveFirst
rstFormação.MoveNext

oSelection.EndKey Unit:=wdStory
oSelection.TypeText Text:=vbCrLf

Loop

Set tbl = Nothing

Exit Function
******************************CODE END******************************
 

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