PDA

View Full Version : Solved: VBA Macro - Alignment Problems in word



Eclipse
01-19-2006, 11:03 AM
Hello All,

Ok, I will try my best to describe my problem.
I have a VB (http://www.tek-tips.com/viewthread.cfm?qid=1179213&page=1#) macro (http://www.tek-tips.com/viewthread.cfm?qid=1179213&page=1#) that modifies a word document. Everything works fine.

I needed some specific image, text and page number alignment in the footer.But the alignment is currently skewed. If the text is smaller in length...alignment of image, text and page number works fine...but this gets skewed if the text is longer. Image hides the text...

then i thought of making the image inline and below is the function i wrote...now when the text is longer the page number is pushed down..also height is increased...

The other option i thought of is having like 3 containers in which i can i have image, text and page number respectively which stay in their fixed position all the time...but i am not sure how to implement it...

any suggestions...

below is my current code (http://www.tek-tips.com/viewthread.cfm?qid=1179213&page=1#)...




Sub FormatFooterLogo()
Dim bFoundFirst As Boolean
Selection.HomeKey unit:=wdStory
If ActiveWindow.ActivePane.View.Type = wdNormalView _
Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
For Each Section In ActiveDocument.Sections
With Section

For Each Shape In .Footers(wdHeaderFooterPrimary).Shapes
Shape.Select
Debug.Print "Primary:: " & Shape.Name & " :: " & Shape.Visible
If UCase(Left(Shape.Name, 7)) = "PICTURE" And Shape.Visible = True Then

Shape.ConvertToInlineShape

End If
Next

For Each Shape In .Footers(wdHeaderFooterFirstPage).Shapes
Shape.Select
Debug.Print "First Page:: " & Shape.Name & " :: " & Shape.Visible
If UCase(Left(Shape.Name, 7)) = "PICTURE" And Shape.Visible = True Then

Shape.ConvertToInlineShape

End If
Next
End With
Next
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Thanks in advance...

Rembo
01-22-2006, 09:25 AM
Hello Eclipse,

I once ran into a similar problem. To solve it I placed a table in the footer, hid the lines and set (forced) the columnwidth of the columns. You might want to try this approach as well.

Rembo

Eclipse
01-22-2006, 09:53 AM
could you provide some sample code or link that has code which does the task of creating table and forcing the column width...

thanks so much...

-Eclipse

Rembo
01-23-2006, 03:19 PM
could you provide some sample code or link that has code which does the task of creating table and forcing the column width...

I added the table by hand but you could do it from VBA as well. One way to get there is like this:

Sub AddTableToFooter()
Dim tMyTable As Table

'Open the footer for editing
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

'Add a table to the document footer
Set tMyTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=1, NumColumns:= _
3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)

'Change column widths to resp. 5, 3 and 1 cm.
With tMyTable
.Columns(1).PreferredWidth = CentimetersToPoints(5)
.Columns(2).PreferredWidth = CentimetersToPoints(3)
.Columns(3).PreferredWidth = CentimetersToPoints(1)
End With

'Return to the main document
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

Hope that helps,

Rembo

fumei
01-23-2006, 07:42 PM
I would just like to point out that it is, in some ways, better to action headers (or footers) directly - as objects.

Dim oTable As Word.Table
Set oTable = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Tables.Add(Range:=Selection.Range, numrows:=1, numcolumns:=3)
Set oTable = Nothing
By actioning directly on the header (above), and specifically the Primary header, it is not necessary to use View. Thereby eliminating the need to change the actual view - the actual image on screen. There is no going between Header view, and Main view. This speeds up the instructions.

fumei
01-23-2006, 07:44 PM
Uh, I could have said that plainer.

You do not need to open the footer for editing, in order to action a table there. Or any thing else, for that matter.

Rembo
01-24-2006, 01:02 AM
Hello Gerry,


I would just like to point out that it is, in some ways, better to action headers (or footers) directly - as objects.

I think that's good advice, thanks. I noticed a little error though because the table is created in the main document (wrong range setting). I altered the code to create the table in the footer:

Sub CreateTableInFooter()
Dim oTable As Word.Table
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
Set oTable = .Range.Tables.Add(Range:=.Range, numrows:=1, numcolumns:=3)
End With
Set oTable = Nothing
End Sub

Come to think of it I used something like that to reference the footer of a document. I just looked it up and found it was the StoryRanges method. This works as well:

Sub CreateTableInFooter2()
Dim oTable As Word.Table
With ActiveDocument.StoryRanges(wdPrimaryFooterStory)
Set oTable = .Tables.Add(Range:=.Parent.StoryRanges(wdPrimaryFooterStory), _
numrows:=1, numcolumns:=3)
End With
Set oTable = Nothing
End Sub

I like the first routine better though, the code is easier to read.

Rembo

Eclipse
01-27-2006, 08:02 AM
Guys,


Sorry for getting back sooner...i really appreciate all your assistance...

Now the problem i am facing is how to grab my image, text and page number and place it in the respective cells of the table that we created inside the footer...

i want to place image in the first cell, text in the second cell and page number inside the third cell of the table...

any sample pseudo code would be really helpful...thanks in advance...

-Eclipse

Rembo
01-30-2006, 05:53 AM
Hello Eclipse,


i want to place image in the first cell, text in the second cell and page number inside the third cell of the table...

Sorry for my late reply, I forgot about your post :doh:
Anyway, here is some code that will do what you want. If you tried to get it to work yourself you might have experienced some problems inserting the page number. The trick here is that you first have to 'collapse' the range and then select it. This will result in a blinking cursor in the cell, rather then a selected cell. You can then insert a page number.

The code:

Sub CreateTableInFooter()

'Insert table in footer
Dim oTable As Word.Table
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
Set oTable = .Range.Tables.Add(Range:=.Range, numrows:=1, numcolumns:=3)
With oTable

'Place picture in first cell
.Cell(1, 1).Range.InlineShapes.AddPicture FileName:="C:\YourDir\YourPicture.jpg", _
LinkToFile:=False, SaveWithDocument:=True

'Place text in second cell
.Cell(1, 2).Range.Text = "Enter your text here"

'Add pagenumber in third cell
With .Cell(1, 3).Range
.Collapse
.Select
.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
End With
End With
End With

'Clean up
Set oTable = Nothing

'Close footer and return to document body
ActiveWindow.ActivePane.Close
End Sub

Hope that helps,

Rembo

Rembo
01-30-2006, 06:00 AM
.

Eclipse
01-30-2006, 02:32 PM
Thanks Rembo, everything is working fine...but i have one final question on this topic...when i have a very long text...how do i make it go to the next line in the same cell...when i have a smaller text everything looks good as shown below...

image | test sample text | 3

but when i have longer text...its screwed up...i want it to look like below

image | adfs adsfkshdf afka2nsf asdfahsdfknasdf adfk| 3
| sdfdsafsdfsdafasdf

thanks

Rembo
01-31-2006, 05:00 AM
Hi Eclipse,


...when i have a very long text...how do i make it go to the next line in the same cell...

I wasn't able to recreate the problem. Could you post a sample document so that I can look at what's going on?

Rembo

Eclipse
02-01-2006, 02:25 PM
Rembo, i am sorry for the confusion...i was looking at the wrong place...the code worked just fine...thanks so much for your invaluable assistance...

Rembo
02-02-2006, 12:00 AM
You're welcome :)

geekgirlau
02-02-2006, 12:45 AM
Eclipse, don't forget to mark this thread as "solved" - you can do this using the Thread Tools option at the top of the page.

Eclipse
02-02-2006, 08:03 AM
i wasnt aware of this functionality..thanks for pointing it to me...i wish i cud rate these guys...they have been really awesome...

thanks