Solved Automating Word (1 Viewer)

Pat Hartman

Super Moderator
Staff member
Joined
Feb 19, 2002
Messages
44,163
I have an application that populates bookmarks in a Word document. The app works fine on my PC but not on the client's. I'm using the O365 subscription slightly behind the bleeding edge. Client has a 2019 boxed version. Both are 32 bit

The error is 5941 -Requested member of collection does not exist

The code stops at:

WordDoc.FormFields("Salutation").Result = rsDAO!Salutation

The error is with the formfield. The Salutation bookmark exists and Word seems to see it when I open the document. The document works fine when I run it on my PC.

When I skip over all the population code, the automation finishes and displays the document. Of course, none of the fields are populated. So, yes the document exists. Yes, I have opened the correct document. Yes I can see the bookmarks when I open the document in Word. I just can't get Access to populate the bookmarks. I tried to print the current contents and get the same error, so I can't view them or update them.

Could this be a manifestacion of a security error?

I made the drive that holds the app a trusted location for both Word and Access but that didn't help so if it is security, I probably need to get their IT support to fix it for me.

Google hasn't been helpful. All the responses are for code running within word and seem to relate to some review setting which is of course not showing in any of my menus so who knows what version the posts relate to.
 

Attachments

  • 1718740056981.png
    1718740056981.png
    158.9 KB · Views: 32
I have 2019 32bit single purchase, so if you want me to test anything, I am more than happy to?
 
Hello Pat,
I asked chatty to answer your question and this is chatty 's answer:-

Based on your detailed description, it seems you're encountering a compatibility issue between your O365 setup and the client's Word 2019 boxed version. Here are some steps and considerations to troubleshoot and potentially resolve the error 5941:

### 1. **Check FormField Existence Programmatically**
Ensure the form field "Salutation" exists in the client's document programmatically before attempting to populate it. You can add a check in your code like this:


Code:
If WordDoc.FormFields.Exists("Salutation") Then
    WordDoc.FormFields("Salutation").Result = rsDAO!Salutation
Else
    MsgBox "FormField 'Salutation' does not exist."
End If

### 2. **Check Bookmark vs. FormField**
Ensure that "Salutation" is indeed a form field and not just a bookmark. You can test for both cases:


Code:
Dim bm as Bookmark
Dim ff as FormField


On Error Resume Next
Set bm = WordDoc.Bookmarks("Salutation")
Set ff = WordDoc.FormFields("Salutation")


If Not ff Is Nothing Then
    WordDoc.FormFields("Salutation").Result = rsDAO!Salutation
ElseIf Not bm Is Nothing Then
    bm.Range.Text = rsDAO!Salutation
Else
    MsgBox "Bookmark or FormField 'Salutation' does not exist."
End If
On Error GoTo 0


### 3. **Ensure Trust Settings**
You mentioned making the drive a trusted location, which is good. Verify that both the file and macros are also trusted:

- In Word, go to **File** > **Options** > **Trust Center** > **Trust Center Settings**.
- Ensure that **Macro Settings** are set to **Enable all macros** (at least for testing).
- Add the document location to **Trusted Locations**.

### 4. **Check Compatibility and References**
Since the client is using Word 2019 and you are on a different version, ensure that all necessary references are included and compatible:

- Open the VBA editor (`Alt + F11`).
- Go to **Tools** > **References**.
- Ensure that all necessary references are checked and there are no missing references.

### 5. **Error Logging**
Implement error logging to capture more detailed information about what happens when the error occurs. For example:


Code:
On Error GoTo ErrorHandler


' Your code here...


Exit Sub


ErrorHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    ' Optionally log the error to a file or a log
    ' Open "C:\ErrorLog.txt" For Append As #1
    ' Print #1, "Error " & Err.Number & ": " & Err.Description
    ' Close #1
    Resume Next
End Sub


### 6. **Compatibility Mode**
If the document was created in a newer version of Word, it might be running in compatibility mode on the client's Word 2019. Try saving the document in Word 2019 format explicitly.

### 7. **Client Environment**
Ensure the client’s environment is not restricting macros or automation scripts:

- Check the antivirus/firewall settings.
- Ensure that no Group Policy settings are preventing script execution.

### Conclusion
Implement these troubleshooting steps to identify and resolve the compatibility issue. It’s likely a minor discrepancy between the environments causing the problem. If the problem persists, consider working closely with the client’s IT support to check any security or permission issues more thoroughly.
 
So, if I go into developer mode, activate "Design Mode" and add some field while in macro recording mode, I see that I'm dealing with things like:
Selection.Range.ContentControls.Add
And
Selection.ParentContentControl

So, I went ahead and did this:
Code:
Sub test()
    Dim jj As Object
    Set jj = ActiveDocument.ContentControls
    Stop
End Sub

It returned my fields
1718744375495.png


I added 2, there they are. So, you're probably looking for those objects in order to do automation with added fields using the aforementioned method.
 
Everything works fine on my PC. That means that the formfields and bookmarks are created correctly. Chatty had a lot of suggestions. Several are irrelevant such as save in 2019 format. There is no option to do that. The rest I've already tried.
The references include Word and automation.
The File locations are trusted - this used to fix automation problems.
While the code is running, Access can not populate the formfields and it also can't even see them. That's a little strange.

If I stop the code at the error and continue at the line after all the formfields are filled, the app continues normally and correctly creates and saves the document. Just none of the fields are filled.

I have 2019 32bit single purchase, so if you want me to test anything, I am more than happy to?
Thanks, I'd have to remove most of the data, but I may do it if the IT guy can't figure out the issue which I'm pretty sure is security.
 
And this seems to be the way to write to it:
ActiveDocument.ContentControls.Item(1).Range.Text = "ff"

So, if you target the item's title, you should be good to go.
 
If I stop the code at the error and continue at the line after all the formfields are filled, the app continues normally and correctly creates and saves the document. Just none of the fields are filled.
As far as I can see in my word document, after adding fields using Design Mode, there is nothing inside the FormFields object.
 
Since it is calling out an error blaming security settings, check the settings on your A/V or network security package. They can be set to Low all the way up to High and usually a couple of clicks inbetween.
 
And this seems to be the way to write to it:
I appreciate your interest. There are several ways to populate the fields in Word. I do NOT want to use the method that refers to the controls by their number which is what your suggestion does. Also "ActiveDocument" is how you refer to the document within Word. It's the equivalent of "Me" in Access. I want to always refer to a specific document because who knows what other documents the user might have open when she's trying to print a letter from this app. That's why my app creates a specific Word Object and uses that object to qualify all the code. The app might be creating a couple hundred documents at one time which will take a few minutes. I certainly can't prevent the user from starting the process and then opening Word and making a new document. Her document could easily become the "ActiveDocument" if the timing is right.

Set WordDoc = WordApp.Documents.Add(sDocName)

Since it is calling out an error blaming security settings, check the settings on your A/V or network security package. They can be set to Low all the way up to High and usually a couple of clicks inbetween.
Thanks but I don't have authorization to do any security stuff on their systems and I don't want it.
 
Also "ActiveDocument" is how you refer to the document within Word. It's the equivalent of "Me" in Access.
It can be any document object, not necessarily ActiveDocument.
I do NOT want to use the method that refers to the controls by their number which is what your suggestion does.
Unfortunately, the ContentControl object does not have any Name property and its Title property can not be used for it. However, in a For Each loop for the items of the ContentControl object, you can use some condition to target the Title property, such as:
Code:
For Each CC In TheDocumentObject.ContentControls
    If CC.Title = "Salutation" Then CC.Range.Text = rsDAO!Salutation
Next CC

Where CC is a ContentControl object

Or maybe a Select Case.
 
So, why are you so convinced that changing the way I reference the controls will fix the problem on the user's computer? There is nothing wrong with the method I am using. It works perfectly on my PC?
 
Let's see what gasman comes out with as he doesn't have any security restrictions.
 
So, why are you so convinced that changing the way I reference the controls will fix the problem on the user's computer? There is nothing wrong with the method I am using. It works perfectly on my PC?

Well, there are FormFields and ContentControls.

The way you get FormFields is by using DesignMode and adding a "Legacy" control.
1718752718613.png

The way you get ContentControls is by NOT adding a "Legacy" control.
1718752778600.png


If you're getting this error:
The error is 5941 -Requested member of collection does not exist
Then it's likely because there is NOTHING in the FormFields object. If there is nothing, but you do have "fields", then, likely, you have ContentControls.
 
@Gasman I tried to start a conversation with you but I get a message that I can't start a conversation with you. Can you start one with me.
 
Here's a file for testing, if you or anyone wants to see what I'm talking about.

Just go to Developer Tab > Macros > test

The first field is a ContentControl with title "Salutation"
The second field is a FormField with Name "MySalutationLegacy"
 

Attachments

@Gasman I tried to start a conversation with you but I get a message that I can't start a conversation with you. Can you start one with me.
Will do.
Sorry, I have for only people I follow, so I have followed you now. TBH I thought I was following you. :)
Now you should be good.
 
Thanks everyone. The app works as is on two other computers so I'll just have to wait for the client's IT people to solve her security problem.
 

Users who are viewing this thread

Top Bottom