Order the objects on the word doc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ghaleon
    New Member
    • Aug 2013
    • 28

    Order the objects on the word doc

    I need to place 3 things on this Word Doc. In this order:

    -An Image;
    -Some Text(justify);
    -A table (Aligned with the text);


    I'm using my code to place this in the order that I want, but when I open the .doc, the order is all messed up!

    The table is the first thing to appear, followed by the text, and the image no longer appears.

    Why? And how may I set the position or order of this objects?

    My Currently Code:

    Code:
    Private Sub Command1_Click()
    Dim Word_App            As Word.Application
    Dim Word_Doc            As Word.Document
    Dim Word_Table          As Word.Table
    Dim Word_Range          As Word.Range
    Dim iCount              As Integer
    
    iCount = 1
    Set Word_App = New Word.Application
    Set Word_Doc = Word_App.Documents.Add(DocumentType:=wdNewBlankDocument)
    Set Word_Range = Word_App.ActiveDocument.Range()
    
    'Insert the image
    Word_Doc.Shapes.AddPicture FileName:="C:\p\53.jpg", Left:=100, Top:=0, SaveWithDocument:=True
    
    'Insert some text
    With Word_Range
        .InsertParagraphAfter
        .Font.Name = "Tahoma"
        .Font.Size = 10
        .Text = "Prezado<Field1> (<Field2>),"
        .Text = "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT "
        .Text = "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT ."
        .InsertParagraphAfter
        .Text "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT  TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT  "
        .Text "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT ."
        .InsertParagraphAfter
        .Text "TEXT TEXT TEXT TEXT TEXT TEXT ."
        .Text " TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT ."
        .Text "TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT ."
    End With
    
    'Insert Table
    Set Word_Table = Word_Doc.Tables.Add(Range:=Word_Doc.Range(Start:=1, End:=1), NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitFixed)
    'Config Table
    With Word_Table
        If .Style <> "Tabela com grade" Then
            .Style = "Tabela com grade"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    
    'Populate the table temporary with this text. It will be populated with data from DataBase.
    Word_Table.Columns(1).Cells(1).Range.Text = "Débito"
    Word_Table.Columns(2).Cells(1).Range.Text = "Valor"
    Word_Table.Columns(3).Cells(1).Range.Text = "Juros/Multa"
    Word_Table.Columns(4).Cells(1).Range.Text = "Total"
    Word_Table.Columns(1).Cells(2).Range.Text = "<debito>"
    Word_Table.Columns(2).Cells(2).Range.Text = "<valor>"
    Word_Table.Columns(3).Cells(2).Range.Text = "<jurosmulta>"
    Word_Table.Columns(4).Cells(2).Range.Text = "<total>"
    Word_Table.Columns(4).Cells(1).Range.Text = "Débito"
    Word_Table.Columns(3).Cells(3).Range.Text = "Total"
    Word_Table.Columns(4).Cells(3).Range.Text = "<totalf>"
    
    'Save the .Doc
    Word_Doc.SaveAs FileName:="C:\p\TestandoPicture"
    Word_Doc.Close False
    Word_App.Quit False
    
    End Sub
    Also, what would be the best way to place the text?
    Because doing like i'm doing, it just print the last line of text...

    Obs: I found something about Bookmark would that be my solution ? i'm trying to use it right now...

    Here's an exemaple of the result:
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I don't do a lot of word automation so I can't be sure, but I'm guessing it's because you chose your range before doing anything. Therefore, the range is the beginning of the document. Which means you insert an image at the beginning. Then you write text over the image at the beginning. Then you insert a table at the beginning. You should try redefining the range after each addition to the document.

    Comment

    • Ghaleon
      New Member
      • Aug 2013
      • 28

      #3
      Thanks again³.
      Yes, But I don't know how to set the range to the end of the text... I'm trying to understand this Bookmarks, but I'm not going very well ;x

      UPDATE 2
      Its over haha ;D Thanks so much !!!!
      Finally !

      I did as following for those who may be in the same situation as mine:
      Code:
      Word_Doc.Bookmarks.Add("NameOfTheBookmark")
      OK, now you just created a bookmark with the name you wanted, now to insert an object AFTER that bookmark, in my case, it was a table:
      Code:
      Word_Table = Word_Doc.Tables.Add(Word_Doc.Bookmarks("NameOfTheBookmark").Range,...
      DOne !! HeHe xD

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Glad you got it working. Good luck with the rest of your project.

        Comment

        Working...