Overførsel fra Excel til Word giver fejl
Jeg prøver lige igen!Jeg har en makro, der laver et array ud fra et regneark. Dette array overføres til Word.
Makroen fungerer uden problemer første gang den køres. Men, 2. gang makroen køres kommer følgende fejl:
"Run-time error '462':
The remote server machine does not exist or is unavailable"
Fejlen opstår i denne linie af koden:
.ActiveDocument.PageSetup.TopMargin = CentimetersToPoints(2)
En yderligere "krølle" på problemet er, at fejlen kun opstår når Excel 2000 bruges. I Excel 2003 opstår problemet ikke.
Er der nogen der ved hvorfor fejlen opstår og evt. har en løsning herpå??
Makroen er gengivet i hele sin længde nedenfor
Sub TilWord2()
Dim UArray, UOverArray, UTotalerArray As Variant
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim x, y As Integer
' skriv diverse ranges til arrays
Sheets("SAMBESKATNINGSINDKOMST").Select
UArray = Range("A6:M306")
UOverArray = Range("A2:M2")
UTotalerArray = Range("A4:M4")
' Opret et midlertidigt ark og kopier arrays hertil
Application.DisplayAlerts = False
Application.ScreenUpdating = False
' Tjek om arket findes
On Error Resume Next
Set wSheet = Sheets("MIDL")
If wSheet Is Nothing Then
Set wSheet = Nothing
On Error GoTo 0
Else
Sheets("MIDL").Delete
Set wSheet = Nothing
On Error GoTo 0
End If
Sheets.Add.Name = "MIDL"
Sheets("MIDL").Select
Range("A1").Select '1. kolonne
ActiveCell.FormulaR1C1 = "Nr."
Range("B1").Select '2. kolonne
ActiveCell.FormulaR1C1 = "Selskabets navn"
Range("B2").Select
ActiveCell.FormulaR1C1 = "-"
Range("C1").Select '3. kolonne
ActiveCell.FormulaR1C1 = "Skattepligtig"
Range("C2").Select
ActiveCell.FormulaR1C1 = "indkomst"
Range("D1").Select '4. kolonne
ActiveCell.FormulaR1C1 = "Fremført sær-"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Skattepligtig" ' 5
Range("F1").Select
ActiveCell.FormulaR1C1 = "Fremført egne" ' 6
Range("G1").Select
ActiveCell.FormulaR1C1 = "Skattepligtig" ' 7
Range("J1").Select
ActiveCell.FormulaR1C1 = "Fordeling af" ' 8
Range("K1").Select
ActiveCell.FormulaR1C1 = "Skattepligtig" ' 9
Range("L1").Select
ActiveCell.FormulaR1C1 = "Fremført andre" ' 10
Range("M1").Select
ActiveCell.FormulaR1C1 = "Sambeskatnings-" ' 11
Range("M2").Select
ActiveCell.FormulaR1C1 = "indkomst"
Range("L2").Select
ActiveCell.FormulaR1C1 = "underskud"
Range("K2").Select
ActiveCell.FormulaR1C1 = "indk. herefter"
Range("J2").Select
ActiveCell.FormulaR1C1 = "årets indkomst"
Range("G2").Select
ActiveCell.FormulaR1C1 = "indk. herefter"
Range("F2").Select
ActiveCell.FormulaR1C1 = "underskud"
Range("E2").Select
ActiveCell.FormulaR1C1 = "indk. herefter"
Range("D2").Select
ActiveCell.FormulaR1C1 = "underskud"
Range("A3:M3") = UTotalerArray
Range("A4:M304") = UArray
UArray = Range("A1:M302")
' start Word og opret et dokument
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
With wrdApp
.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
.ActiveDocument.PageSetup.TopMargin = CentimetersToPoints(2)
.ActiveDocument.PageSetup.BottomMargin = CentimetersToPoints(2)
.ActiveDocument.PageSetup.LeftMargin = CentimetersToPoints(1.25)
.ActiveDocument.PageSetup.RightMargin = CentimetersToPoints(2)
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(0.5), Alignment:=wdAlignTabLeft
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(6.5), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(9), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(11.5), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(14), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(16.5), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(19), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(21.5), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(24), Alignment:=wdAlignTabRight
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(26.5), Alignment:=wdAlignTabRight
.Selection.Font.Name = "Times New Roman"
.Selection.Font.Size = 10
End With
x = 1
' Skriv data i array'et til Word dokumentet
With wrdDoc
Do While UArray(x, 2) <> ""
For y = 1 To 13 '13 kolonner
If y = 8 Then
y = 10
End If
.Content.InsertAfter "" & UArray(x, y)
wrdApp.Selection.MoveRight Unit:=wdCharacter, Count:=Len(UArray(x, y)) - p
wrdApp.Selection.TypeText Text:=vbTab
p = p - 1
Next y
.Content.InsertParagraphAfter
x = x + 1
Loop
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
Sheets("MIDL").Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
