能否修改此 MS Word 宏以删除 AddressLine 书签中的空白行?

问题描述 投票:0回答:1

我创建了一个工作宏,其中信函模板提示用户在文本框中输入信息以更新文档。我遇到的问题是有些客户端的地址超过 3 行,有些超过 5 行等,并且宏为任何未填充的书签留下空白行。有没有办法修改这个宏以便删除空行?

谢谢

我尝试在宏中输入多行地址字段选项,但无法使其工作。

Sub LetterTemplate()

    Dim bookmarkName As String
    Dim bookmarkText As String
    Dim i As Integer

    ' Set the bookmark names in your template
    Dim bookmarkNames As Variant
    bookmarkNames = Array("Name", "TitleSurname", "AddressLine1", "AddressLine2", "AddressLine3", "Postcode", "AccountNumber") ' Add or remove bookmark names as needed

    ' Loop through each bookmark
    For i = LBound(bookmarkNames) To UBound(bookmarkNames)
        ' Prompt for text to insert
        bookmarkName = bookmarkNames(i)

        If bookmarkName = "AddressBookmark" Then
            ' For multiline address field, use InputBox with vbOKCancel and vbInformation
            bookmarkText = InputBox("Enter address for " & bookmarkName & ":", "Enter Address", , , , vbInformation + vbOKCancel)

            ' Check if the user clicked OK
            If bookmarkText = vbOKCancel Then
                ' Insert text into the bookmark
                Call InsertIntoBookmark(bookmarkName, bookmarkText)
            End If
        Else
            ' For other bookmarks, use the standard InputBox
            bookmarkText = InputBox("Enter text for " & bookmarkName & ":", "Enter Text")

            ' Insert text into the bookmark
            Call InsertIntoBookmark(bookmarkName, bookmarkText)
        End If
    Next i

    MsgBox "Action Complete"

End Sub

Sub InsertIntoBookmark(bookmarkName As String, bookmarkText As String)
    Dim bmRange As Range

    ' Check if bookmark exists
    If ActiveDocument.Bookmarks.Exists(bookmarkName) Then
        ' Set the range to the bookmark
        Set bmRange = ActiveDocument.Bookmarks(bookmarkName).Range

        ' Clear existing content in the bookmark
        bmRange.Text = ""

        ' Insert the new text into the bookmark
        bmRange.Text = bookmarkText

        ' Extend the bookmark to cover the inserted text
        ActiveDocument.Bookmarks.Add bookmarkName, bmRange
    Else
        MsgBox "Bookmark '" & bookmarkName & "' not found.", vbExclamation, "Bookmark Not Found"
    End If
End Sub


vba ms-word
1个回答
0
投票
Sub InsertIntoBookmark(bookmarkName As String, bookmarkText As String)
    Dim bmRange As Range, oBM As Bookmark
    ' Check if bookmark exists
    If ActiveDocument.Bookmarks.Exists(bookmarkName) Then
        Set oBM = ActiveDocument.Bookmarks(bookmarkName)
        ' Set the range to the bookmark
        Set bmRange = oBM.Range
        If Len(bookmarkText) = 0 Then
            ' Remove paragraph
            bmRange.Paragraphs(1).Range.Delete
            ' Remove bookmark
            oBM.Delete
        Else
            ' Insert the new text into the bookmark
            bmRange.Text = bookmarkText
            ' Extend the bookmark to cover the inserted text
            ActiveDocument.Bookmarks.Add bookmarkName, bmRange
        End If
    Else
        MsgBox "Bookmark '" & bookmarkName & "' not found.", vbExclamation, "Bookmark Not Found"
    End If
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.