0

So what I would like to do is to do is for the macro to find out the number of pages in the document through:

pages.count

As far as I know, using the search and replace method is in this case not possible, as I cant insert a variable into the

replacement.text

Another method I tried is simply first find the location of the part of the text that should be replaced, select it and then use the

Selection.TypeText Text:=Pages.Count

but this results in an object required error. So this also doesn't work. As such I am sort of out of ideas which method to use to replace the text.

Would someone have an idea how to approach this issue?

2
  • What are you actually trying to do?
    – DavidPostill
    Jul 7, 2018 at 19:57
  • Updating a table of contents that is not word generated (this is unfortunately a requirement). So what I would do is replace 3 - X with whatever the number of pages is.
    – Noir
    Jul 7, 2018 at 20:57

2 Answers 2

0

I don't mean to sound esoteric but there actually is no such thing as real "Page" in Microsoft Word. Since Microsoft Word is a Word Processing program and not a Page Layout program, Microsoft Word depends dynamically on many factors to determine what fits onto a printed page. Some of these include Font, Point Size, Spacing, Margin Settings, Page Type, Page Orientation, Print Driver Settings, and more.

Because of this, to force something inserted onto a fixed page number is not something that can or should be done ... and not knowing more about what exactly you are trying to accomplish limits all of us to mere speculation.

Please describe fully what you are trying to do.

2
  • First a really big macro runs which does a lot of stuff, which likely results in the total of number of pages being changed. In order for me not having to change the total number of pages in the table of contents manually (indeed I just need to change only one single number which will always be something like 3 - 5 to 3 - 6. This table of contents is manually inserted, as such it cant be updated via references > update table), would want to use a macro the count the number of pages and replace the one integer.
    – Noir
    Jul 8, 2018 at 6:12
  • I don't really want to overcomplicate the question, even trough the solution might not be straightforward: How do I use search and replace something, where the replacement value is a variable, that is determined via some kind of feature of the contents of the word document (in my case the number of pages, which I can get using pages.count).
    – Noir
    Jul 8, 2018 at 6:17
0

Use Content Controls. You can label them and then search and replace their text. Below is a screen clip and you can see the one I selected has a labeled of Module Hours. In your VBA code you would then have a routine similar to what I have included as an example below that searched all content controls in the document and then replaced the text of the controls it wanted. enter image description here

        For Each cc In doc.ContentControls
        If cc.Tag = "Course hours" Then
            cc.Range.Text = courseHours
        End If
        If cc.Tag = "Course minutes" Then
            cc.Range.Text = courseMinutes
        End If
        Next

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .