Wednesday, September 14, 2011

Get a document's paper size via automation in VFP

Here is a question inside Foxite that took my interest:

how to make code this in vfp, to get the Paper's Size of the document file in (MS OFFICE)?

So I also thought, how?  And I can't think of any other way but automation.  Problem though is it seems you have to recompute the page width and height before you can attain the proper result as those are computed inside Word via "InchesToPoints" and I cannot find a property to get that computed result.  Solution? Try to compute it yourself. :)

Anyway, this seem to work.  Maybe there is a better way than this but this is what I can think of right now:


*******Beginning of Code

LOCAL loWord as word.application, lnHeight, lnWidth, lcSize, lcPaperSize
loWord = CREATEOBJECT("word.application")
lcFile = GETFILE("doc,docx")
loWord.Documents.Open(m.lcFile)
lnHeight = ROUND((loWord.Selection.PageSetup.PageHeight/72.0231),2)
lnWidth = ROUND((loWord.Selection.PageSetup.PageWidth/71.98911),2)
lcSize = TRANSFORM(m.lnWidth)+"x"+TRANSFORM(m.lnHeight)
DO CASE
   CASE m.lcSize = "8.50x11" OR m.lcSize = "11x8.50"
     lcPaperSize = "Letter"
   CASE m.lcSize = "8.50x14" OR m.lcSize = "14x8.50"
     lcPaperSize = "Legal"
   CASE m.lcSize = "11x16.99" OR m.lcSize = "16.99x11"
     lcPaperSize = "Tabloid"
   * and so on......
   OTHERWISE
   lcPaperSize = "Unrecognized Size"
ENDCASE
MESSAGEBOX(m.lcPaperSize)
loWord.Quit

******* End of Code


If you think this approach will work on your side, better create a table to store the widths, heights and paper sizes; and use that table to get the appropriate document's paper size. Widths and Heights should be stored as numeric. I just want to show here how we can see things like (8.5x11 for letter) but best save those as numeric and seek those numeric values in the table itself.  Also as you can see, you can determine further if the document is in landscape or portrait, i.e., if width is bigger than height, it is landscape; otherwise portrait.

And like some of my blogs, I decided to post it here so maybe some other non-members of foxite who will find this somewhat useful  can maybe implement this on their side.  Enjoy!

No comments:

Post a Comment