Buongiorno a tutti, sono lieto di essere entrato a far parte di questo gruppo.
Colgo da subito l'occasione per chiedere il vostro aiuto su di un problema dal quale non riesco proprio ad uscire.
Vengo al dunque, ho creato un Database access per la compilazione automatica di modelli word direttamente da access, il tutto mi funziona correttamente solo che ho un piccolo problema, ovvero al primo lancio di compilazione automatica il tutto mi funge correttamente, ma dal secondo in poi mi dice: "Errore di run-time '462' il computer server remoto non esiste o non è disponibile." ma dopo aver cliccato fine e rilanciato la compilazione va tutto a buon fine, solo che poi si ripresente alla successiva compilazione. Credo che il problema stai o nella chiusura incompleta di word o del documento, o che comunque tenga in memoria qualcosa che va ad intaccare il funzionamento. di seguito posto il codice affinche qualcuno con più esperienza di me possa darmi la dritta che tanto ricerco. Grazie in anticipo con l'augurio di una buona giornata.
Private Sub Comando212_Click()
Dim Wrd As Word.Application
Dim Doc As Word.Document
Dim Rst As DAO.Recordset
Dim MODEL As String, NomeFile As String, i As Integer
Dim Openpath As String, Savepath As String
Dim Record As String, SQL As String
Dim Tbl As String * 1
Dim TotRiga As Currency, Totale As Currency
Dim ReplSel As Boolean
Openpath = "D:\automatic moo\VSE-MO-CQ\Modelli\MO_Automatizzate\"
MODEL = Openpath & "MO_generica.dot"
On Error Resume Next 'gestione errori step by step
'cerca un'istanza di Word già aperta
Set Wrd = GetObject(, "Word.Application")
If Err.Number = 429 Then
'se c'è stato un errore è perchè Word non era già aperto:
'aprilo adesso
Set Wrd = CreateObject("Word.Application")
End If
On Error GoTo 0 'ripristina la segnalazione degli errori
'rendi visibile la finestra di Word:
Wrd.Visible = True
'attiva Word e portalo in primo piano:
Wrd.Activate
'abilita l'opzione "Sostituisci la selezione". Se non fosse
'attiva, i campi modulo rimarrebbero all'interno del testo.
ReplSel = Wrd.Options.ReplaceSelection
Wrd.Options.ReplaceSelection = True
'Apri un nuovo documento basato sul modello:
Set Doc = Wrd.Documents.Add(MODEL)
'attivalo e portalo in primo piano:
Doc.Activate
Doc.Bookmarks("Descr_App").Select
Wrd.Selection.TypeText Me.DESCRIZIONE
If SIC <> "" Then
'seleziona il bookmark
Doc.Bookmarks("sic").Select
'e sovrascrivilo
Wrd.Selection.TypeText Format(Me.SIC, "00000")
End If
If MODELLO <> "" Then
Doc.Bookmarks("modello").Select
Wrd.Selection.TypeText Me.MODELLO
End If
If PRODUTTORE <> "" Then
Doc.Bookmarks("produttore").Select
Wrd.Selection.TypeText Me.PRODUTTORE
End If
If SERIAL_NUMBER <> "" Then
Doc.Bookmarks("serial").Select
Wrd.Selection.TypeText Me.SERIAL_NUMBER
End If
'If [Codice ASL] <> "" Then
'Doc.Bookmarks("cod_asl").Select
'Wrd.Selection.TypeText Me.[Codice ASL]
'End If
If PRESIDIO <> "" Then
Doc.Bookmarks("presidio").Select
Wrd.Selection.TypeText Me.PRESIDIO
End If
If Reparto_proprietà <> "" Then
Doc.Bookmarks("reparto").Select
Wrd.Selection.TypeText Me.Reparto_proprietà
End If
If TIPOLOGIA_COD <> "" Then
Doc.Bookmarks("TIPOLOGIA_COD").Select
Wrd.Selection.TypeText Me.TIPOLOGIA_COD
End If
If Piano <> "" Then
Doc.Bookmarks("Piano").Select
Wrd.Selection.TypeText Me.Piano
End If
If [1_62] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_1_62"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [1_62] = "no" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_1_62"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
End If
End If
If [13_1] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_13_1"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [13_1] = "no" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_13_1"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [13_1] = "N.A." Then
Selection.GoTo What:=wdGoToBookmark, Name:="Na_13_1"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
End If
End If
End If
If [13_2] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_13_2"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [13_2] = "no" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_13_2"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [13_2] = "N.A." Then
Selection.GoTo What:=wdGoToBookmark, Name:="Na_13_2"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
End If
End If
End If
If [13_3] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_13_3"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [13_3] = "No" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_13_3"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
End If
End If
If [13_4] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_13_4"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [13_4] = "No" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_13_4"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
End If
End If
If [10_32] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_10_32"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [10_32] = "no" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_10_32"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [10_32] = "N.A." Then
Selection.GoTo What:=wdGoToBookmark, Name:="Na_10_32"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
End If
End If
End If
If [10_33] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_10_33"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [10_33] = "no" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_10_33"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [10_33] = "N.A." Then
Selection.GoTo What:=wdGoToBookmark, Name:="Na_10_33"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
End If
End If
End If
If [8_155] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_8_155"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [8_155] = "No" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_8_155"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
End If
End If
If [8_157] = "Si" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Si_8_157"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
Else
If [8_157] = "No" Then
Selection.GoTo What:=wdGoToBookmark, Name:="No_8_157"
With Wrd.Selection.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
With Selection.Borders(wdBorderDiagonalDown)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
With Selection.Borders(wdBorderDiagonalUp)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
End If
End If
If [Ricambi1] <> "" Then
Doc.Bookmarks("ricambi1").Select
Wrd.Selection.TypeText Me.[Ricambi1]
End If
If [Ricambi2] <> "" Then
Doc.Bookmarks("ricambi2").Select
Wrd.Selection.TypeText Me.[Ricambi2]
End If
If [Ricambi3] <> "" Then
Doc.Bookmarks("ricambi3").Select
Wrd.Selection.TypeText Me.[Ricambi3]
End If
If [Q_Ricambi1] <> "" Then
Doc.Bookmarks("Q_Ricambi1").Select
Wrd.Selection.TypeText Me.[Q_Ricambi1]
End If
If [Q_Ricambi2] <> "" Then
Doc.Bookmarks("Q_Ricambi2").Select
Wrd.Selection.TypeText Me.[Q_Ricambi2]
End If
If [Q_Ricambi3] <> "" Then
Doc.Bookmarks("Q_Ricambi3").Select
Wrd.Selection.TypeText Me.[Q_Ricambi3]
End If
If [Usura1] <> "" Then
Doc.Bookmarks("Usura1").Select
Wrd.Selection.TypeText Me.[Usura1]
End If
If [Usura2] <> "" Then
Doc.Bookmarks("Usura2").Select
Wrd.Selection.TypeText Me.[Usura2]
End If
If [Usura3] <> "" Then
Doc.Bookmarks("Usura3").Select
Wrd.Selection.TypeText Me.[Usura3]
End If
If [Q_Usura1] <> "" Then
Doc.Bookmarks("Q_Usura1").Select
Wrd.Selection.TypeText Me.[Q_Usura1]
End If
If [Q_Usura2] <> "" Then
Doc.Bookmarks("Q_Usura2").Select
Wrd.Selection.TypeText Me.[Q_Usura2]
End If
If [Q_Usura3] <> "" Then
Doc.Bookmarks("Q_Usura3").Select
Wrd.Selection.TypeText Me.[Q_Usura3]
End If
If [Note] <> "" Then
Doc.Bookmarks("Note").Select
Wrd.Selection.TypeText Me.[Note]
End If
If [Esito] <> "" Then
If [Esito] = "Positivo" Then
Doc.Bookmarks("Positivo").Select
Wrd.Selection.TypeText ("X")
Else
Doc.Bookmarks("Negativo").Select
Wrd.Selection.TypeText ("X")
End If
Else
End If
If [Non Conformità] <> "" Then
Doc.Bookmarks("Non_Conf").Select
Wrd.Selection.TypeText Me.[Non Conformità]
End If
Doc.Bookmarks("Tecnico").Select
Wrd.Selection.TypeText Me.[Tecnico]
If [Data] <> "" Then
Doc.Bookmarks("Data").Select
Wrd.Selection.TypeText Me.[Data]
End If
If [Periodicita] <> "" Then
Doc.Bookmarks("Periodicità").Select
Wrd.Selection.TypeText Me.[Periodicita]
End If
If [T_esec] <> "" Then
Doc.Bookmarks("T_Esec").Select
Wrd.Selection.TypeText Me.[T_esec]
End If
'ripristina il valore originario dell'opzione
'sostituisci la selezione"
Wrd.Options.ReplaceSelection = False
ReplSel = Wrd.Options.ReplaceSelection
''Per spostare il cursore all'inizio del documento
Wrd.Selection.HomeKey Unit:=wdStory
'avvisa l'utente che l'esportazione è terminata
'Wrd.Application.WordBasic.MsgBox "Esportazione terminata", "Esportazione dati da Access"
Wrd.Application.WordBasic.MsgBox "Esportazione terminata", "Esportazione dati da Access"
' Finestra di stampa
Wrd.Dialogs(wdDialogFilePrint).Show
NomeFile = Format(Me.SIC, "0000") & "_GENERICA.doc"
If [Esito] = "Positivo" Then
Nomepath = "C:\VSE-MO-CQ\MO\2011\Positivi\"
Else
Nomepath = "C:\VSE-MO-CQ\MO\2011\Negativi\"
End If
Doc.SaveAs Nomepath & NomeFile
Wrd.Application.WordBasic.MsgBox "Salvataggio documento OK", "Salva documento da Access"
''per CHIUDERE il documento:
''(se il documento non è già stato salvato, in Word
''comparirà la finestra "Salva con nome")
On Error Resume Next 'se l'utente annulla il salvataggio si genera un errore
Wrd.Quit
On Error GoTo 0
'azzera le variabili oggetto
Set Doc = Nothing
Set Wrd = Nothing
'NB: L'azzeramento degli oggetti Doc e Wrd senza la loro
'preventiva chiusura (Doc.Close e Wrd.Quit)
'fa sì che dopo l'esportazione dei dati
'Word e il documento rimangano aperti
End Sub
P.S. Se ho sbagliato qualcosa chiedo preventivamente scusa ai moderatori.