0
votes

First time post and whilst I may be trying to run before I can crawl, here is my situation (using Word 2013).

  • I have created a Userform where Branch A, asks for all of the data required to populate a document (fixed format and cannot be altered) used in our organisation, for a particular instance.

  • The document is now saved as a ".docx" file, stripped of the macros (vba
    code). So far so good.

  • Branch B, is intended to allow the loading of an existing document created in Branch A (as above). The data from this existing document should now be copied over to the Userform embedded in the new document created when double- clicking the template, to allow updating of various fields, whilst avoiding the necessity of re-input of unchanged details (to be subsequently saved as '.docx').

  • I am able to launch the new document by double-clicking the template and then via command button, execute the code to open the pre-existing document.

But this is where I start to get horribly confused...

When I begin by creating a new document through the template, Project Explorer looks like this...

Project Explorer on new document from template

Now I open an existing document (Filled.docx) in VBA to give a Project Explorer view like..

Existing doc opened in preparation for copying of data

So now I have 2 instances of ThisDocument.

  1. The code is being executed under '(DOC Generator)'

  2. The data I want to extract/copy to the userform in (DOC Generator) is ThisDocument in (Filled).

  3. When I am finished editing the information in the userform, I will save ThisDocument (Document1) as I would, had I been creating an entirely new document.

The difficulty I have is, how do I reference an ActiveX control in the existing document to copy it to the userform.

Something like this???

txt_Author = ThisDocument.lbl_AssessingOfficer.Caption, where
  • txt_Author is a textbox in the userform, and,

-ThisDocument.lbl_AssessingOfficer.Caption is intended to refer to a label in the existing document (Filled) which holds the data.

Except that when I test this, ThisDocument or ActiveDocument both pick-up the data from ThisDocument (Document1).

How do I reference the label (data) from ThisDocumentunder (Filled)?

Thankyou in advance for any help and your patience. If I could be so bold, if possible when answering, it would be extremely helpful to me (and maybe subsequent others) if you could explain how the code referencing works.

2
Olle, thank you. Still stuck. Included code with 'Public oOtherDocument as Document' in Module 1. I get "error 434". when I try to copy over the data. Code like: DestinationName = oOtherDocument.Lbl_DestinationName12Pg1.Caption Where, DestinationName is a textbox in the userform, and, oOtherDocument.Lbl_DestinationName12Pg1.Caption should refer to the text located in the label on "Filled.docx" (which received the text from DestinationName originally). Value transits thus, DestinationName (userform = manual input) => Lbl_DestinationName12Pg1.Caption => new doc from template. My error?Sheepdog
Olle, thank you. Still stuck. Included code with 'Public oOtherDocument as Document' in Module 1. I get "Run-time error '438' Object does not support this property or method", with code: DestinationName = oOtherDocument.Lbl_DestinationName12Pg1.Caption DestinationName is a textbox in the userform + oOtherDocument.Lbl_DestinationName12Pg1.Caption should refer to text located in label on "Filled.docx" (received the text from DestinationName originally). Value transits thus, DestinationName (userform = manual input) => Lbl_DestinationName12Pg1.Caption => new doc from template. My error?Sheepdog

2 Answers

0
votes

Every Word document has a ThisDocument object. If you write VBA code which uses ThisDocument, that object always points to the ThisDocument object in the same file as your code. ActiveDocument, on the other hand, always points to the document that is active in Word, that is, the one that is visible in the Word window at the moment. If possible, try to avoid using ActiveDocument since it can point to a different document than what you think.

In order to access a Document object other than the one your code is in, you cannot use ThisDocument, so instead you create a reference to the other document:

Option Explicit

Sub FindOtherDocument()
    Dim oOtherDocument As Document
    Set oOtherDocument = Application.Documents("Filled.docx")

    Call MsgBox("This is the name of the file which oOtherDocument points to: " & oOtherDocument.FullName)
End Sub

Note: Application.Documents() is used to access documents that are already opened. If the document is closed you need to open it using Application.Documents.Open() or create a new one using Application.Documents.Add(), both returning Document objects.

0
votes

Well as you might expect given his reputation, Ollie's answer was correct. But I was still stuck, each time I tried to copy a value over from the previously filled document, I was receiving,

"Run-time error '438' Object does not support this property or method"

It turns out that changing the file type to a Macro-enabled document (edited the file extension) was the key. If you have read above, after creating a document using the userform interface, I had copied and saved the data to a '.docx' (which strips away the VBA code... I don't want this included).

However, it would seem that this file extension also stops VBA code associated with the original userform (Edit Existing function I had written) from doing any operations in the "filled" document as well.

So, thanks again Ollie and for anyone else, watch out for this gotcha!