VBA/Userform - 使用书签将值从文本框传输到 Word 不起作用

VBA/Userform - Transfer Value from a TextBox to Word with Bookmarks don't work

我正在尝试创建一个用户窗体,将 TextBoxes 的值传输到 Word 文件中的书签位置,但出现错误。我尝试了一些在 Google 上找到的示例,但我仍然遇到错误。

我收到错误“VBA 对象不支持此 属性 或方法错误(错误 438)”

我现在正在尝试使用一个较短的宏来打开 Word 文件,并将“测试”写入一个书签:

Private Sub CommandButton3_Click()
Dim wordApp As Object
Dim wordDoc As Object
VorlagePfad = "D:\Temp\Testfile.doc"
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0

wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad)

With wordDoc.Selection
    .Bookmarks("Zeile1").Range.Text = "Test"
End With

End Sub

错误发生在

With wordDoc.Selection

我也试过这段代码,也得到了同样的错误:

Private Sub CommandButton3_Click()
Dim wordApp As Object
Dim wordDoc As Object
VorlagePfad = "D:\Temp\Testfile.doc"
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0
wordApp.documents.Open VorlagePfad
wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad)

wordApp.Bookmarks("Zeile1").Range.Text = "Test"

End Sub

希望你能帮助我。

亲切的问候

您只需要删除“.Selection”。像这样:

Private Sub CommandButton3_Click()
Dim wordApp As Object
Dim wordDoc As Object
VorlagePfad = "D:\Temp\Testfile.doc"
DisplayAlerts = False
Set wordApp = CreateObject("word.application")
wordApp.Options.SaveInterval = 0

wordApp.Visible = True
Set wordDoc = wordApp.documents.Open(Filename:=VorlagePfad)

With wordDoc
    .Bookmarks("Zeile1").Range.Text = "Test"
End With

End Sub