Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Retrieving page number count from Word DOC
Try the old way:
Dim PageCount as long pagecount=ExecuteExcel4Macro("GET.DOCUMENT(50)") NickHK "Garbunkel" wrote in message ... Hi all, I am working with an Excel VB script (V6.3) to extract data from a set of Microsoft Word DOC files. I need to use a VB solution to retrieve the page number count (the actual number of pages that would be printed if I hit the 'Print' button) from a Microsoft Word DOC file. I tried the following way: Dim ActiveWB As Workbook Set ActiveWB = ActiveWorkbook ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = True HorizBreaks = ActiveWB.Worksheets(1).HPageBreaks.Count HPages = HorizBreaks + 1 VertBreaks = ActiveWB.Worksheets(1).VPageBreaks.Count VPages = VertBreaks + 1 NumPages = HPages * VPages ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = False ActiveWB.Worksheets(1).Cells(1, 1).Value = NumPages ...but it always comes back with a '2' Any assistance I can get on this would be greatly appreciated. - Michael |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Retrieving page number count from Word DOC
You say "doc" and Word, but you code is for an Excel worksheet. Confused ??
If it is Excel, maybe you have the PrintArea set to those 2 pages. NickHK "Garbunkel" wrote in message ... Hi NickHk, Thanks for your response. I tried the code you sent & it still outputted a '2' for the pagecount. The actual number of pages in the doc I worked with is 21. Is this only counting the number of manual page breaks? Please advise. -- "NickHK" wrote: Try the old way: Dim PageCount as long pagecount=ExecuteExcel4Macro("GET.DOCUMENT(50)") NickHK "Garbunkel" wrote in message ... Hi all, I am working with an Excel VB script (V6.3) to extract data from a set of Microsoft Word DOC files. I need to use a VB solution to retrieve the page number count (the actual number of pages that would be printed if I hit the 'Print' button) from a Microsoft Word DOC file. I tried the following way: Dim ActiveWB As Workbook Set ActiveWB = ActiveWorkbook ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = True HorizBreaks = ActiveWB.Worksheets(1).HPageBreaks.Count HPages = HorizBreaks + 1 VertBreaks = ActiveWB.Worksheets(1).VPageBreaks.Count VPages = VertBreaks + 1 NumPages = HPages * VPages ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = False ActiveWB.Worksheets(1).Cells(1, 1).Value = NumPages ...but it always comes back with a '2' Any assistance I can get on this would be greatly appreciated. - Michael |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Retrieving page number count from Word DOC
Garbunkel wrote: The VB code is running from an Excel worksheet, but retrieving & outputting the page count of several Microsoft Word documents. Sorry I didn't make this clear enough before. Here's the code of what I'm trying to do: Dim CurrentWB As Workbook Dim ActiveWB As Workbook Option Explicit 'We use "Option Explicit" to help us check for coding mistakes Sub Analyze_Doc() 'The file handle & other I/O variables Dim fso As Object Dim fil As Object Dim objWord Dim PageCount as long Dim i as integer Dim rsFolderPath as String Dim wordPath 'The document we are currently reading data from Dim currentDocument Set ActiveWB = ActiveWorkbook Set objWord = CreateObject("Word.Application") 'Don't display any messages about documents needing to be converted 'from old Word file formats objWord.DisplayAlerts = 0 'Put the path & filename in cell D1 of Worksheet 1 rsFolderPath = ActiveWB.Worksheets(1).Cells(1, 4).Value i = 1 Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(rsFolderPath) Then For Each fil In fso.GetFolder(rsFolderPath).Files If StrComp(fil.Type, "Microsoft Word Document", vbTextCompare) = 0 Then wordPath = fil 'Open the Word document as read-only 'Open (path, confirmconversions, readonly objWord.Documents.Open wordPath, False, True 'Access the document Set currentDocument = objWord.Documents(1) 'Need to set pagecount = <SOME objWord. OBJECT THAT CONTAINS PAGE COUNT# 'Display pagenumber count, along with file name ActiveWB.Worksheets(1).Cells(i, 1).Value = pagecount ActiveWB.Worksheets(1).Cells(i, 2).Value = currentDocument i = i + 1 End if Next fil End if Thanks again! "NickHK" wrote: You say "doc" and Word, but you code is for an Excel worksheet. Confused ?? If it is Excel, maybe you have the PrintArea set to those 2 pages. NickHK "Garbunkel" wrote in message ... Hi NickHk, Thanks for your response. I tried the code you sent & it still outputted a '2' for the pagecount. The actual number of pages in the doc I worked with is 21. Is this only counting the number of manual page breaks? Please advise. -- "NickHK" wrote: Try the old way: Dim PageCount as long pagecount=ExecuteExcel4Macro("GET.DOCUMENT(50)") NickHK "Garbunkel" wrote in message ... Hi all, I am working with an Excel VB script (V6.3) to extract data from a set of Microsoft Word DOC files. I need to use a VB solution to retrieve the page number count (the actual number of pages that would be printed if I hit the 'Print' button) from a Microsoft Word DOC file. I tried the following way: Dim ActiveWB As Workbook Set ActiveWB = ActiveWorkbook ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = True HorizBreaks = ActiveWB.Worksheets(1).HPageBreaks.Count HPages = HorizBreaks + 1 VertBreaks = ActiveWB.Worksheets(1).VPageBreaks.Count VPages = VertBreaks + 1 NumPages = HPages * VPages ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = False ActiveWB.Worksheets(1).Cells(1, 1).Value = NumPages ...but it always comes back with a '2' Any assistance I can get on this would be greatly appreciated. - Michael |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Retrieving page number count from Word DOC
Garbunkel wrote:
The VB code is running from an Excel worksheet, but retrieving & outputting the page count of several Microsoft Word documents. Sorry I didn't make this clear enough before. Here's the code of what I'm trying to do: Dim CurrentWB As Workbook Dim ActiveWB As Workbook Option Explicit 'We use "Option Explicit" to help us check for coding mistakes Sub Analyze_Doc() 'The file handle & other I/O variables Dim fso As Object Dim fil As Object Dim objWord Dim PageCount as long Dim i as integer Dim rsFolderPath as String Dim wordPath 'The document we are currently reading data from Dim currentDocument Set ActiveWB = ActiveWorkbook Set objWord = CreateObject("Word.Application") 'Don't display any messages about documents needing to be converted 'from old Word file formats objWord.DisplayAlerts = 0 'Put the path & filename in cell D1 of Worksheet 1 rsFolderPath = ActiveWB.Worksheets(1).Cells(1, 4).Value i = 1 Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(rsFolderPath) Then For Each fil In fso.GetFolder(rsFolderPath).Files If StrComp(fil.Type, "Microsoft Word Document", vbTextCompare) = 0 Then wordPath = fil 'Open the Word document as read-only 'Open (path, confirmconversions, readonly objWord.Documents.Open wordPath, False, True 'Access the document Set currentDocument = objWord.Documents(1) 'Need to set pagecount = <SOME objWord. OBJECT THAT CONTAINS PAGE COUNT# 'Display pagenumber count, along with file name ActiveWB.Worksheets(1).Cells(i, 1).Value = pagecount ActiveWB.Worksheets(1).Cells(i, 2).Value = currentDocument i = i + 1 End if Next fil End if Thanks again! "NickHK" wrote: You say "doc" and Word, but you code is for an Excel worksheet. Confused ?? If it is Excel, maybe you have the PrintArea set to those 2 pages. NickHK "Garbunkel" wrote in message ... Hi NickHk, Thanks for your response. I tried the code you sent & it still outputted a '2' for the pagecount. The actual number of pages in the doc I worked with is 21. Is this only counting the number of manual page breaks? Please advise. -- "NickHK" wrote: Try the old way: Dim PageCount as long pagecount=ExecuteExcel4Macro("GET.DOCUMENT(50)") NickHK "Garbunkel" wrote in message ... Hi all, I am working with an Excel VB script (V6.3) to extract data from a set of Microsoft Word DOC files. I need to use a VB solution to retrieve the page number count (the actual number of pages that would be printed if I hit the 'Print' button) from a Microsoft Word DOC file. I tried the following way: Dim ActiveWB As Workbook Set ActiveWB = ActiveWorkbook ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = True HorizBreaks = ActiveWB.Worksheets(1).HPageBreaks.Count HPages = HorizBreaks + 1 VertBreaks = ActiveWB.Worksheets(1).VPageBreaks.Count VPages = VertBreaks + 1 NumPages = HPages * VPages ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = False ActiveWB.Worksheets(1).Cells(1, 1).Value = NumPages ...but it always comes back with a '2' Any assistance I can get on this would be greatly appreciated. - Michael Garbunkel -- If you make direct reference to the Word Object Model then you can use the ActiveDocument Object in Word. First go to Tools|References and check the Microsoft Word 11.0 (or the latest one that you have) Object Library. In this example I'm deleting the number of pages in excess of 1. You could just use the variable lngNumPages to hold the count of the number of pages. Sub DeletePage2(docname As String) Set wdApp = New Word.Application Dim lngNumPages As Long With ActiveDocument.BuiltinDocumentProperties lngNumPages = .Item("Number of Pages") End With If lngNumPages 1 Then ActiveDocument.Tables(1).Delete End If End Sub Hope this helps. Harold |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Retrieving page number count from Word DOC
Garbunkel--
When using the ActiveDocument Object you're actually opening each doc. Therefore, you need to close them. Here's some code to get you started: ActiveDocument.Close _ SaveChanges:=wdDoNotSaveChanges, _ OriginalFormat:=wdWordDocument Next Document See the Visual Basic documentation (in Word) for the Close method of the Document object. Remember, you're working in Word when you're doing this stuff and the Word Object model is somewhat different than Excel's. There is also a way to suppress Alerts but I can't remember it right now. In the code snippet that I wrote above, you're just saying to close the ActiveDocument and move onto the next one. Play around with this idea and see how it works for you. HTH Harold Garbunkel wrote: Thanks Harold! I pasted your code segment into my code (listed below), except in my case I used 'With objWord.ActiveDocument.BuiltinDocumentProperties lngNumPages = .Item("Number of Pages") End With resultant from my earlier definition: Dim objWord Set objWord = CreateObject("Word.Application") The only issue I'm encountering with this now is that it explictly opens each DOC file that it reads and prompts me with a 'Save As' window. This causes a Run-time '5155' error. Is there any way to flag this so that it will not prompt me with any window? LMK if you get a chance. Thanks! -- " wrote: Garbunkel wrote: The VB code is running from an Excel worksheet, but retrieving & outputting the page count of several Microsoft Word documents. Sorry I didn't make this clear enough before. Here's the code of what I'm trying to do: Dim CurrentWB As Workbook Dim ActiveWB As Workbook Option Explicit 'We use "Option Explicit" to help us check for coding mistakes Sub Analyze_Doc() 'The file handle & other I/O variables Dim fso As Object Dim fil As Object Dim objWord Dim PageCount as long Dim i as integer Dim rsFolderPath as String Dim wordPath 'The document we are currently reading data from Dim currentDocument Set ActiveWB = ActiveWorkbook Set objWord = CreateObject("Word.Application") 'Don't display any messages about documents needing to be converted 'from old Word file formats objWord.DisplayAlerts = 0 'Put the path & filename in cell D1 of Worksheet 1 rsFolderPath = ActiveWB.Worksheets(1).Cells(1, 4).Value i = 1 Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(rsFolderPath) Then For Each fil In fso.GetFolder(rsFolderPath).Files If StrComp(fil.Type, "Microsoft Word Document", vbTextCompare) = 0 Then wordPath = fil 'Open the Word document as read-only 'Open (path, confirmconversions, readonly objWord.Documents.Open wordPath, False, True 'Access the document Set currentDocument = objWord.Documents(1) 'Need to set pagecount = <SOME objWord. OBJECT THAT CONTAINS PAGE COUNT# 'Display pagenumber count, along with file name ActiveWB.Worksheets(1).Cells(i, 1).Value = pagecount ActiveWB.Worksheets(1).Cells(i, 2).Value = currentDocument i = i + 1 End if Next fil End if Thanks again! "NickHK" wrote: You say "doc" and Word, but you code is for an Excel worksheet. Confused ?? If it is Excel, maybe you have the PrintArea set to those 2 pages. NickHK "Garbunkel" wrote in message ... Hi NickHk, Thanks for your response. I tried the code you sent & it still outputted a '2' for the pagecount. The actual number of pages in the doc I worked with is 21. Is this only counting the number of manual page breaks? Please advise. -- "NickHK" wrote: Try the old way: Dim PageCount as long pagecount=ExecuteExcel4Macro("GET.DOCUMENT(50)") NickHK "Garbunkel" wrote in message ... Hi all, I am working with an Excel VB script (V6.3) to extract data from a set of Microsoft Word DOC files. I need to use a VB solution to retrieve the page number count (the actual number of pages that would be printed if I hit the 'Print' button) from a Microsoft Word DOC file. I tried the following way: Dim ActiveWB As Workbook Set ActiveWB = ActiveWorkbook ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = True HorizBreaks = ActiveWB.Worksheets(1).HPageBreaks.Count HPages = HorizBreaks + 1 VertBreaks = ActiveWB.Worksheets(1).VPageBreaks.Count VPages = VertBreaks + 1 NumPages = HPages * VPages ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = False ActiveWB.Worksheets(1).Cells(1, 1).Value = NumPages ...but it always comes back with a '2' Any assistance I can get on this would be greatly appreciated. - Michael Garbunkel -- If you make direct reference to the Word Object Model then you can use the ActiveDocument Object in Word. First go to Tools|References and check the Microsoft Word 11.0 (or the latest one that you have) Object Library. In this example I'm deleting the number of pages in excess of 1. You could just use the variable lngNumPages to hold the count of the number of pages. Sub DeletePage2(docname As String) Set wdApp = New Word.Application Dim lngNumPages As Long With ActiveDocument.BuiltinDocumentProperties lngNumPages = .Item("Number of Pages") End With If lngNumPages 1 Then ActiveDocument.Tables(1).Delete End If End Sub Hope this helps. Harold |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Retrieving page number count from Word DOC
I guess you are using late-binding of Word - no reference set and all object
variables declared as Object. If so, you need to define the value, which is 0 according the Object Browser. NickHK "Garbunkel" wrote in message ... Hi Harold, Thanks a lot! I'll try this & see what happens. It doesn't seem to recognize the variable 'wdDoNotSaveChanges' (most likely since this is a WORD based variable) & it still prompts me as to whether to save the file or not. I'll keep digging on this; I have far more to use now on my search. Thanks again. - Michael -- " wrote: Garbunkel-- When using the ActiveDocument Object you're actually opening each doc. Therefore, you need to close them. Here's some code to get you started: ActiveDocument.Close _ SaveChanges:=wdDoNotSaveChanges, _ OriginalFormat:=wdWordDocument Next Document See the Visual Basic documentation (in Word) for the Close method of the Document object. Remember, you're working in Word when you're doing this stuff and the Word Object model is somewhat different than Excel's. There is also a way to suppress Alerts but I can't remember it right now. In the code snippet that I wrote above, you're just saying to close the ActiveDocument and move onto the next one. Play around with this idea and see how it works for you. HTH Harold Garbunkel wrote: Thanks Harold! I pasted your code segment into my code (listed below), except in my case I used 'With objWord.ActiveDocument.BuiltinDocumentProperties lngNumPages = .Item("Number of Pages") End With resultant from my earlier definition: Dim objWord Set objWord = CreateObject("Word.Application") The only issue I'm encountering with this now is that it explictly opens each DOC file that it reads and prompts me with a 'Save As' window. This causes a Run-time '5155' error. Is there any way to flag this so that it will not prompt me with any window? LMK if you get a chance. Thanks! -- " wrote: Garbunkel wrote: The VB code is running from an Excel worksheet, but retrieving & outputting the page count of several Microsoft Word documents. Sorry I didn't make this clear enough before. Here's the code of what I'm trying to do: Dim CurrentWB As Workbook Dim ActiveWB As Workbook Option Explicit 'We use "Option Explicit" to help us check for coding mistakes Sub Analyze_Doc() 'The file handle & other I/O variables Dim fso As Object Dim fil As Object Dim objWord Dim PageCount as long Dim i as integer Dim rsFolderPath as String Dim wordPath 'The document we are currently reading data from Dim currentDocument Set ActiveWB = ActiveWorkbook Set objWord = CreateObject("Word.Application") 'Don't display any messages about documents needing to be converted 'from old Word file formats objWord.DisplayAlerts = 0 'Put the path & filename in cell D1 of Worksheet 1 rsFolderPath = ActiveWB.Worksheets(1).Cells(1, 4).Value i = 1 Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(rsFolderPath) Then For Each fil In fso.GetFolder(rsFolderPath).Files If StrComp(fil.Type, "Microsoft Word Document", vbTextCompare) = 0 Then wordPath = fil 'Open the Word document as read-only 'Open (path, confirmconversions, readonly objWord.Documents.Open wordPath, False, True 'Access the document Set currentDocument = objWord.Documents(1) 'Need to set pagecount = <SOME objWord. OBJECT THAT CONTAINS PAGE COUNT# 'Display pagenumber count, along with file name ActiveWB.Worksheets(1).Cells(i, 1).Value = pagecount ActiveWB.Worksheets(1).Cells(i, 2).Value = currentDocument i = i + 1 End if Next fil End if Thanks again! "NickHK" wrote: You say "doc" and Word, but you code is for an Excel worksheet. Confused ?? If it is Excel, maybe you have the PrintArea set to those 2 pages. NickHK "Garbunkel" wrote in message ... Hi NickHk, Thanks for your response. I tried the code you sent & it still outputted a '2' for the pagecount. The actual number of pages in the doc I worked with is 21. Is this only counting the number of manual page breaks? Please advise. -- "NickHK" wrote: Try the old way: Dim PageCount as long pagecount=ExecuteExcel4Macro("GET.DOCUMENT(50)") NickHK "Garbunkel" wrote in message ... Hi all, I am working with an Excel VB script (V6.3) to extract data from a set of Microsoft Word DOC files. I need to use a VB solution to retrieve the page number count (the actual number of pages that would be printed if I hit the 'Print' button) from a Microsoft Word DOC file. I tried the following way: Dim ActiveWB As Workbook Set ActiveWB = ActiveWorkbook ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = True HorizBreaks = ActiveWB.Worksheets(1).HPageBreaks.Count HPages = HorizBreaks + 1 VertBreaks = ActiveWB.Worksheets(1).VPageBreaks.Count VPages = VertBreaks + 1 NumPages = HPages * VPages ActiveWB.Worksheets(1).DisplayAutomaticPageBreaks = False ActiveWB.Worksheets(1).Cells(1, 1).Value = NumPages ...but it always comes back with a '2' Any assistance I can get on this would be greatly appreciated. - Michael Garbunkel -- If you make direct reference to the Word Object Model then you can use the ActiveDocument Object in Word. First go to Tools|References and check the Microsoft Word 11.0 (or the latest one that you have) Object Library. In this example I'm deleting the number of pages in excess of 1. You could just use the variable lngNumPages to hold the count of the number of pages. Sub DeletePage2(docname As String) Set wdApp = New Word.Application Dim lngNumPages As Long With ActiveDocument.BuiltinDocumentProperties lngNumPages = .Item("Number of Pages") End With If lngNumPages 1 Then ActiveDocument.Tables(1).Delete End If End Sub Hope this helps. Harold |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How do I lighten the word "Page" (and number) in Page Break View? | Excel Discussion (Misc queries) | |||
Counting the number of times a word appears 'anywhere' on a page | New Users to Excel | |||
count number of occurances of a word in a range | Excel Worksheet Functions | |||
Open Microsoft Word and get Page count. | Excel Programming | |||
determine page number in Word document | Excel Programming |