如何使用书签将ExcelBox中的Userform,TextBox的值复制到doc文件中?

时间:2016-11-21 23:12:38

标签: excel vba ms-word textbox

我尝试弄清楚如何将int中插入的值复制到TextBox中。我希望将Userform中插入的所有值保存在使用书签的doc文件中。

这是我的代码:

Userform

2 个答案:

答案 0 :(得分:0)

我为此使用了一个单独的Sub(下面) - 调整代码:

Sub report()
    Dim wrdApp As Word.Application, doc
    Set wrdApp = CreateObject("Word.Application")


    Set doc = wrdApp.documents.Open("C:\Users\ment_hoch.docx")

    SetBookmarkText doc, "ab_name", Me.TextBox1.text 

    SetBookmarkText doc, "abteilung", ComboBox1.Value

End Sub


'Replace the text in a bookmark or insert text into an empty (zero-length) bookmark
Sub SetBookmarkText(oDoc As Word.Document, sBookmark As String, sText As String)

    Dim BMRange As Word.Range

    If oDoc.Range.Bookmarks.Exists(sBookmark) Then
      Set BMRange = oDoc.Range.Bookmarks(sBookmark).Range
      BMRange.Text = sText
      oDoc.Range.Bookmarks.Add sBookmark, BMRange
    Else
      MsgBox "Bookmark '" & sBookmark & "' not found in document '" & oDoc.Name & "'" & _
              vbCrLf & "Content not updated"
    End If

End Sub

答案 1 :(得分:0)

经过一些测试,我发现当我使用UserForm1时,它工作得很好。而不是我

.Selection.GoTo What:= wdGoToBookmark,Name:=" ab_name" .Selection.TypeText text:= UserForm1.TextBox1.text

.Selection.GoTo What:= wdGoToBookmark,Name:=" abteilung" .Selection.TypeText text:= UserForm1.ComboBox1.Value

非常感谢你的回复:)当我得到一些空闲时间的时候,我也会尝试一下!