3
votes

I am writing a vba macro that checks that word documents are formatted correctly to meet certain specifications. One of the things I have to check for are the left margins of each line - different paragraphs are supposed to have different first indents and hanging indents depending on the context. This should be as simple as checking the style, but unfortunately it is not - some of the documents use styles to change the indents, but others use manual spaces and tabs to position the text correctly. So I need some way to check the actual physical position of the first physical character in each Document.Paragraphs. I have no problem getting a range with the first visible character in the paragraph, but I'm not sure about getting the distance from the margin (or from the left side of the page - doesn't make a difference because the margins are consistent).

I found the Window.GetPoint method, but I'm nervous to use it, because that is based on the actual physical location on the screen. This macro is going to be used on different computers, with different versions of word, and I'm not sure about how it is affected by other view settings (like print layout, zoom, etc.) Is there a consistent way to use this method to determine the distance from the margin?

The other method would be (because all of the documents are in Courier New 12) to look at the firstindent property of the style, and the count manually all of the spaces and tabs (but that would need to take into account tabstops). This I'm also not sure how to do. I would think that there should be a much simpler way of doing this, but I can't find it, so if anyone has any suggestions I would really appreciate any help.

1
You have all the information you need in the document itself: ActiveDocument.PageSetup.LeftMargin, ActiveDocument.PageSetup.PageWidth, etc. The only bit requiring some work is determining the actual width of the strings (including blank spaces). There seems to not be any in-built function for this in VBA and thus you would have to write some code (or copy it from somewhere). In your situation, I would do some research/testing to find out all the relevant information present in PageSetup, would create a preliminary version and come back here with more clearly-defined problems.varocarbas
If the indents of paragraphs need to be consistent then you might consider checking if the beginning of a paragraph is whitespace (tabs, spaces). If so, remove these and apply the correct indenting using either a style or an indent setting. This is just a suggestion and I appreciate that it might not be appropriate for your requirements.Andy G
Andy G - your suggestion would work and be easy for the final result, but the problem is that I also need to produce an error report wherever it wasn't done correctly.clum
varocarbas - As I mentioned in my question, the margins aren't the issue. My concern is what you mentioned - finding the width of the whitespace. The truth is, I didn't experiment enough with Window.GetPoint, it could be that all I need to do is use Window.GetPoint to figure out the width of any physically typed whitespace and add it to the Indent and FirstIndent of the paragraph's style.clum

1 Answers

3
votes

It was there after all! Range.Information(wdHorizontalPositionRelativeToPage)