简体   繁体   中英

Accessing another word document TextBox

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 (ei Document1 ). I am opening a second document (ei 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

I can speculate at some errors in the coding you have provided above, but if this line works without returning an error:

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

THen you should be able to do:

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

This is because you have already opened "Document2.docx" and furthermore you have specifically assigned it to the WordDoc object variable. Because you have done this, you do not need to explicitly reference it from the Documents collection, as you are doing in your original code.

NB: This assumes that txtBoxNameDoc2 is valid string that identifies a bookmark in the WordDoc document. If it should be interpreted as a literal string (ie, it is the actual name of the bookmark, then you need to qualify it with quotation marks, like:

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

If this continues to raise an error, then the named bookmark doesn't exist.

It is possible to assign a bookmark to a TextBox object. Bookmarks do not "automatically" exist in a document, so first you have to ensure such a bookmark exists. You can review these and assign them (if they do not exist) through the ribbon).

Bookmarks don't exist unless you create them. You've assumed that the object's name can also refer to a Bookmark, and while it can , first you need to create the bookmark and assign it the name by which you want to refer it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM