PDA

View Full Version : Solved: Few questions



r054
12-10-2007, 03:51 PM
Hi...

I have this simple questions which I'm not sure where to find the answer.

1. How can I detect the end of a document that's opened? I open a document and run a macro on it. But I need my macro to stop when it finds the end of the document. I tried using this code

Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")

but it keeps running and becomes an infinite loop.


2. Is there anyway to check if a line is separated in 2 columns? I have a document where sometime only the first line is divided into 2 columns and the rest are not. What i want to do is either to delete that first line or combine the 2 or more columns into 1 line. Is this possible? I have attached the sample documents.

Thanks for the help.

r054
12-10-2007, 03:51 PM
the other sample

TonyJollans
12-10-2007, 05:20 PM
1. It depends what the code is doing and how it is traversing the document.

2. Yes - but what sort of columns? I can't see an example of this in your sample docs.

r054
12-10-2007, 06:32 PM
1. This is the code that i have for traversing the document

Sub OcrPageNumberMain()

Dim startPage As String
Dim endPage As String
Dim pageTwo As String
Dim docsType As String


startPage = InputBox("The first page number?")
If startPage = "" Then
Exit Sub
End If

bottomPage = MsgBox("Is the page number at the bottom of the page?", vbYesNo + vbQuestion, "Page Number")


Selection.HomeKey unit:=wdStory
Selection.TypeText Text:="Page " & startPage
Selection.TypeParagraph
Selection.TypeParagraph
pageTwo = startPage + 1
If bottomPage = 6 Then
'OcrPageNumberBottom firstPage:=pageTwo
Else
Do Until ActiveDocument.Bookmarks("\Sel") = ActiveDocument.Bookmarks("\EndOfDoc")
Selection.GoTo what:=wdGoToBookmark, Name:="\Page"
Selection.MoveRight unit:=wdCharacter, Count:=1
Selection.EndKey unit:=wdLine, Extend:=wdExtend
If Selection.FormFields.Count > 1 Then
Selection.Delete unit:=wdLine, Count:=1
End If
Selection.TypeText Text:="Page " & pageTwo
Selection.TypeParagraph
Selection.TypeParagraph
pageTwo = pageTwo + 1
Loop
End If
End Sub


2. On Sample1 document... if you click the cursor on the first line of page 2 and so on... you will see it is formatted as 2 columns... In sample2 it's on page 10 and 15

TonyJollans
12-11-2007, 02:20 AM
2. I wish you good luck with this! I presume you have got these documents from OCR software of some sort. To tell how many columns are in the current section you can use:

Selection.Sections(1).PageSetup.TextColumns.Count

and to change the number of columns you can use:

Selection.Sections(1).PageSetup.TextColumns.SetCount 1 ' or however many you want

But you probably also want to remove extra paragraph marks and it would be better if you could remove the extra Section as well, although that can sometimes prove awkward.

TonyJollans
12-11-2007, 02:27 AM
1. You have a lot of work to do with this and I don't have time to look at it closely. You would be better not using the Selection but, with what you have, it is probably as easy as anything to check the end of the selection against the end of the document:

Do Until Selection.End = ActiveDocument.Content.End - 1