Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the Page Number from a word document in code

Status
Not open for further replies.

GComyn

Programmer
Jul 24, 2001
177
US
I'm trying to take a mail merge document with multiple "accounts" in it, and create PDF files for each account.

How would one get the page number of the selection? I am using the find method to find the words "Account No. " then putting the account number in a variable. I want to get the page number that the current selection is located, but can't seem to find anything on the net that will help. the only thing I've been able to find about word is page numbering, and that is not what I want.

I'd appreciate any help I can get.

Thanks
GComyn
 
Selection.Information(wdActiveEndAdjustedPageNumber)


unknown
 
Thanks!

That does what I want... now... .PrintOut is not working like the help says it should.... I'm using

objWordDoc.PrintOut Pages:=strPages

Where strPages = "1-7"

but it is printing out the entire document.

GComyn
 
Hi GComyn,

Your printout problem arises from the fact that every Section of the mailmerge output document has pages 1 to whatever (presumably no more than 7). What you need to do is to specify the Sections to print (or even the pages within a particular Section if you don't want to print the whole Section).

Also, since you're outputting to PDF, the question arises as to whether you have Adobe Acrobat Professional (V7 or later) installed. If you do, that app already has the capacity to generate individual mailmerge PDFs for Word 2003 and later.


Cheers
[MS MVP - Word]
 
I didn't realize that... thanks for the information... however, I cannot use Adobe Acrobat for this process, since I have the completed documents to split, not a template that is creating the mail merge.

So... I'll have to go another way... I'm thinking about copying the pages in question into another word document, then creating a pdf from that.

BTW... I'm using an access database to do this, since I have several years worth of documents that need to be split, and that is the environment that I am most familiar with.

GComyn
 
Or (although you do not state what it is you are actually doing), if you want to print EACH Section...
Code:
Dim j As Long
For j = 1 To ActiveDocument.Sections.Count
      Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, _
      Item:=wdPrintDocumentContent, Copies:=1, _
      Pages:="s" & j, PageType:=wdPrintAllPages
This prints each Section separately.


unknown
 
Thanks.

The project that I am working on is a mail merge a list. The distinguishing variable is the account number. Once the mail merge has been created, there is no way to get this number, except to search the pages for it.

I want the PDF files to be named by this account number. So, to do this, I need to determine what the account number is for each section.

I've got some code that works for the template that I have, but there are about 25 templates, and not all have the same format.

right now, I'm cutting each section, then creating a new document, printing that out (I have my pdfcreator set to automatically print, with a timestap at the beginning of the filename), then using 'name as' I rename the pdf file to the correct folder/filename.

I do this until there is only 1 page, which means that the document is empty, then I close the document (which was opened readonly), empty the clipboard, then close the word application.

if there is interest, I'll put the code here for others to peruse, and maybe expand/correct/make better.

thanks

GComyn
 
I'm cutting each section, then creating a new document, printing that out "

What is the difference between that, and my code to print each section?

If you are putting out to PDF, use code to get your account name and then use the print by section routine.

Or am I missing something?


unknown
 
Nothing is wrong with it. I've tried it, and it will print out each section, but, I can't determine what the account number is for the section that is printing. So I can't rename the file properly.

using the cut/paste method, I know that the only account number in the document is the one that I want.

also, when I tried it, it put a footer that shows the total number of pages in the document... the sections are about 6 pages, and the document that I'm testing is 12 pages, so the footer on the last page is 'Page 6 of 12'... making it seem that there should be more pages...

it also prints out the entire document, which I don't want.

 
Nothing is wrong with it. I've tried it, and it will print out each section, but, I can't determine what the account number is for the section that is printing. So I can't rename the file properly."

How are you determining the acocunt number in the new (cut/paste) document? Manually looking? If you are using code, then use the SAME code, except just for the Section.

"using the cut/paste method, I know that the only account number in the document is the one that I want."

The it will be the only account number in the Section. Same thing.

"also, when I tried it, it put a footer that shows the total number of pages in the document... the sections are about 6 pages, and the document that I'm testing is 12 pages, so the footer on the last page is 'Page 6 of 12'... making it seem that there should be more pages..."

True. You could certainly fix up the footers via code, but it would be fussy, so I see your point.

"it also prints out the entire document, which I don't want." As I posted: "if you want to print EACH Section..."

The alternative is easy..if you do NOT want to print each Section..., then...don't.


unknown
 
Ok... here is the code that I'm using to do this. As you can see, I'm using the Selection.Find method to find the account number, and them I'm using the number of lines down from the top of the document to get the date. If anyone can suggest a better/more efficient way to do this, I'd appreciate it.

The documents that I am going to be processing are from different regions, and (from what I'm told) legally need to be slightly different formats. That is where the select case comes in.

Code:
Option Compare Database
Option Explicit

Const PATH_NAME As String = "G:\RLCfinance\Agency\Stan\National\Miscellaneous\Testing_New_Database\Strip_NOIs\"
Const PRINT_PATH_NAME As String = "G:\RLCfinance\Agency\Stan\ReportsToMail\"

Declare Function GetDesktopWindow Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal Hwnd As Long) As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function CloseClipboard Lib "User32" () As Long

Public Sub Split_NOIs(strFileName As String)
    Dim objWordApp      As Word.Application
    Dim objWordDoc      As Word.Document
    Dim strFN           As String
    Dim strNFN          As String
    Dim strOFN          As String
    Dim strAccount      As String
    Dim strDate         As String
    Dim strPDF_path     As String
    Dim x               As Integer
    Dim DeskHwnd        As Long
    Dim junk            As Long
    Dim intDateLine     As Long
    Dim strAcctFind     As String
    Dim lngCount        As Long

    DeskHwnd = GetDesktopWindow()
    Set objWordApp = New Word.Application
    strOFN = strFileName
    strFN = PATH_NAME & strOFN
    With objWordApp
        .Visible = False
        Select Case Left(strOFN, 3)
            Case "100"
                intDateLine = 11
                If Left(strOFN, 4) = "1000" Then
                    strAcctFind = "RE:"
                Else
                    strAcctFind = "Account No.:"
                End If
            Case "200"
                intDateLine = 9
                strAcctFind = "Account No.:"
            Case "201"
                intDateLine = 5
                strAcctFind = "Account No.:"
            Case "300"
                intDateLine = 11
                strAcctFind = "Account No.:"
            Case "301", "601", "701", "702", "802", "850"
                intDateLine = 5
                strAcctFind = "Account No.:"
            Case "400", "401", "404", "801", "901"
                intDateLine = 6
                strAcctFind = "Account No."
            Case "500"
                intDateLine = 12
                strAcctFind = "Account No.:"
            Case "501"
                intDateLine = 5
                strAcctFind = "Account No.:"
            Case "800"
                If InStr(1, strOFN, "MDN") > 0 Then
                    intDateLine = 16
                    strAcctFind = "Account No.:"
                Else
                    intDateLine = 11
                    strAcctFind = "Account No.:"
                End If
            Case Else
                Exit Sub
        End Select
        Set objWordDoc = .Documents.Open(strFN, , True)
        .Selection.EndKey unit:=wdStory
        .Selection.HomeKey unit:=wdStory
        lngCount = Nz([Forms]![frmSplitNOIs]![txtAccounts].Value, 0)
        Do Until .Selection.Information(wdNumberOfPagesInDocument) = 1
            .Selection.Extend
            .Selection.MoveEnd unit:=wdSection, Count:=1
            .Selection.Cut
            .Documents.Add Visible:=True
            .Selection.PasteAndFormat Type:=wdFormatOriginalFormatting
            .Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
            .Selection.Delete unit:=wdCharacter, Count:=1
            .Selection.HomeKey unit:=wdStory
            .Selection.Find.Execute findtext:=strAcctFind, Forward:=True, Wrap:=wdFindStop
            .Selection.EndKey unit:=wdLine
            .Selection.MoveLeft unit:=wdWord, Count:=1, Extend:=wdExtend
            strAccount = .Selection.Text
            .Selection.HomeKey unit:=wdStory
            .Selection.MoveDown unit:=wdParagraph, Count:=intDateLine
            .Selection.EndKey unit:=wdLine, Extend:=wdExtend
            .Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
            strDate = .Selection.Text
            strPDF_path = PATH_NAME & Format(CDate(strDate), "yyyymm") & "\"
            Debug.Print strAccount & " - " & strDate
            If Dir(strPDF_path, vbDirectory) = "" Then
                MkDir strPDF_path
            End If
            .PrintOut Background:=False
            Do
                strFN = Dir(PRINT_PATH_NAME & "*" & .ActiveDocument.Name & ".pdf")
            Loop Until strFN <> ""
            On Error Resume Next
            strNFN = strPDF_path & strAccount & "-NOI.pdf"
            If Dir(strNFN) <> "" Then
                Do
                    x = x + 1
                    strNFN = strPDF_path & strAccount & "-NOI_" & Right("000" & x, 2) & ".pdf"
                Loop Until Dir(strNFN) = ""
            End If
            lngCount = lngCount + 1
            
            [Forms]![frmSplitNOIs]![txtDate].Value = strDate
            [Forms]![frmSplitNOIs]![txtAccount].Value = strAccount & IIf(x > 0, " - " & x, "")
            [Forms]![frmSplitNOIs]![txtAccounts].Value = lngCount
            [Forms]![frmSplitNOIs].Repaint
            
            Do
                Name PRINT_PATH_NAME & strFN As strNFN
            Loop Until (Dir(strPDF_path & strAccount & "-NOI.pdf") <> "") And (Dir(PRINT_PATH_NAME & strFN) = "")
            On Error GoTo 0
            .ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
            x = 0
        Loop
    End With
    
    objWordDoc.Close SaveChanges:=wdDoNotSaveChanges
    junk = OpenClipboard(DeskHwnd)
    junk = EmptyClipboard()
    junk = CloseClipboard()
    objWordApp.Quit
    Set objWordDoc = Nothing
    Set objWordApp = Nothing
    
End Sub

GComyn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top