PDA

View Full Version : Accessing another document textbox



OUO
06-23-2015, 08:05 AM
In my word "sheet", I have a CommandButton of which, when clicked, triggers a certain part of code which basically is about opening a second word document and inserting some informations from the current one (Document1) into the second one (Document2) TextBox.
I have String variables containing text in one word document (e.i. Document1). I am opening a second document (e.i. Document2). Then, I need to reach a specific TextBox from the Document2 and insert into it the value of one of the String variables I already have.
That being said, I can't access that second document (Document2) since I always gets the "4160 error" which result of the file name being incorrect.

Therefore, how can I access my second document (Document2) TextBox and insert into it a specific value I already have?

My code as follow (simplified to one variable since it'll be the same for every other):
___________________________________________________________________________ _____________________________________
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
___________________________________________________________________________ ______________________________________

Tommy
06-23-2015, 02:14 PM
Hello,

WordDoc will be the variable you need, it is the document2.

OUO
06-25-2015, 04:30 AM
Hello,

WordDoc will be the variable you need, it is the document2.

Indeed it is. Thanks.