我尝试实现以下目标:将字符串元素从excel单元复制到word书签中。在那里,用户可以修改字符串,修改后可以将字符串发送回excel。
我的问题:我可以将字符串复制到书签中(bookmars被设置为单词中的封闭书签,这意味着我有一个刚写好的“ abc”,应该在其中放置书签,标记为“ abc”并按下了书签。)复制文本后仍然存在,但是当我尝试将书签的字符串写回Excel时,.Range-Object为空。
我的问题:谁能帮助我实现我的目标,即从书签中将字符串取回至Excel。因此,我只需要一个对象中的字符串即可。我可以将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
好的,我自己找到了答案。
oData.PutInClipboard
oData.GetFromClipboard
CurrentDoc.Bookmarks(Step & "Number").Range.InsertBefore oData.GetText
Debug.Print (CurrentDoc.Bookmarks(Step & "Number").Range.Text)
做到了。您只需要删除之前用于封闭书签的字符。
在这里,我们有一个很好的资源:VBA和书签
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句