Accessing another text document TextBox

In my word "sheet" I have a CommandButton that when clicked, triggers a certain piece of code, which basically consists of opening the second word document and pasting some information from the current ( Document1 ) into the second ( Document2 ) TextBox .

I have String variables containing text in a single document document (ei Document1 ). I open the second document (ei Document2 ). Then I need to reach a specific TextBox from Document2 and insert into it the value of one of the String variables I already have.

That being said, I cannot access this second document ( Document2 ), as I always get the error "4160" which is the result of an invalid filename.

So how can I access the second Document ( Document2 ) of the TextBox and insert into it a specific value that I already have?

My code is as follows (simplified to one variable since it will be the same for everyone else):


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

      

+3


source to share


1 answer


I can guess some errors in the code you listed above, but if this line works without returning an error:

Set WordDoc = WordApp.Documents.Open(strFile)
WordApp.Visible = True

      

You should be able to:

WordDoc.Bookmarks(txtBoxNameDoc2).Range.Text = name

      

This is because you have already opened "Document2.docx" and furthermore you have specifically assigned it to an object variable WordDoc

. Since you did this, you don't need to explicitly reference it from the collection Documents

as you do in your source code.



NB: This is assumed to be txtBoxNameDoc2

a valid string that identifies a bookmark in the document WordDoc

. If it is to be interpreted as a literal string (that is, it is the actual name of the bookmark, then you need to qualify it with quotes, e.g .:

WordDoc.Bookmarks("txtBoxNameDoc2").Range.Text = name

      

If it continues to raise an error, then the named bookmark does not exist.

you can assign a bookmark to the TextBox object. Bookmarks do not "automatically" exist in the document, so you must first provide such a bookmark. You can view them and assign them (if they don't exist) via the ribbon).

Bookmarks don't exist unless you create them. You assumed that the object name could also refer to a bookmark, and while it is possible, you first need to create a bookmark and give it the name with which you want to refer to it.

+2


source







All Articles