Consulting

Results 1 to 16 of 16

Thread: Solved: VBA Macro - Alignment Problems in word

  1. #1

    Solved: VBA Macro - Alignment Problems in word

    Hello All,

    Ok, I will try my best to describe my problem.
    I have a VB macro 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...




    [VBA]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[/VBA]

    Thanks in advance...

  2. #2

    Re: VBA Macro - Alignment Problems in word

    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

  3. #3
    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

  4. #4

    Re: VBA Macro - Alignment Problems in word

    Quote Originally Posted by Eclipse
    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:

    [VBA]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[/VBA]

    Hope that helps,

    Rembo

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I would just like to point out that it is, in some ways, better to action headers (or footers) directly - as objects.

    [vba]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[/vba]
    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.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  7. #7

    Re: VBA Macro - Alignment Problems in word

    Hello Gerry,

    Quote Originally Posted by fumei
    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:

    [VBA]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[/VBA]

    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:

    [VBA]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[/VBA]

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

    Rembo

  8. #8
    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

  9. #9
    Hello Eclipse,

    Quote Originally Posted by 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
    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:

    [VBA]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[/VBA]

    Hope that helps,

    Rembo
    Last edited by Rembo; 01-30-2006 at 06:05 AM.

  10. #10

  11. #11
    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

  12. #12
    Hi Eclipse,

    Quote Originally Posted by 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

  13. #13
    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...

  14. #14
    You're welcome

  15. #15
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    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.

  16. #16
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •