locked
Decimal page numbers in word RRS feed

  • Question

  • Hey Scripting guy, I have done extensive research and have come to you as a last resort as I know your time is prescious. I have multiple documents that need consecutive page numbering, I worked out how to do this successfully with my script (starting with 1 on the first page of document Unit 1 P1.docx to 265 on the start page of the last document Unit 105 D3.docx, but there is a problem. I have a root folder and 9 subfolders (named: Unit 1, Unit 3, Unit 9, Unit 18, Unit 28, Unit 29, Unit 30, Unit 42 and Unit 105), In these subfolder are the documents (named Unit number + P1 - P9 or M1 -M4 or D1 - D3 all with docx ext). In principle each folder is a chapter, each document is a section and each document has pages. I have a seperate document for the TOC in the root folder which picks up the correct headings and page numbers fine with what I have already. But to cut a longer story short, what I would like to appear as page numbers in my document is a decimal number (i.e. first folder as number. first document as number. first{page} which becomes 1.1.1) and have my TOC pick this up as page numbers.

    I have tried a {SEQ} and failed and even if it did work how would the TOC pick this up as a page number.

    Here follow the code snippet for page numbering multiple documents.

    Unit = Array("Unit 1", "Unit 2", "Unit 3", "Unit 9", "Unit 18", "Unit 28", "Unit 29", "Unit 30", "Unit 42", "Unit 105")
    Criteria = Array("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "M1", "M2", "M3", "M4", "D1", "D2", "D3")
    
    Set objWD = createObject("Word.Application")
    objWD.Visible = False
    
       For i=0 to UBound(unit) ' Loop through unit array items
    	   unitName = unit(i)
    	   If unitName = "Unit 105" Then
    		   filePath = sResult & "\unit 9"
    	   Else
    	   ' Concatenate sResult with unitName delimited with "\" 
    		   filePath = sResult & "\" & unitName
    	   End If
    	   For j=0 to UBound(criteria) ' Loop through criteria array items
    		   criteriaName = criteria(j)
    		   thisfile = filePath&"\"&unitName&" "&criteriaName&ext ' Create file name by concatenating filePath with "space" criteriaName and ext
    		   ' Test if file exists
    		   If objFSO.FileExists(thisfile) Then
    			   ' If file exists do something (i.e. process number of pages/modify document start page number)
    			   ' Inform user of file being processed and close popup after 3 seconds
    			   intReturn = objShell.Popup("Processing file - " & thisfile, 3, " ", wshQuestionMark)
    			   ' Open document in word using generated filePath in read/write mode
    			   ' Process first section footer page number and amend to start as intPages (total pages)  + 1
    			   Set objDoc = objWD.Documents.Open (thisFile,0,false)
    			   objDoc.Sections(1).Footers(1).PageNumbers.StartingNumber = intPages + 1 
    			   Set aRange = objDoc.Range
    			   aRange.Collapse 0
    			   aRange.Select
    			   intPages = objWD.Selection.information(1)
    			   objDoc.Save
    			   objDoc.Close
    			   Set objDoc = nothing
    		   Else
    			   'If file doesn't exist do something else (inform of non existant document and close popup after 3 seconds
    			   intReturn = objShell.Popup("File: " & thisfile & " - Does not exist", 3, " ", wshQuestionMark)
    		   End If
    	   Next
    	   filePath = ""
       Next
    objWD.Quit

    sResult derives from a filebrowser script I discovered.

    I have the int i and j which I can use for the first two places in the sequence and I can add a page field to this, but how could I get my TOC to pick up this as the page number?

    I would greatly appreciate any help or guidance you could give, I would use a master document but I would still run into the same problem when editing the documents. The problem is, I have to write and submit each section seperately and maybe not in order so automated numbering becomes an issue.

    Ps I hope this makes sense lol  

    Thursday, October 11, 2012 9:17 PM

Answers

All replies

  • Ps I hope this makes sense lol  

    Sorry, but none of it makes any sense to me. It sounds like you have some very specific requirements and would need to hire a consultant to accomplish what you need.

    Bill

    Friday, October 12, 2012 4:48 PM
  • Hi Bill

    I think I am beginning to see through the mist a bit and heading in the right direction to achieving what I am tried to do.

    The problem started with not being able to use relative links effectively with MSWords built in file link association, all the directories I have are on a USB-Stick. This means I have to constantly updated links when the drive is swapped between computers, even then the links more often than not brake. Initially I just wanted continuous page numbering (Code segment in main post solved this to a degree) throughout the various documents. But I soon realised that I might not be working on and submitting the documents in sequence therefore the page numbering would be out of sink or overlap once everything is collated. So the solution I came up with is to have folder/chapter-document/section-actual page number combination on each page in place of the standard {PAGE} field in the footer. This way the page numbering would be consistent no matter what I was working on. 

    I can write the numbers in the footer on each page as plain text using a modified version of the script I have where I use and concatonate in anyway I choose VAR i for the folder number, VAR j as the document number and introduce a new VAR "p" which will represent the page number. This can be achieved with code something like {For p=0 to UBound(document.range)}. This bit I could possibly work out I think, but then comes the TOC, I am using words predefined headings and am using words automated TOC which picks up, using the {RD} field, headings and the page number from the {PAGE} fields on each corresponding page of every document. If I substitute the {PAGE} fields for plain text (i.e. 1-1-1) the automated TOC would no longer work. I could write the TOC manually, but this defeats the object of automation and could be very time consuming.

    The second part of the theory comes to mind: Write the TOC as the page numbers are being written, the script becomes very complicated to my meagre mind. What I see happening is a search of the current page for headings and store the heading level, Heading text and custom page number in a 3d array if there are no headings then nothing is stored. All this done at the time the numbers are being written to the pages to ensure the correct number sequences are used for the headings. The script then writes/ amends a custom list in a word document.

    I mostly understand the principle of what I want to do and how it might work, it's just understanding how to script it all that I am having a bit of trouble with. It took me nearly a week just to figure out what I have already.

    I know what I have said maybe seems like a compicated way of saying saying what I want, but I though I should say why I want to achieve this too.

    Thanks

    Nimbus (RN: Phil)


    Nimbus3

    Friday, October 12, 2012 7:57 PM
  • Hi,

    You should ask Word VBA questions in the Word for Developers forum.

    Bill

    • Proposed as answer by Bill_Stewart Friday, October 12, 2012 9:39 PM
    • Marked as answer by IamMred Sunday, October 14, 2012 12:15 AM
    Friday, October 12, 2012 8:13 PM
  • Hi

    I have found a script from hey scripting guy and on a VBA forum for searching/finding headings in a word document. These along with what I rewriting in my own script have got me started in the right direction for what I need.

    Thanks


    Nimbus3

    Friday, October 12, 2012 9:35 PM