centering and sizing a word application active window

S

Steve

I am trying to figure out how I can center and size a word application
active window within my vb.net form when the word document is open using
automation so that the word window doesn't extend beyond the form window
(top, left, width or top).

Thanks

Steve
 
S

Steve

Just another note...

Just an add on note...Here's the code I have so far

WordApp.ActiveWindow.View.Zoom.Percentage = 75
WordApp.ActiveWindow.WindowState =
Word.WdWindowState.wdWindowStateNormal
WordApp.ActiveWindow.Height = Me.Height - 300
WordApp.ActiveWindow.Width = Me.Width - 275
WordApp.ActiveWindow.Top = Me.Top + ((Me.Height -
WordApp.ActiveWindow.Height) / 2)
WordApp.ActiveWindow.Left = Me.Left + ((Me.Width -
WordApp.ActiveWindow.Width) / 2)

Where me. is the form initiating the word automation. Me.top = 113.
Me.height = 767. Me.Left = 242. Me.width = 796. So based on my
calculations, ActiveWindow.height = 467. ActiveWindow.width = 521 making it
smaller than the form it displays on top of. ActiveWindow.Top would then
be equal to 263. And ActiveWindow.Left would then be equal to 380. Which
should center the activewindow in the middle of the form. But instead it
displays it to the right and below.

Not sure what I'm doing wrong.

Thanks

Steve
 
C

Cindy M.

Hi Steve,
Where me. is the form initiating the word automation. Me.top = 113.
Me.height = 767. Me.Left = 242. Me.width = 796. So based on my
calculations, ActiveWindow.height = 467. ActiveWindow.width = 521 making it
smaller than the form it displays on top of. ActiveWindow.Top would then
be equal to 263. And ActiveWindow.Left would then be equal to 380. Which
should center the activewindow in the middle of the form. But instead it
displays it to the right and below.

Not sure what I'm doing wrong.
Is it possible it's orienting itself to the windows form, rather than the
screen? Do you get a different result if you use Application.Top, for example?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

Steve

Hi Cindy...

Sorry I didn't get back to your reply for a few days. I was trying some
different things.

Regarding orientating to the windows form - I don't think it is. It seems
to work on the development machine but not the target machine. Possibly due
to resolution since both machines have different resolutions.

To get it to work on the development machine, I modified the code so that it
was using a pixeltopoints conversion. Maybe you or someone else can figure
out what I'm doing wrong or what I need to add.

Thanks

Steve

WordApp.Visible = True
WordApp.ActiveWindow.WindowState =
Word.WdWindowState.wdWindowStateNormal
WordApp.ActiveWindow.View.Zoom.Percentage = 75
WordApp.Resize(WordApp.PixelsToPoints(Int(0.9 * Me.Width),
False), WordApp.PixelsToPoints(Int(0.8 * Me.Height), True))
WordApp.Move(WordApp.PixelsToPoints(Me.Left, False) +
((WordApp.PixelsToPoints(Me.Width, False) - WordApp.Width) / 2), _
WordApp.PixelsToPoints(Me.Top, True) +
((WordApp.PixelsToPoints(Me.Height, True) - WordApp.Height) / 2))
 
S

Steve

Well the problem appears to be centered around the DPI and font size on the
target box versus the DPI and font size on the development box.

For instance on newer laptops, the dpi size is usually 120 whereas on older
monitors - it's usually set at 96. So when the word app window comes up on
the 120 dpi size, the window is bigger. And if I set the dpi on the laptop
down to 96, reboot the laptop, run the app, and then run the process in the
app to create the active window, the active window will display within the
vb.net window right.

So somehow I need to modify the formula for .top, .left, .height, and .width
to factor in the dpi.

Steve
 
C

Cindy M.

Hi Steve,

Glad you were able to track down what's causing the problem :)
Well the problem appears to be centered around the DPI and font size on the
target box versus the DPI and font size on the development box.

For instance on newer laptops, the dpi size is usually 120 whereas on older
monitors - it's usually set at 96. So when the word app window comes up on
the 120 dpi size, the window is bigger. And if I set the dpi on the laptop
down to 96, reboot the laptop, run the app, and then run the process in the
app to create the active window, the active window will display within the
vb.net window right.

So somehow I need to modify the formula for .top, .left, .height, and .width
to factor in the dpi.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy M.

Hi Steve,
Tracked it down - not sure how to solve it though...:)
I was afraid of that :) Unfortunately, this kind of thing
isn't really my forté... If you're working in classic VB or
VBA, I'd try asking in a newsgroup for classic VB (VB6).
The folks there generally have a much better grasp of this
kind of thing.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
S

Steve

Hi Cindy...

I think I figured it out. Here's the code that I used.

Dim Factor As SizeF = Me.CurrentAutoScaleDimensions
Factor.Height /= 96.0F
Factor.Width /= 96.0F

Dim form_wh As New PointF
form_wh.X = WordApp.PixelsToPoints(Me.Width, False)
form_wh.Y = WordApp.PixelsToPoints(Me.Height, True)

Dim wordapp_wh As New PointF
wordapp_wh.X = 0.9 * form_wh.X
wordapp_wh.Y = 0.8 * form_wh.Y

Dim wordApp_scaled_wh As New PointF
wordApp_scaled_wh.X = wordapp_wh.X / Factor.Width
wordApp_scaled_wh.Y = wordapp_wh.Y / Factor.Height

Dim form_lt As New PointF
form_lt.X = WordApp.PixelsToPoints(Me.Left, False)
form_lt.Y = WordApp.PixelsToPoints(Me.Top, True)

Dim wordapp_lt As New PointF
wordapp_lt.X = (form_lt.X + (form_wh.X - wordapp_wh.X) / 2)
wordapp_lt.Y = (form_lt.Y + (form_wh.Y - wordapp_wh.Y) / 2)

Dim wordapp_scaled_lt As New PointF
wordapp_scaled_lt.X = wordapp_lt.X / Factor.Width
wordapp_scaled_lt.Y = wordapp_lt.Y / Factor.Height

WordApp.Resize(wordApp_scaled_wh.X, wordApp_scaled_wh.Y)
WordApp.Move(wordapp_scaled_lt.X, wordapp_scaled_lt.Y)

WordApp.ActiveWindow.WindowState =
Word.WdWindowState.wdWindowStateNormal
WordApp.ActiveWindow.View.Zoom.PageFit =
Word.WdPageFit.wdPageFitBestFit
WordApp.ActiveWindow.Activate()
WordApp.Visible = True

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top