Ich will beim Austauschen einer Excel-Tabelle (Notes-Dateianhang) benutzerdefinierte Tabellenblätter in die neue Vorlage hinüberkopieren:
'check for user defined worksheets in original template
c = excelobj_o.Workbooks(1).Worksheets.Count
For x = 1 To c
wsn$=excelobj_o.Workbooks(1).Worksheets(x).Name
If wsn$ <> "Performance Analysis" and wsn$ <> "Reportinfos" And wsn$ <> "PA-Export" And wsn$ <> "Translations" Then
Set xlsheet_o = excelobj_o.Workbooks(1).Worksheets(wsn$)
Print "copying user defined worksheet " & wsn$
Set lastsheet = excelobj_n.ActiveWorkbook.Worksheets(excelobj_n.ActiveWorkbook.Worksheets.Count)
Set xlsheet_o = excelobj_o.Workbooks(1).Worksheets(wsn$)
Call xlsheet_o.Copy(Null,lastsheet)
Call excelobj_n.ActiveWorkbook.Save
MsgBox excelobj_n.ActiveWorkbook.Worksheets.Count
End If
Next
Tut aber nix. Die MsgBox gibt für die neue Tabelle die Anzahl der bereits vordefinierten Arbeitsblätter zurück; das Arbeitsblatt xlsheet_o wird nicht kopiert (Zuweisung passt - xlsheet_o.Name gibt korrekt den Namen zurück).
Bin ein wenig ratlos...
Ich habe jetzt kein Excel da um das zu testen, aber wenn Du beide Templates auf der Platte hast, dann würde ich das wohl in etwa so probieren
tempDir$ = Environ("Temp")
set xlwbTemplateOld_o = excelobj_o.Workbooks.Open(tempDir$ & "\Template_Old.xlt")
set xlwbTemplateNew_o = excelobj_o.Workbooks.Open(tempDir$ & "\"Template_New.xlt")
orginalSheetNames(0) = "Performance Analysis"
orginalSheetNames(1) = "Reportinfos"
orginalSheetNames(2) = "PA-Export"
orginalSheetNames(3) = "Translations"
countOld = xlwbTemplateOld_o.Worksheets.Count
countNew = xlwbTemplateNew_o.Worksheets.Count
Print "count (before): ", Cstr(countNew)
doSave = False
For index = 1 To countOLD
wsn$ = xlwbTemplateOld_o.Worksheets(index).Name
If IsNull(Arraygetindex(orginalSheetNames, wsn$)) Then
Set xlsheet_o = xlwbTemplateOld_o.Worksheets(wsn$)
Print "copying user defined worksheet " & wsn$
Set lastsheet_o = xlwbTemplateNew_o.Worksheets(countNew)
Call xlsheet_o.Copy(Null,lastsheet_o)
doSave = True
countNew = xlwbTemplateNew_o.Worksheets.Count
Print "count (current): ", Cstr(countNew)
End If
Next index
Print "count (after): ", Cstr(countNew)
If doSave = True Then
Call xlwbTemplateNew_o.Save
End If