0

I am trying to automate a Word file with a userform.

I was thinking to have some variables in the Word file and to put a button in the ribbon that opens a userform to input these values.

I followed some tutorial I found online and I have the following code when I press OK on the userform:

Private Sub cmdOK_Click()

Set oVars = ActiveDocument.Variables
Hide

oVars(RfQ).Value = TextBox1.Value
oVars(SFDC).Value = TextBox2.Value
ActiveDocument.Fields.Update
Unload.Me

End Sub

I get the error

"Compile Error, Variable not defined".

In my text I have the following: {DOCVARIABLE SFDC * MERGEFORMAT}

My aim is to fill the variables with the value I input in the userform.

Further to this, I would like that every time I open the userform from the ribbon I get pre filled the information that is in the variable and that I can change/update them.

Community
  • 1
  • 1

1 Answers1

0

The first time you will have to add the variable, like this:

oVars.Add "RfQ", TextBox1.Value

Once it is created, you can change it like this:

oVars("RfQ") = TextBox1.Value

Update after questions in comments
In your example you wrote oVars(RfQ).Value. This means "Take the document variable with the name stored in the vba variable RfQ". I'm guessing that that isn't what you want.
My guess is that the document variable is named RfQ and that you want to use that as a hardcoded name for the variable. In this case, add quotes to the document variable name, like this:

oVars("RfQ").Value = TextBox1.Value

Since you had no quotes, you introduced a new implicit vba variable, with the name RfQ, with no value. That gave the effect that you tried to read a document variable with an empty name. If you put Option Explicit at the top of your module, you will be protected from issues like this.

Sam
  • 5,424
  • 1
  • 18
  • 33
  • Thank you. What do you mean with the first time and once is created? I have already the variable RfQ in the word file.. it seems that the script is not able to get them from the word file.. – Davide Galloni Mar 18 '20 at 11:00
  • If you already have it, then skip the first step. In your question you wrote `(RfQ)`, while I wrote `("RfQ")`. If you have a variable with the name RfQ, what does it contain? – Sam Mar 18 '20 at 11:06
  • I am quite new and I may not understand everything. I do apologize for that. Let's say the first time I open the file the RfQ DocVariable text will be RfQ, after the first user form usage it may be any number input from the user form. After that I would like that when I press OK the variable is updated in my word file, and if I want to change it I can open the user form again and change it. – Davide Galloni Mar 18 '20 at 11:11
  • Thanks for the explanation. Now I have an additional issue. when I run it, it opens the user form, I get to enter the values and then when I click ok I get compile error: argument not optional" – Davide Galloni Mar 18 '20 at 11:56
  • What line is highlighted? – Sam Mar 18 '20 at 12:09
  • Solved. thank you. Now I am able to use the UserForm to fill in the Variables in my word file. As an additional question, how to I get the variables values from the word file each time I will reopen the UserForm and have them prefilled in the TextBox? I mean, if I want to change some data from the form, I don't want to type in everything again, but I would like to see already the filled in data whenever I run again the macro. Thank you – Davide Galloni Mar 18 '20 at 14:27
  • I suggest that you have a look at custom properties instead. – Sam Mar 18 '20 at 15:00
  • Thanks, any link to some explanation how I can still use the UserForm to modify the custom properties? that would be brilliant – Davide Galloni Mar 18 '20 at 15:28
  • https://learn.microsoft.com/en-us/office/vba/api/word.document.customdocumentproperties – Sam Mar 18 '20 at 15:33
  • I'm so bad at this, sorry to bother you. is there any way to pull the customproperties from Word in VBA and displaythem in the userform, and then update them from userform each time? – Davide Galloni Mar 18 '20 at 15:45
  • I found a workable way!! thank you for all the tips :) – Davide Galloni Mar 18 '20 at 16:05