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!

Returning page and line numbers

Status
Not open for further replies.

harky

Technical User
Oct 29, 2001
39
GB
Hi,

I want to create a file which displays all the styles being used in another file - I've managed to get this ok, but I'd like to also insert what page and line number the styles appear on - how do I retrieve these?

Thanks
Harky
 
Hi harky,

There's not really enough in your post to give more details, but the functions ..

Code:
[blue]selection.Information(wdActiveEndPageNumber)[/blue]
and
Code:
[blue]selection.Information(wdFirstCharacterLineNumber)[/blue]

.. will give you the information.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Styles are not associated with line number. Styles are directly associated with paragraphs. Therefore, say if you have 405 paragraphs that use a style named "BodyText", are you asking that your information document list all 405 paragraphs?

yes you can get information on what are being used in a document. That information would, the example above, return ONE answer for the use of "BodyText".

It could be done, but seems....ummm, questionable? What exactly are you trying to do?

Gerry
 
Hi,

I just need to know the exact location of the first instance of any style used in a document, and list it in another document:

eg. Styles file

Heading1 - file23, page 6, line2
Heading2 - file23, page 12, line 8

etc etc

I can get the filename easily enough, but the code I use doesn't seem to let me return page or line numbers, see below

Sub StylesinUse()

-----------------------------------------------------------

Dim sty As Style
Dim strStyleList As Collection
Dim j As Integer
Dim intFileNum As Integer
Dim strTempFilename As String
Dim filenameprint As String

Dim oDoc As Document
filenameprint = ActiveDocument.Name
Set strStyleList = New Collection

For Each sty In ActiveDocument.Styles
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles(sty)
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
' Style type is tested with it's value rather than the wdStyleTypeList
' constant because it is not declared in Word 97.
If Selection.Find.Found = True And sty.Type <> 4 Then
strStyleList.Add Item:=sty
End If
Next sty

strTempFilename = "C:\Styles in Use\" + filenameprint + "_Styles in Use.doc"
Set oDoc = New Document
oDoc.SaveAs FileName:=strTempFilename, FileFormat:=wdFormatDocument
oDoc.Close
Set oDoc = Nothing

intFileNum = FreeFile
Open strTempFilename For Output Access Write As intFileNum
Print #intFileNum, "Styles in Use in FILE ", filenameprint
Print #intFileNum,
Print #intFileNum,
For j = 1 To strStyleList.Count
Print #intFileNum, strStyleList(j)
Next j

Close intFileNum

'printout/save file
Set oDoc = Documents.Open(FileName:=strTempFilename)

Rem If MsgBox("Do you want to print out the document?" & vbCrLf & "Click Yes to Print out. Click No to Save to a file.", vbYesNo) = vbYes Then
Rem oDoc.PrintOut

oDoc.Activate
ActiveDocument.SaveAs FileName:=strTempFilename


oDoc.Close
Set oDoc = Nothing
Rem Kill strTempFilename


End Sub
------------------------------------------------------------
 
Hi harky,

You have a complicated way of writing out your file. I suggest you write each style to your Word document as you find it (and ditch your collection), something like this ..

Code:
[blue]Sub StylesinUse()

Dim sty As Style
Dim strStyleList As Collection
Dim j As Integer
Dim intFileNum As Integer
Dim strTempFilename As String
Dim filenameprint As String

Dim oDoc As Document
filenameprint = ActiveDocument.Name
[green]' Set strStyleList = New Collection NOT NEEDED ANY MORE[/green]

[green]' Moved from below [/green]
strTempFilename = "C:\" + filenameprint + "_Styles in Use.doc"
Set oDoc = New Document

[green]' Re-activate your original doc [/green]
Documents(filenameprint).Activate

For Each sty In ActiveDocument.Styles
    With Selection.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles(sty)
        .Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
    End With
    ' Style type is tested with it's value rather than the wdStyleTypeList
    ' constant because it is not declared in Word 97.
    If Selection.Find.Found = True And sty.Type <> 4 Then
[green]' Write out to new doc here - including page and line number
' Reactivate original doc - to be perfectly honest I don't see why this should be needed ... but it is
' And remove the adding to the collection[/green]
        oDoc.Content.InsertAfter sty.NameLocal & _
            vbTab & "Page " & Selection.Information(wdActiveEndPageNumber) & _
            vbTab & "Line " & Selection.Information(wdFirstCharacterLineNumber) & vbNewLine
        Documents(filenameprint).Activate
[green] '       strStyleList.Add Item:=sty[/green]
    End If
Next sty
[green]' Now done before the Find loop
'strTempFilename = "C:\Styles in Use\" + filenameprint + "_Styles in Use.doc"
'Set oDoc = New Document[/green]
oDoc.SaveAs FileName:=strTempFilename, FileFormat:=wdFormatDocument
[green]' oDoc.Close ' Don't Close Yet[/green]
[green]' Get rid of all this process
' Set oDoc = Nothing

' intFileNum = FreeFile
' Open strTempFilename For Output Access Write As intFileNum
' Print #intFileNum, "Styles in Use in FILE ", filenameprint
' Print #intFileNum,
' Print #intFileNum,
' For j = 1 To strStyleList.Count
'     Print #intFileNum, strStyleList(j)
' Next j

' Close intFileNum
[/green]
'printout/save file
[green]' Not closed - no need to re-open
' Set oDoc = Documents.Open(FileName:=strTempFilename)
[/green]
Rem If MsgBox("Do you want to print out the document?" & vbCrLf & "Click Yes to Print out. Click No to Save to a file.", vbYesNo) = vbYes Then
    Rem oDoc.PrintOut

    oDoc.Activate
    ActiveDocument.SaveAs FileName:=strTempFilename


oDoc.Close
Set oDoc = Nothing
Rem Kill strTempFilename


End Sub[/blue]

I hope I've caught all the changes - come back if you're stuck

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
[smile][rofl][smile]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Is that a cannonball? Remember Culloden!

...bloody English.

Just kidding!

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top