3
votes

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
1
Great job on your question. I have a pending edit to improve your formatting-- I recommend you do this in the future as it helps people read your question and hopefully get you an answer more quickly. I also added more related tags-- again, the goal is to help your question be seen by the right people. - David Schwartz
In the code you have provided, you have not assigned any value to the variable txtBoxNameDoc2. That appears to be an error. Also, are you certain that the document name is "Document2.docx"? If that is incorrect, it will also be an error. - David Zemens
First, thank you @DavidSchwartz for your edit no my formatting ! I'll look into it for the other questions I may ask during the years to come. - Marks
As for @DavidZemens, the ' txtBoxNameDoc2 ' would be the name of the TextBox in the Document2. On the Document2.docx, it's not the name, I used Doc1 and Doc2 as an exemple but yes, I am pretty sure of the name of the document since I copied it using the "rename" (F2) option and pasted it directly in my code. - Marks
If the txtBoxNameDoc2 is a string literal, it needs to be enclosed in quotes... - David Zemens

1 Answers

2
votes

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 (i.e., 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.