Results 1 to 9 of 9

Thread: [RESOLVED] Help with formatting this word document

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Help with formatting this word document

    Hi all,

    I have a series of letters which I create in MS Word through .net com interop, the main content is fine as it flows down the page however at the very bottom of each page (well actually about 10cm up from the bottom) and about 6cm indented from the left I want to put a reference no, then on the same line about 6cm further on the reference no repeated.

    Id id think I had this sussed but later found that if my address was a line longer or shorter it displaced my reference numbers. Any help please.

    heres my code at the moment,
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Try
    3.             Dim wrdSelection As Word.Selection
    4.             Dim wrdMailMerge As Word.MailMerge
    5.             Dim wrdMergeFields As Word.MailMergeFields
    6.             Dim strToAddSaleNo
    7.             Dim oTable As Word.Table
    8.             Dim StrToAdd As String
    9.  
    10.             Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
    11.             ' Create an instance of Word  and make it visible.
    12.             wrdApp = CreateObject("Word.Application")
    13.             wrdApp.Visible = True
    14.  
    15.             ' Add a new document.
    16.  
    17.             wrdDoc = wrdApp.Documents.Add()
    18.             wrdDoc.Select()
    19.  
    20.             wrdSelection = wrdApp.Selection()
    21.             wrdMailMerge = wrdDoc.MailMerge()
    22.  
    23.             ' Create MailMerge Data file.
    24.             CreateMailMergeDataFile()
    25.             With wrdDoc.PageSetup
    26.                 .TopMargin = 90
    27.                 .LeftMargin = 90
    28.                 .RightMargin = 22
    29.                 .BottomMargin = 20
    30.  
    31.             End With
    32.             InsertLines(4)
    33.  
    34.             ' Insert merge data.
    35.             wrdSelection.ParagraphFormat.Alignment = _
    36.                         Word.WdParagraphAlignment.wdAlignParagraphLeft
    37.             wrdMergeFields = wrdMailMerge.Fields()
    38.             wrdMergeFields.Add(wrdSelection.Range, "Title")
    39.             wrdSelection.TypeText(" ")
    40.             wrdMergeFields.Add(wrdSelection.Range, "FirstName")
    41.             wrdSelection.TypeText(" ")
    42.             wrdMergeFields.Add(wrdSelection.Range, "Surname")
    43.             wrdSelection.TypeParagraph()
    44.             wrdMergeFields.Add(wrdSelection.Range, "Street")
    45.             wrdSelection.TypeParagraph()
    46.             wrdMergeFields.Add(wrdSelection.Range, "Village")
    47.             wrdSelection.TypeParagraph()
    48.             wrdMergeFields.Add(wrdSelection.Range, "Town")
    49.             wrdSelection.TypeParagraph()
    50.             wrdMergeFields.Add(wrdSelection.Range, "County")
    51.             wrdSelection.TypeParagraph()
    52.             wrdMergeFields.Add(wrdSelection.Range, "Postcode")
    53.  
    54.             '////Initial cod eto get to bottom of page///////
    55.             'InsertLines(32)
    56.             ''wrdSelection.Font.Bold = Word.Font
    57.             'wrdSelection.ParagraphFormat.LineSpacing = 7.5
    58.             'InsertLines(3)
    59.             'wrdSelection.ParagraphFormat.LineSpacing = 10
    60.             ''InsertLines(2)
    61.             'wrdSelection.Paragraphs.TabIndent(0.5)
    62.             'wrdSelection.TypeText("RCD")
    63.             ''wrdMergeFields.Add(wrdSelection.Range, "SaleNo")
    64.             ''wrdSelection.TypeText("                                                                                    RCD")
    65.  
    66.             'wrdMergeFields.Add(wrdSelection.Range, "SaleNo")
    67.             ' Go to the end of the document.
    68.             '////////////////////////////////////////////////////
    69.  
    70.             '///Failed attempt at using a table to arrange data layout ///////////
    71.             'oTable = wrdDoc.Tables.Add(wrdSelection.Range, NumRows:=2, _
    72.             '     NumColumns:=2)
    73.  
    74.             'With oTable
    75.             '    '    ' Set the column widths.
    76.             '    .Columns.Item(1).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
    77.             '    .Columns.Item(2).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
    78.             '    .Rows.Item(1).SetHeight(20, Word.WdRowHeightRule.wdRowHeightExactly)
    79.             '    .Rows.Item(2).SetHeight(40, Word.WdRowHeightRule.wdRowHeightExactly)
    80.             '    '    ' Set the shading on the first row to light gray.
    81.             '        .Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex = _
    82.             '         Word.WdColorIndex.wdAuto
    83.             '    ' Bold the first row.
    84.             '    .Rows.Item(1).Range.Bold = False
    85.             '    ' Center the text in Cell (1,1).
    86.             '        .Cell(1, 1).Range.Paragraphs.Alignment = _
    87.             '        Word.WdParagraphAlignment.wdAlignParagraphRight
    88.  
    89.             '    ' Fill each row of the table with data.
    90.             '    .Cell(1, 2).Range.Paragraphs.Alignment = _
    91.             '                  Word.WdParagraphAlignment.wdAlignParagraphRight
    92.  
    93.  
    94.             '    '.Cell(1, 1).Range.InsertAfter(wrdSelection.Range, "SaleNo")
    95.             '    '.Cell(1, 2).Range.InsertAfter(wrdMergeFields.Add(wrdSelection.Range, "SaleNo"))
    96.             'End With
    97.             '////////////////////////////////////////////////////////////////////
    98.             '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    99.  
    100.             ' Perform mail merge.
    101.             wrdMailMerge.Destination = _
    102.                        Word.WdMailMergeDestination.wdSendToNewDocument
    103.             wrdMailMerge.Execute(False)
    104.  
    105.             ' Close the original form document.
    106.             wrdDoc.Saved = True
    107.             wrdDoc.Close(False)
    108.  
    109.             ' Release References.
    110.             wrdSelection = Nothing
    111.             wrdMailMerge = Nothing
    112.             wrdMergeFields = Nothing
    113.             wrdDoc = Nothing
    114.             wrdApp = Nothing
    115.  
    116.             ' Clean up temp file.
    117.             System.IO.File.Delete("C:\DataDoc.doc")
    118.         Catch ex As Exception
    119.             MsgBox(ex.ToString & " Contact IT on x5005 Now ", MsgBoxStyle.OKOnly, "Error")
    120.         End Try
    121.         Me.Cursor = System.Windows.Forms.Cursors.Default
    122.     End Sub

  2. #2
    Lively Member safewithyou247's Avatar
    Join Date
    Dec 2005
    Posts
    64

    Re: Help with formatting this word document

    I found this code for mail merge and it seems to work well. You may want to prot some of the lines into your code.

    VB Code:
    1. Dim wrdApp As Word.Application
    2.     Dim wrdDoc As Word._Document
    3.     Private Sub InsertLines(ByVal LineNum As Integer)
    4.         Dim iCount As Integer
    5.  
    6.         For iCount = 1 To LineNum
    7.             wrdApp.Selection.TypeParagraph()
    8.         Next iCount
    9.     End Sub
    10.     Private Sub FillRow(ByVal Doc As Word.Document, ByVal Row As Integer, ByVal Text1 As String, ByVal Text2 As String, ByVal Text3 As String, ByVal Text4 As String)
    11.         With Doc.Tables.Item(1)
    12.             .Cell(Row, 1).Range.InsertAfter(Text1)
    13.             .Cell(Row, 2).Range.InsertAfter(Text2)
    14.             .Cell(Row, 3).Range.InsertAfter(Text3)
    15.             .Cell(Row, 4).Range.InsertAfter(Text4)
    16.         End With
    17.     End Sub
    18.     Private Sub CreateMailMergeDataFile()
    19.         Dim wrdDataDoc As Word._Document
    20.         Dim iCount As Integer
    21.         wrdDoc.MailMerge.CreateDataSource(Name:="C:\DataDoc.doc", HeaderRecord:="FirstName, LastName, Address, CityStateZip")
    22.  
    23.         wrdDataDoc = wrdApp.Documents.Open("C:\DataDoc.doc")
    24.         For iCount = 1 To 2
    25.             wrdDataDoc.Tables.Item(1).Rows.Add()
    26.         Next iCount
    27.         FillRow(wrdDataDoc, 2, "Jamuna Silks", "1-55, Ist cross", "Thupaakula", "Bagalore")
    28.         FillRow(wrdDataDoc, 3, "Mohan Enterpirses", "1234 5th Street", "Seven Street Junction", "Hyderabad")
    29.         FillRow(wrdDataDoc, 4, "Imax", "Banjara Hills", "Beside assembly", "Hyderabad")
    30.         wrdDataDoc.Save()
    31.         wrdDataDoc.Close(False)
    32.     End Sub
    33.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    34.         Dim wrdSelection As Word.Selection
    35.         Dim wrdMailMerge As Word.MailMerge
    36.         Dim wrdMergeFields As Word.MailMergeFields
    37.         Dim StrToAdd As String
    38.         wrdApp = CreateObject("Word.Application")
    39.         wrdApp.Visible = True
    40.         wrdDoc = wrdApp.Documents.Add()
    41.         wrdDoc.Select()
    42.         wrdSelection = wrdApp.Selection()
    43.         wrdMailMerge = wrdDoc.MailMerge()
    44.         CreateMailMergeDataFile()
    45.         StrToAdd = "CompIndia Infotech P LTD" & vbCr & "An Offshore development for IndyZen Inc USA"
    46.         wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    47.         wrdSelection.TypeText(StrToAdd)
    48.         InsertLines(4)
    49.         wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
    50.         wrdMergeFields = wrdMailMerge.Fields()
    51.         wrdMergeFields.Add(wrdSelection.Range, "FirstName")
    52.         wrdSelection.TypeParagraph()
    53.         wrdMergeFields.Add(wrdSelection.Range, "LastName")
    54.         wrdSelection.TypeParagraph()
    55.         wrdMergeFields.Add(wrdSelection.Range, "Address")
    56.         wrdSelection.TypeParagraph()
    57.         wrdMergeFields.Add(wrdSelection.Range, "CityStateZip")
    58.         InsertLines(2)
    59.         wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    60.         wrdSelection.InsertDateTime(DateTimeFormat:="dddd, MMMM dd, yyyy", InsertAsField:=False)
    61.         InsertLines(2)
    62.         wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
    63.         wrdSelection.TypeText("Dear Sir / Medam")
    64.  
    65.         wrdSelection.TypeText(",")
    66.         InsertLines(2)
    67.         StrToAdd = "The Company was incorporated on 24 February 1998 as Seven Hills InfoTech Pvt Ltd., and was later changed to CompIndia Infotech Pvt Ltd., on 21 March 2000. Since then CompIndia has been providing varied IT services to Clients all over the globe. CompIndia is based on an aim to help companies build better processes and systems, to enhance their performance. Our offerings focus on unleashing the potential of integration to meet your current challenges and future opportunities. Built on a foundation of strong core values and a commitment to customer delight, CompIndia offers a wide range of expertise in the following areas of Information Technology: Software Development Services, Product Development, Electronic Commerce and Consulting. CompIndia specializes in customized IT solutions for industries in the areas of Hotel Management, Financial services, Transportation, Healthcare and a lot more. CompIndia is greatly involved in the creation of proprietary software. With a team of highly skilled and experienced professionals, who come from a wide range of business, management and IT disciplines, we have the know-how to successfully address any IT challenge and help business succeed in an ever changing marketplace."
    68.         wrdSelection.TypeText(StrToAdd)
    69.         InsertLines(2)
    70.         wrdDoc.Tables.Add(wrdSelection.Range, NumRows:=9, NumColumns:=4)
    71.         With wrdDoc.Tables.Item(1)
    72.             .Columns.Item(1).SetWidth(51, Word.WdRulerStyle.wdAdjustNone)
    73.             .Columns.Item(2).SetWidth(170, Word.WdRulerStyle.wdAdjustNone)
    74.             .Columns.Item(3).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
    75.             .Columns.Item(4).SetWidth(111, Word.WdRulerStyle.wdAdjustNone)
    76.             .Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdGray25
    77.             .Rows.Item(1).Range.Bold = True
    78.             .Cell(1, 1).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
    79.             FillRow(wrdDoc, 1, "Emp ID", "Employee Name", "Designation", "Remarks")
    80.             FillRow(wrdDoc, 2, "1024", "Muni Hemadri Babu. Jogi", ".Net Developer", "")
    81.             FillRow(wrdDoc, 3, "1025", "Hemadri", "Web Developer", "")
    82.             FillRow(wrdDoc, 4, "1026", "Hemu", "PHP Developer", "")
    83.             FillRow(wrdDoc, 5, "1027", "Hem", "J2EE Developer", "")
    84.             FillRow(wrdDoc, 6, "1028", "Geetha", ".Net Developer", "")
    85.             FillRow(wrdDoc, 7, "1029", "Rathi", ".Net Developer", "")
    86.             FillRow(wrdDoc, 8, "1030", "Srilatha", ".Net Developer", "")
    87.             FillRow(wrdDoc, 9, "1031", "Srilatha", ".Net Developer", "")
    88.  
    89.         End With
    90.         wrdApp.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast)
    91.         InsertLines(2)
    92.         StrToAdd = "For additional information regarding the " & "CompIndia Infotech P LTD, " & "you can visit our Web site at "
    93.         wrdSelection.TypeText(StrToAdd)
    94.         wrdSelection.Hyperlinks.Add(Anchor:=wrdSelection.Range, Address:="http://www.compindia.comd")  ' Create a string and insert it in the
    95.         StrToAdd = ". Sincerely," & vbCr & vbCr & "Muni Hemadri Babu .J" & vbCr & ".Net Developer" & vbCr
    96.         wrdSelection.TypeText(StrToAdd)
    97.         wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
    98.         wrdMailMerge.Execute(False)
    99.         wrdDoc.Saved = True
    100.         wrdDoc.Close(False)
    101.         wrdSelection = Nothing
    102.         wrdMailMerge = Nothing
    103.         wrdMergeFields = Nothing
    104.         wrdDoc = Nothing
    105.         wrdApp = Nothing
    106.         System.IO.File.Delete("C:\DataDoc.doc")
    107.     End Sub

    Tell me if this is helpful in any way.
    "A candle loses nothing by lighting another candle."

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    That code wont work for me though because it works by just inserting lines to the doc, that means that extra lines in an address will move all the text down. I need to ensure the reference numbers at the bottom are in exactly the same place on each letter. If it helps people understand it is because the letters are getting printed on pre-printed forms i.e I just want it to fill in details.

  4. #4

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    As a quick fix what if I could just ensure that even if a line of the address was missing It still added a row in for it, then I would always know that the refence no's had to be x amount of lines below and just insert x rows.

    How could I implement that idea here the wrdSelection.TypeParagraph() doesnt seem to carriage return regardless like I though?

    VB Code:
    1. wrdSelection.ParagraphFormat.Alignment = _
    2.                         Word.WdParagraphAlignment.wdAlignParagraphLeft
    3.             wrdMergeFields = wrdMailMerge.Fields()
    4.             wrdMergeFields.Add(wrdSelection.Range, "Title")
    5.             wrdSelection.TypeText(" ")
    6.             wrdMergeFields.Add(wrdSelection.Range, "FirstName")
    7.             wrdSelection.TypeText(" ")
    8.             wrdMergeFields.Add(wrdSelection.Range, "Surname")
    9.             wrdSelection.TypeParagraph()
    10.             wrdMergeFields.Add(wrdSelection.Range, "Street")
    11.             wrdSelection.TypeParagraph()
    12.             wrdMergeFields.Add(wrdSelection.Range, "Village")
    13.             wrdSelection.TypeParagraph()
    14.             wrdMergeFields.Add(wrdSelection.Range, "Town")
    15.             wrdSelection.TypeParagraph()
    16.             wrdMergeFields.Add(wrdSelection.Range, "County")
    17.             wrdSelection.TypeParagraph()
    18.             wrdMergeFields.Add(wrdSelection.Range, "Postcode")
    Last edited by FishGuy; Jan 16th, 2006 at 06:45 AM.

  5. #5

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    bump

  6. #6

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    Argh,
    wrdSelection.TypeText("_ ")
    InsertLines(1) will cause a new line to be inserted
    but wrdSelection.TypeText(", ") and wrdSelection.TypeText(" ") will not why?

    VB Code:
    1. wrdMergeFields = wrdMailMerge.Fields()
    2.             wrdMergeFields.Add(wrdSelection.Range, "Title")
    3.             wrdSelection.TypeText(" ")
    4.             wrdMergeFields.Add(wrdSelection.Range, "FirstName")
    5.             wrdSelection.TypeText(" ")
    6.             wrdMergeFields.Add(wrdSelection.Range, "Surname")
    7.             InsertLines(1)
    8.  
    9.             wrdMergeFields.Add(wrdSelection.Range, "Street")
    10.             wrdSelection.TypeText("_ ")
    11.             InsertLines(1)
    12.  
    13.          
    14.  
    15.             wrdMergeFields.Add(wrdSelection.Range, "Village")
    16.             wrdSelection.TypeText(", ")
    17.             InsertLines(1)
    18.  
    19.  
    20.             wrdMergeFields.Add(wrdSelection.Range, "Town")
    21.             wrdSelection.TypeText(" ")
    22.             InsertLines(1)
    23.  
    24.          
    25.  
    26.             wrdMergeFields.Add(wrdSelection.Range, "County")
    27.             wrdSelection.TypeText("_ ")
    28.             InsertLines(1)
    29.  
    30.          
    31.             wrdMergeFields.Add(wrdSelection.Range, "Postcode")

  7. #7

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    Still bump!

  8. #8

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    bump

  9. #9

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Help with formatting this word document

    Ok heres my work around, it seemed that the only way I could guarentee that a newline would be inserted was if it was followed by another character (except "," or " "). However a letter to the public cannot have unnecessary characters on it so I have ended each line to be inserted with "_" I have then made it font color white so it is not visible when printed.
    Not the neatest sollution but a workaround nonethe less.

    VB Code:
    1. wrdSelection.ParagraphFormat.Alignment = _
    2.                         Word.WdParagraphAlignment.wdAlignParagraphLeft
    3.             wrdMergeFields = wrdMailMerge.Fields()
    4.             wrdMergeFields.Add(wrdSelection.Range, "Title")
    5.             wrdSelection.TypeText(" ")
    6.             wrdMergeFields.Add(wrdSelection.Range, "FirstName")
    7.  
    8.             wrdSelection.TypeText(" ")
    9.             wrdMergeFields.Add(wrdSelection.Range, "Surname")
    10.  
    11.             wrdSelection.Font.Color = Word.WdColor.wdColorWhite
    12.             wrdSelection.TypeText("_")
    13.             InsertLines(1)
    14.             wrdSelection.Font.Color = Word.WdColor.wdColorBlack
    15.  
    16.             wrdMergeFields.Add(wrdSelection.Range, "Street")
    17.             wrdSelection.Font.Color = Word.WdColor.wdColorWhite
    18.             wrdSelection.TypeText("_")
    19.             InsertLines(1)
    20.             wrdSelection.Font.Color = Word.WdColor.wdColorBlack
    21.  
    22.  
    23.             wrdMergeFields.Add(wrdSelection.Range, "Village")
    24.             wrdSelection.Font.Color = Word.WdColor.wdColorWhite
    25.             wrdSelection.TypeText("_")
    26.             InsertLines(1)
    27.             wrdSelection.Font.Color = Word.WdColor.wdColorBlack
    28.  
    29.  
    30.             wrdMergeFields.Add(wrdSelection.Range, "Town")
    31.             wrdSelection.Font.Color = Word.WdColor.wdColorWhite
    32.             wrdSelection.TypeText("_")
    33.             InsertLines(1)
    34.             wrdSelection.Font.Color = Word.WdColor.wdColorBlack
    35.  
    36.             wrdMergeFields.Add(wrdSelection.Range, "County")
    37.             wrdSelection.Font.Color = Word.WdColor.wdColorWhite
    38.             wrdSelection.TypeText("_")
    39.             InsertLines(1)
    40.             wrdSelection.Font.Color = Word.WdColor.wdColorBlack
    41.  
    42.             wrdMergeFields.Add(wrdSelection.Range, "Postcode")
    43.             wrdSelection.Font.Color = Word.WdColor.wdColorWhite
    44.             wrdSelection.TypeText("_")
    45.             InsertLines(1)
    46.             wrdSelection.Font.Color = Word.WdColor.wdColorBlack

Posting Permissions

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



Click Here to Expand Forum to Full Width