gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:44:45 27 4
gpt4 key购买 nike

我正在尝试创建一个用户表单,将 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

关于excel - VBA/Userform - 将值从文本框传输到带有书签的 Word 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68752539/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com