关于Word宏的几个问题

发布于 2024-08-13 08:51:23 字数 1065 浏览 2 评论 0原文

我需要从 Excel 中获取姓名列表并将其插入到 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

请原谅我,如果这段代码明显错误或其他什么,我只是从这个开始。

I need to grab a list of names from Excel and insert them into a Word document, printing one document per name. The document has some text and a bookmark called "name". The code is below.

First, I want to know if it's possible to detect how long is the list of names in the Excel spreadsheet and grab that, instead of hardcoding the number.

Second, I can't figure out how to delete the text I already put inside the document. When I insert text in a bookmark, it gets appended after the bookmark, so if I keep adding names they all stack together.

Maybe with the code this will be clearer:

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

Forgive me if this code is obviously wrong or something, I'm just beginning with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-08-20 08:51:23

我需要从 Excel 中获取姓名列表并将其插入到 Word 文档中,为每个姓名打印一个文档。

为什么不直接使用邮件合并功能呢?

I need to grab a list of names from Excel and insert them into a Word document, printing one document per name.

Why don't you simply use the mail merge feature?

小傻瓜 2024-08-20 08:51:23

以下 Sub 应该可以为您解决此问题,但您可能需要更改书签的定义方式。

插入书签的方法不止一种。此方法要求通过突出显示文本来插入书签,而不是简单地将光标定位在文本中的某个位置。

Sub insertar_nombre()

Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet

Dim strFilename As String

Dim bkmName As Word.Range
Dim strBookmarkOriginalText As String

Dim lngRowLast As Long
Dim rngRowStart As Excel.Range
Dim rngRowEnd As Excel.Range

Dim rngNames As Excel.Range
Dim rngName As Excel.Range


'Open file dialog and only allow Excel files'
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Title = "Seleccionar Documento de Excel"

    'Only let them select Excel files'
    .Filters.Clear
    .Filters.Add "Excel Documents (*.xls)", "*.xls"

    'Check if a file is selected'
    If .Show = True Then

        'Since AllowMultiSelect is set to False, _
            only one file can be selected'
        strFilename = .SelectedItems(1)
    Else

        'No file selected, so exit the Sub'
        Exit Sub
    End If
End With


'Set the bookmark to a Word range (not a Bookmark object)'
Set bkmName = ActiveDocument.Bookmarks("name").Range

'Save the original text of the bookmark'
strBookmarkOriginalText = bkmName.Text


'Open the Excel file'
Set xlWorkbook = Excel.Workbooks.Open(strFilename)
Set xlWorksheet = xlWorkbook.Worksheets(1)

'Range of the first cell that contains a name'
Set rngRowStart = xlWorksheet.Cells(2, 1)

'Range of the last cell in the column'
lngRowLast = xlWorksheet.Range("A65536").End(xlUp).Row
Set rngRowEnd = xlWorksheet.Cells(lngRowLast, 1)

'Range of all cells from first name cell to last name cell'
Set rngNames = xlWorksheet.Range(rngRowStart, rngRowEnd)


'Loop through the range of names'
For Each rngName In rngNames

    'Ignore any blank cells'
    If rngName <> vbNullString Then

        'Set the text of the bookmark range to the name from Excel'
        bkmName.Text = rngName

        'The above statement deleted the Bookmark, so create _
            a new Bookmark using the range specified in bkmName'
        ActiveDocument.Bookmarks.Add Name:="name", Range:=bkmName

        'Print the document'
        ActiveDocument.PrintOut
    End If
Next


'Restore the orignal value of the bookmark'
bkmName.Text = strBookmarkOriginalText
ActiveDocument.Bookmarks.Add Name:="name", Range:=bkmName

'Close the Workbook without saving'
xlWorkbook.Close SaveChanges:=False

End Sub

希望这有帮助。

the following Sub should solve this for you, but you might need to change the way your bookmark is defined.

There is more than one way to insert a Bookmark. This method requires the Bookmark to be inserted by highlighting the text, not simply positioning the cursor at a location in the text.

Sub insertar_nombre()

Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet

Dim strFilename As String

Dim bkmName As Word.Range
Dim strBookmarkOriginalText As String

Dim lngRowLast As Long
Dim rngRowStart As Excel.Range
Dim rngRowEnd As Excel.Range

Dim rngNames As Excel.Range
Dim rngName As Excel.Range


'Open file dialog and only allow Excel files'
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Title = "Seleccionar Documento de Excel"

    'Only let them select Excel files'
    .Filters.Clear
    .Filters.Add "Excel Documents (*.xls)", "*.xls"

    'Check if a file is selected'
    If .Show = True Then

        'Since AllowMultiSelect is set to False, _
            only one file can be selected'
        strFilename = .SelectedItems(1)
    Else

        'No file selected, so exit the Sub'
        Exit Sub
    End If
End With


'Set the bookmark to a Word range (not a Bookmark object)'
Set bkmName = ActiveDocument.Bookmarks("name").Range

'Save the original text of the bookmark'
strBookmarkOriginalText = bkmName.Text


'Open the Excel file'
Set xlWorkbook = Excel.Workbooks.Open(strFilename)
Set xlWorksheet = xlWorkbook.Worksheets(1)

'Range of the first cell that contains a name'
Set rngRowStart = xlWorksheet.Cells(2, 1)

'Range of the last cell in the column'
lngRowLast = xlWorksheet.Range("A65536").End(xlUp).Row
Set rngRowEnd = xlWorksheet.Cells(lngRowLast, 1)

'Range of all cells from first name cell to last name cell'
Set rngNames = xlWorksheet.Range(rngRowStart, rngRowEnd)


'Loop through the range of names'
For Each rngName In rngNames

    'Ignore any blank cells'
    If rngName <> vbNullString Then

        'Set the text of the bookmark range to the name from Excel'
        bkmName.Text = rngName

        'The above statement deleted the Bookmark, so create _
            a new Bookmark using the range specified in bkmName'
        ActiveDocument.Bookmarks.Add Name:="name", Range:=bkmName

        'Print the document'
        ActiveDocument.PrintOut
    End If
Next


'Restore the orignal value of the bookmark'
bkmName.Text = strBookmarkOriginalText
ActiveDocument.Bookmarks.Add Name:="name", Range:=bkmName

'Close the Workbook without saving'
xlWorkbook.Close SaveChanges:=False

End Sub

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文