我需要從Excel中獲取名稱列表,並將它們插入到Word文檔中,爲每個名稱打印一個文檔。該文件有一些文本和一個名爲「名稱」的書籤。代碼如下。關於Word宏的幾個問題
首先,我想知道是否有可能在Excel電子表格中檢測名稱列表的多長時間並獲取該名稱,而不是硬編碼該數字。
其次,我想不出如何刪除我已經放在文檔中的文本。當我在書籤中插入文本時,它會在書籤後追加,因此如果我不斷添加名稱,它們將全部疊加在一起。
也許與代碼,這將是更清楚:
Sub insertar_nombre()
Dim Excel As Excel.Application
Dim Planilla As Excel.Workbook
Dim Hoja As Excel.Worksheet
Set Excel = CreateObject("Excel.Application")
Dim Filename As String
Dim fname As Variant
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Title = "Seleccionar Documento de Excel"
.Show
For Each fname In .SelectedItems
Filename = fname
Next
End With
Set Planilla = Excel.Workbooks.Open(Filename)
Set Hoja = Planilla.Worksheets(1)
Dim Nombre As String
For Count = 2 To 10
Nombre = Hoja.Cells(Count, 1).Value
ActiveDocument.Bookmarks("name").Range.Text = Nombre
ActiveDocument.PrintOut
Next
End Sub
原諒我,如果這個代碼顯然是錯誤什麼的,我只是這個開始。
我知道這一點,我只是想做這個手動的方式來獲得樂趣。 – Javier