#1
|
|||
|
|||
Textboxes position isn't equal to initial position
Hey Guys,
I'm new to VBA and programming a small macro. Its Task is to put a small TextBox on every page at the top and bottom at the exact same Position. It works fine on 'normal' documents, but on specific the textboxes move their Position by only a few mm. I think this is caused by the documents Header and layout. The only Thing i do is this: Code:
'get coordinates for textbox' position coordinates() = giveRightPosition(0) 'create shape Dim shp As Word.Shape 'Werte unten mittig (321,561,200,25) bei einer SGröße von 11 Set shp = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _ coordinates(0), coordinates(1), 200, 35) With shp .Name = "TB_" & CStr(Rnd()) .Line.Visible = msoFalse .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage .RelativeVerticalPosition = wdRelativeVerticalPositionPage With .TextFrame.TextRange .text = ButtonText .ParagraphFormat.Alignment = wdAlignParagraphCenter With .Font .Name = "Arial" .Size = 11 End With End With End With Code:
Public Function giveRightPosition(position As Integer) As Integer() ' position defines the textbox position: ' 0 = top ' 1 = bottom Set myDoc = ActiveDocument Dim orientation As WdOrientation Dim paperSize As WdPaperSize Dim paperWidth As Integer Dim paperHeight As Integer Dim Message As Integer Dim coordinates(2) As Integer orientation = myDoc.PageSetup.orientation paperSize = myDoc.PageSetup.paperSize paperWidth = myDoc.PageSetup.PageWidth paperHeight = myDoc.PageSetup.PageHeight 'Debug Only test = MsgBox(paperSize, vbYesNo, paperWidth) 'check paper format If paperSize = wdPaperA4 Then coordinates(0) = (paperWidth / 2) - (200 / 2) 'textbox.width is 200 and textalignment is center ' check selected position for calculation of vertical coordinates If position = 0 Then coordinates(1) = 30 'Debug Only Message = MsgBox("detected top!", vbOKOnly, "") Else coordinates(1) = paperHeight - 50 '30 'Debug Only Message = MsgBox("detected bottom!", vbOKOnly, "") End If Else Message = MsgBox("Your paperformat is invalid! Process aborted.", vbOKOnly, "Error") End If Thxxx Last edited by dxdevil; 01-30-2017 at 04:35 AM. Reason: Failed Code |
Tags |
header, textbox, vba code |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Different page number position | Yankel | Word | 2 | 08-01-2014 04:48 AM |
Shadow Position | shanemarkley | Excel | 0 | 07-30-2014 08:23 PM |
DL envelope window position | rogerwilding | Word | 1 | 02-20-2013 01:57 PM |
VBA: how can I know the position on a document? | tinfanide | Excel Programming | 3 | 02-27-2012 03:24 PM |
fix position for a segment in a doc | tai | Word | 3 | 10-20-2011 01:04 PM |