在书签位置

时间:2016-05-10 14:27:23

标签: vba word-vba

我是新手,正在创建一个将数据输入到字母中的用户表单。有一个字符串,我需要加粗和下划线。

    With .Bookmarks("bidDate").Range.InsertBefore
      .Text = inputBidM & " " & inputBidD & ", " & inputBidY
      .Font.Bold = True
      .Font.Underline = True
    End With

我发现这个http://computer-programming-forum.com/1-vba/8e9aacaf425425ad.htm得到了我上面的内容,但是收到了编译错误:无效或不合格的引用。

非常感谢任何帮助。

编辑:以下是该部分的代码。我已经注释了所有其他内容。

Private Sub startButton_Click()
 ' Inserting Addendum Info from fill in boxes
 With ActiveDocument
    .Bookmarks("addenDate").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
    .Bookmarks("addenDateA").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
    .Bookmarks("contractNo").Range.InsertBefore inputContractNo
    .Bookmarks("contractNoA").Range.InsertBefore inputContractNo
    .Bookmarks("fapNo").Range.InsertBefore inputFAPNo
    .Bookmarks("descrip").Range.InsertBefore inputDescrip
    .Bookmarks("addenNo").Range.InsertBefore inputAddenNo
    .Bookmarks("addenNoA").Range.InsertBefore inputAddenNo
    .Bookmarks("addenNoB").Range.InsertBefore inputAddenNo
    .Bookmarks("bidDate").Range.Text inputBidM & " " & inputBidD & ", " & inputBidY
        .Font.Bold = True
        .Font.Underline = True
 End With
End Sub

2 个答案:

答案 0 :(得分:0)

以下工作正常:

Sub test()

    With Selection
        .GoTo What:=wdGoToBookmark, Name:="bidDate"
        .Text = "blah blah blah"
        .Font.Bold = True
        .Font.Underline = True
    End With

End Sub

.Bookmarks之前的冒号只有在使用另一个With语句时才有可能。这就是您收到错误消息的原因。

答案 1 :(得分:0)

With关键字允许您对对象执行多个操作,而无需为每一行键入对象。通过将With放在每个End With行的前面,您显示的代码可以在没有ActiveDocument.Bookmarks的情况下编写。您可以看到如何节省打字时间,并且可以更容易阅读: - )

由于您想要使用最后一个书签而不仅仅是插入文本,您可以添加(嵌套)另一个With...End With以使您使用该范围,如下所示:

Private Sub startButton_Click()
  ' Inserting Addendum Info from fill in boxes
  With ActiveDocument
    .Bookmarks("addenDate").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
    .Bookmarks("addenDateA").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
    .Bookmarks("contractNo").Range.InsertBefore inputContractNo
    .Bookmarks("contractNoA").Range.InsertBefore inputContractNo
    .Bookmarks("fapNo").Range.InsertBefore inputFAPNo
    .Bookmarks("descrip").Range.InsertBefore inputDescrip
    .Bookmarks("addenNo").Range.InsertBefore inputAddenNo
    .Bookmarks("addenNoA").Range.InsertBefore inputAddenNo
    .Bookmarks("addenNoB").Range.InsertBefore inputAddenNo
     With .Bookmarks("bidDate").Range
        .Text = inputBidM & " " & inputBidD & ", " & inputBidY
        .Font.Bold = True
        .Font.Underline = True
    End With
  End With
End Sub