在我的单词“工作表”中,我有一个CommandButton,其中单击时触发代码的某个部分,基本上是打开第二个单词文档并插入当前单词的一些信息(Document1)转换成第二个(Document2)TextBox。访问另一个word文档文本框
我有字符串变量包含文本在一个单词文档(例如文档1)。我正在打开第二份文件(例如Document2)。然后,我需要从文档2到达特定的文本框,并向其中插入我已有的字符串字符串之一的值。
这就是说,我不能访问第二个文档(Document2),因为我总是得到“4160错误”,导致文件名不正确。
因此,我怎么可以访问我的第二份文件(文档2)文本框并插入到它的具体值我已经有了?
我的代码如下(简化为一个变量,因为这将是相同的所有其他):
Private Sub btn1_Click()
Dim strFile As String
Dim WordApp As New Word.Application
Dim WordDoc As Word.Document
Dim name As String
strFile = "C:\Users\WhateverUser\Desktop\WhateverFolder\Document2.docx"
name= txtBoxName.Text
'This comes from the first document (Document1) which is correct.
' Opening another Word document (Document2)
Set WordDoc = WordApp.Documents.Open(strFile)
WordApp.Visible = True
'Here is the problem
'Trying to access the Document2 TextBox (txtBoxNameDoc2) with various ways but I always get the Incorrect File Name Error
'First I tried
Documents("Document2.docx").Bookmarks("txtBoxNameDoc2").Range.Text = name
'Then I tried
Documents("Document2.docx").txtBoxNameDoc2.Text = name
'And after those, I went looking on internet and tried what I could find but none did work.
End Sub
关于你的问题的好工作。我有一个待完成的编辑来改进您的格式 - 我建议您将来这样做,因为它可以帮助人们阅读您的问题,并希望更快地为您解答问题。我还添加了更多相关标签 - 再次,目标是帮助您的问题被正确的人看到。 –
在您提供的代码中,您没有为变量'txtBoxNameDoc2'分配任何值。这似乎是一个错误。另外,您确定文档名称是“Document2.docx”吗?如果这不正确,它也将是一个错误。 –
首先,谢谢@DavidSchwartz你的编辑没有我的格式!我会考虑一下在未来几年我可能会问的其他问题。 – Marks