-1

I want to print a number of pages which depends on the number filled in in a Word content control. I tried referring to the object with this:

Set objCC = xFileName.SelectContentControlsByTag("q_179").Item(1)
   
    Total = objCC
    Counter = 0
    Do
        Counter = Counter + 1
        xFolderItem.InvokeVerbEx ("print")
    Loop While Counter < Total

Does anyone know a solution or spot something I'm missing in the code? If you need more info lmk!

Click here to view full code

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Bryan
  • 1
  • 3

2 Answers2

0

The problem is that xFileName.SelectContentControlsByTag("q_179").Item(1) returns a ContentControl object, and a ContentControl object is not a number that can be compared in an expression such as Counter < Total

In this case the way to get the number is to get the ContentControl's Range.Text and convert it to a number (and you might also need to verify that it is a number).

e.g.assuming you know the number is an Integer you could use

Dim ccValue As Integer
ccValue = CInt(xFileName.SelectContentControlsByTag("q_179").Item(1).Range.Text)
.
.
Loop While Counter < ccValue

Because VBA makes a number of assumptions, if you know the value is Numeric you could probably 'get away' with something more like

Dim ccValue As Integer
ccValue = xFileName.SelectContentControlsByTag("q_179").Item(1).Range
.
.
Loop While Counter < ccValue

You may be wondering why it's valid to use .Range rather than .Range.Text but not OK to use (in effect) objCC.Range. That's because .Text is the "Default Member" of the Range object, so when you use .Range without specifying a member, VBA returns the value of the default member. But I think a lot of people would say it's not a very good practice and it's better to specify the member.

To explore Default Members in more detail, open the View->Object Broswer window in Word's VB Editor, and search for the object/class, e.g. Range. Select it, and the relevant Class should be selected in the list of Classes (to the bottom left, probably). Then look through the list of members of the class. If there is a Default Member, its name should be bolded.

You may also be wondering why it's valid to omit the CInt(). It's because VB can "coerce" a string type into being a numeric type, when it contains a number at any rate. But it's usually better to specify an explicit conversion (especially because the result of a conversion can depend on Regional locale settings in e.g. Windows).

jonsson
  • 655
  • 1
  • 3
  • 10
  • Thank you for your answer and explanation, but even with your code the value is still 0. It just seems to be unable to find the content control or at least the value of it. Doesn't matter which data type I use it's always empty, nothing or 0. – Bryan Aug 31 '22 at 06:42
  • @Bryan Personally I would start checking a few things, e.g. have I got the correct Content Control, how many controls does xFileName.SelectContentControlsByTag("q_179").Count return, what is the actual value of the control's .Range.Text before any kind of numeric conversion, and since it's a Rich Text control, could there be something about the text that makes it look like a number when actually it's something else (don't know what, but e.g. the text is inside a table, it's actually not a number but a superscripted "2" or some such. – jonsson Aug 31 '22 at 07:30
0

I'm now using this, it didn't work before because the content control is inside a table. Thanks @jonsson for your answer!

counter = 0
ccvalue = CInt(objWordDocument.SelectContentControlsByTag("q_179").Item(1).Range.Text)
For Each Table In objWordDocument.Tables
  Do
      counter = counter + 1
      xFolderItem.InvokeVerbEx ("print")
      Excel.Application.Wait (Now + TimeValue("00:00:01"))
  Loop While counter < ccvalue
Next
Bryan
  • 1
  • 3