Hi Everybody,
I am having a problem when formatting Excel and Word documents utilizing VBA. Everything works great for the first go round. For the second run of the process I get a run time error 462 "The remote server or machine is not available or does not exist". I have stepped through the process and checked my task manager to make sure that all instances of Word and Excel close when I finish the first go round (that was a fun task
). The program always stops at the same point of formatting the Word Document. I have colored the stopping point in red in the code below. The code is a little clunky, but I've been playing with it to allow for the closing of Excel in the Task Manager.
I can exit Access and restart the program and it works fine for the first go round, but not the second.
Any thoughts?
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set Xl = New Excel.Application
Set XlBook = GetObject(vFilePath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets(1)
XlSheet.Cells.Select
XlSheet.Cells.WrapText = True
XlSheet.Cells.Font.Name = "Calibri"
XlSheet.Cells.Font.Size = 10
XlSheet.Cells.EntireColumn.AutoFit
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Range("a1").Select
XlSheet.Range("a1").FormulaR1C1 = vWhatItem
XlSheet.Range("b1").Select
XlSheet.Range("B1").FormulaR1C1 = rs.Fields(0)
XlSheet.Range("a3").Select
XlSheet.Range("a3").FormulaR1C1 = rs2.Fields(1) + " Ext. " + rs2.Fields(4)
XlSheet.Range("a2").Select
XlSheet.Range("a2").FormulaR1C1 = "Sales Rep: " + Trim(vRep)
XlSheet.Range("a4").Select
XlSheet.Range("a4").FormulaR1C1 = "Current Mbase: $" + Trim(Str(vOldMbase))
XlSheet.Range("a5").Select
XlSheet.Range("a5").FormulaR1C1 = "New Mbase: $" + Trim(Str(vNewMbase))
Set rs3 = dbs.OpenRecordset("Select MT9270 from [MRC400MFG_SF09270] where IMPN = '" & vWhatItem & "'")
rs3.MoveLast
rs3.MoveFirst
vInventory = rs3.Fields(0) * 30
vEffectiveDate = Date + vInventory
vEffectiveDate = Format(vEffectiveDate, "mm/dd/yyyy")
XlSheet.Range("b5").Select
XlSheet.Range("b5").FormulaR1C1 = "Effective Date: " + Trim(Str(vEffectiveDate))
Xl.ActiveSheet.PageSetup.LeftMargin = 0.25
Xl.ActiveSheet.PageSetup.RightMargin = 0.25
Xl.ActiveSheet.PageSetup.PrintGridlines = True
XlSheet.Columns("A:A").ColumnWidth = 35.03
XlSheet.Columns("B:B").ColumnWidth = 35.03
XlSheet.Columns("C:C").ColumnWidth = 20.03
Do While vwhatline <= vRecCount
StrSql = Trim(Str(vwhatline * 2) + 7)
myrange = "a" + StrSql + ":C" + StrSql
XlSheet.Range(myrange).Select
XlSheet.Range(myrange).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
vwhatline = vwhatline + 1
Loop
XlSheet.Range("A1:c7").Select
XlSheet.Range("A1:c7").Font.Bold = True
XlSheet.Range("A1:c7").Font.Size = 12
XlSheet.Cells.Select
XlSheet.Cells.WrapText = False
XlSheet.Cells.WrapText = True
XlSheet.Cells.Copy
Dim objWord As Word.Document
Set objWord = GetObject(vECNFormPathNew & vECNFormNameNew, "Word.Document")
objWord.Application.Visible = True
objWord.Application.Activate
objWord.Application.ActiveDocument.Unprotect
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(1)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(2)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(3)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(4)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=vWhatItem
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs.Fields(0)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveDown Unit:=wdLine, Count:=5
objWord.Application.Selection.TypeText Text:="See Attached Form"
objWord.Application.Selection.EndKey Unit:=wdStory
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.ParagraphFormat.LineSpacing = LinesToPoints(1)
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.MoveUp Unit:=wdLine, Count:=2
objWord.Application.Selection.PasteExcelTable False, False, False
objWord.Save
objWord.Application.Quit
'objWord.Close
rs.Close
XlBook.Save
XlBook.Close
Xl.Application.Quit
Set Xl = Nothing
Set XlSheet = Nothing
Set XlBook = Nothing
Set objWord = Nothing
I am having a problem when formatting Excel and Word documents utilizing VBA. Everything works great for the first go round. For the second run of the process I get a run time error 462 "The remote server or machine is not available or does not exist". I have stepped through the process and checked my task manager to make sure that all instances of Word and Excel close when I finish the first go round (that was a fun task

I can exit Access and restart the program and it works fine for the first go round, but not the second.
Any thoughts?
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Set Xl = New Excel.Application
Set XlBook = GetObject(vFilePath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets(1)
XlSheet.Cells.Select
XlSheet.Cells.WrapText = True
XlSheet.Cells.Font.Name = "Calibri"
XlSheet.Cells.Font.Size = 10
XlSheet.Cells.EntireColumn.AutoFit
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
XlSheet.Range("a1").Select
XlSheet.Range("a1").FormulaR1C1 = vWhatItem
XlSheet.Range("b1").Select
XlSheet.Range("B1").FormulaR1C1 = rs.Fields(0)
XlSheet.Range("a3").Select
XlSheet.Range("a3").FormulaR1C1 = rs2.Fields(1) + " Ext. " + rs2.Fields(4)
XlSheet.Range("a2").Select
XlSheet.Range("a2").FormulaR1C1 = "Sales Rep: " + Trim(vRep)
XlSheet.Range("a4").Select
XlSheet.Range("a4").FormulaR1C1 = "Current Mbase: $" + Trim(Str(vOldMbase))
XlSheet.Range("a5").Select
XlSheet.Range("a5").FormulaR1C1 = "New Mbase: $" + Trim(Str(vNewMbase))
Set rs3 = dbs.OpenRecordset("Select MT9270 from [MRC400MFG_SF09270] where IMPN = '" & vWhatItem & "'")
rs3.MoveLast
rs3.MoveFirst
vInventory = rs3.Fields(0) * 30
vEffectiveDate = Date + vInventory
vEffectiveDate = Format(vEffectiveDate, "mm/dd/yyyy")
XlSheet.Range("b5").Select
XlSheet.Range("b5").FormulaR1C1 = "Effective Date: " + Trim(Str(vEffectiveDate))
Xl.ActiveSheet.PageSetup.LeftMargin = 0.25
Xl.ActiveSheet.PageSetup.RightMargin = 0.25
Xl.ActiveSheet.PageSetup.PrintGridlines = True
XlSheet.Columns("A:A").ColumnWidth = 35.03
XlSheet.Columns("B:B").ColumnWidth = 35.03
XlSheet.Columns("C:C").ColumnWidth = 20.03
Do While vwhatline <= vRecCount
StrSql = Trim(Str(vwhatline * 2) + 7)
myrange = "a" + StrSql + ":C" + StrSql
XlSheet.Range(myrange).Select
XlSheet.Range(myrange).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
vwhatline = vwhatline + 1
Loop
XlSheet.Range("A1:c7").Select
XlSheet.Range("A1:c7").Font.Bold = True
XlSheet.Range("A1:c7").Font.Size = 12
XlSheet.Cells.Select
XlSheet.Cells.WrapText = False
XlSheet.Cells.WrapText = True
XlSheet.Cells.Copy
Dim objWord As Word.Document
Set objWord = GetObject(vECNFormPathNew & vECNFormNameNew, "Word.Document")
objWord.Application.Visible = True
objWord.Application.Activate
objWord.Application.ActiveDocument.Unprotect
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(1)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(2)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(3)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs2.Fields(4)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=vWhatItem
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.TypeText Text:=rs.Fields(0)
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveRight Unit:=wdCell
objWord.Application.Selection.MoveDown Unit:=wdLine, Count:=5
objWord.Application.Selection.TypeText Text:="See Attached Form"
objWord.Application.Selection.EndKey Unit:=wdStory
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.ParagraphFormat.LineSpacing = LinesToPoints(1)
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.TypeParagraph
objWord.Application.Selection.MoveUp Unit:=wdLine, Count:=2
objWord.Application.Selection.PasteExcelTable False, False, False
objWord.Save
objWord.Application.Quit
'objWord.Close
rs.Close
XlBook.Save
XlBook.Close
Xl.Application.Quit
Set Xl = Nothing
Set XlSheet = Nothing
Set XlBook = Nothing
Set objWord = Nothing