将字符串发送到书签后,将字签名中的字符串返回到excel

时间:2013-10-18 12:06:25

标签: vba excel-vba word-vba excel

我尝试实现以下目标: 将字符串元素从Excel单元格复制到单词中的书签。用户可以修改字符串,修改后可以将字符串发送回excel。

我的问题: 我可以将字符串复制到书签上(博客被设置为单词中的书签,意味着我有一个刚刚写好的'abc',其中书签应该是,标记为'abc'并按下书签。)书签仍然存在之后复制文本,但当我尝试将书签上的字符串写回excel时,.Range-Object为空。

我的问题: 任何人都可以帮助我实现我的目标,让书签从书签中恢复过来。因此我只需要一个对象左右的字符串。我想把msgbox(the_string)放到excel所在的地方。

这是我的代码:

 Sub BM(Step As Byte)
    Dim docApp As Word.Application
    Dim CurrentDoc As Word.Document

    Set docApp = GetObject(, "word.application")
    Set CurrentDoc = docApp.ActiveDocument

    CurrentDoc.Bookmarks(Step & "Number").Range.Select

    Dim objBMRange_pos As Word.Range
    Set objBMRange_pos = CurrentDoc.Bookmarks(Step & "Number").Range

    Dim oData As dataobject

    Set oData = New dataobject

    oData.SetText ""
    'The function Check_A delivers the string from excel cells
    oData.SetText Check_A(Step, "Number")
    oData.PutInClipboard

    oData.GetFromClipboard
    CurrentDoc.Bookmarks(Step & "Number").Range.Text = oData.GetText
    'CurrentDoc.Parent.Selection.PasteSpecial DataType:=wdPasteText
    'Re-new the bookmarks
    CurrentDoc.Bookmarks.Add Name:=(Step & "Number"), Range:=objBMRange_pos
    Debug.Print CurrentDoc.Bookmarks(Step & "Number").Range.Text
End Sub

1 个答案:

答案 0 :(得分:0)

好吧,我自己找到了答案。

oData.PutInClipboard
    oData.GetFromClipboard
    CurrentDoc.Bookmarks(Step & "Number").Range.InsertBefore oData.GetText
    Debug.Print (CurrentDoc.Bookmarks(Step & "Number").Range.Text)

诀窍。您只需要删除之前用于封闭书签的字符。

我们有一个很好的来源:VBA & Bookmarks