|
-
Jan 13th, 2006, 07:38 AM
#1
Thread Starter
Frenzied Member
[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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
Dim strToAddSaleNo
Dim oTable As Word.Table
Dim StrToAdd As String
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
' Create an instance of Word and make it visible.
wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
' Add a new document.
wrdDoc = wrdApp.Documents.Add()
wrdDoc.Select()
wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
' Create MailMerge Data file.
CreateMailMergeDataFile()
With wrdDoc.PageSetup
.TopMargin = 90
.LeftMargin = 90
.RightMargin = 22
.BottomMargin = 20
End With
InsertLines(4)
' Insert merge data.
wrdSelection.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphLeft
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "Title")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "Surname")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Street")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Village")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Town")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "County")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Postcode")
'////Initial cod eto get to bottom of page///////
'InsertLines(32)
''wrdSelection.Font.Bold = Word.Font
'wrdSelection.ParagraphFormat.LineSpacing = 7.5
'InsertLines(3)
'wrdSelection.ParagraphFormat.LineSpacing = 10
''InsertLines(2)
'wrdSelection.Paragraphs.TabIndent(0.5)
'wrdSelection.TypeText("RCD")
''wrdMergeFields.Add(wrdSelection.Range, "SaleNo")
''wrdSelection.TypeText(" RCD")
'wrdMergeFields.Add(wrdSelection.Range, "SaleNo")
' Go to the end of the document.
'////////////////////////////////////////////////////
'///Failed attempt at using a table to arrange data layout ///////////
'oTable = wrdDoc.Tables.Add(wrdSelection.Range, NumRows:=2, _
' NumColumns:=2)
'With oTable
' ' ' Set the column widths.
' .Columns.Item(1).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
' .Columns.Item(2).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
' .Rows.Item(1).SetHeight(20, Word.WdRowHeightRule.wdRowHeightExactly)
' .Rows.Item(2).SetHeight(40, Word.WdRowHeightRule.wdRowHeightExactly)
' ' ' Set the shading on the first row to light gray.
' .Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex = _
' Word.WdColorIndex.wdAuto
' ' Bold the first row.
' .Rows.Item(1).Range.Bold = False
' ' Center the text in Cell (1,1).
' .Cell(1, 1).Range.Paragraphs.Alignment = _
' Word.WdParagraphAlignment.wdAlignParagraphRight
' ' Fill each row of the table with data.
' .Cell(1, 2).Range.Paragraphs.Alignment = _
' Word.WdParagraphAlignment.wdAlignParagraphRight
' '.Cell(1, 1).Range.InsertAfter(wrdSelection.Range, "SaleNo")
' '.Cell(1, 2).Range.InsertAfter(wrdMergeFields.Add(wrdSelection.Range, "SaleNo"))
'End With
'////////////////////////////////////////////////////////////////////
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Perform mail merge.
wrdMailMerge.Destination = _
Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
' Close the original form document.
wrdDoc.Saved = True
wrdDoc.Close(False)
' Release References.
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDoc = Nothing
wrdApp = Nothing
' Clean up temp file.
System.IO.File.Delete("C:\DataDoc.doc")
Catch ex As Exception
MsgBox(ex.ToString & " Contact IT on x5005 Now ", MsgBoxStyle.OKOnly, "Error")
End Try
Me.Cursor = System.Windows.Forms.Cursors.Default
End Sub
-
Jan 13th, 2006, 08:01 AM
#2
Lively Member
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:
Dim wrdApp As Word.Application
Dim wrdDoc As Word._Document
Private Sub InsertLines(ByVal LineNum As Integer)
Dim iCount As Integer
For iCount = 1 To LineNum
wrdApp.Selection.TypeParagraph()
Next iCount
End Sub
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)
With Doc.Tables.Item(1)
.Cell(Row, 1).Range.InsertAfter(Text1)
.Cell(Row, 2).Range.InsertAfter(Text2)
.Cell(Row, 3).Range.InsertAfter(Text3)
.Cell(Row, 4).Range.InsertAfter(Text4)
End With
End Sub
Private Sub CreateMailMergeDataFile()
Dim wrdDataDoc As Word._Document
Dim iCount As Integer
wrdDoc.MailMerge.CreateDataSource(Name:="C:\DataDoc.doc", HeaderRecord:="FirstName, LastName, Address, CityStateZip")
wrdDataDoc = wrdApp.Documents.Open("C:\DataDoc.doc")
For iCount = 1 To 2
wrdDataDoc.Tables.Item(1).Rows.Add()
Next iCount
FillRow(wrdDataDoc, 2, "Jamuna Silks", "1-55, Ist cross", "Thupaakula", "Bagalore")
FillRow(wrdDataDoc, 3, "Mohan Enterpirses", "1234 5th Street", "Seven Street Junction", "Hyderabad")
FillRow(wrdDataDoc, 4, "Imax", "Banjara Hills", "Beside assembly", "Hyderabad")
wrdDataDoc.Save()
wrdDataDoc.Close(False)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
Dim StrToAdd As String
wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
wrdDoc = wrdApp.Documents.Add()
wrdDoc.Select()
wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
CreateMailMergeDataFile()
StrToAdd = "CompIndia Infotech P LTD" & vbCr & "An Offshore development for IndyZen Inc USA"
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
wrdSelection.TypeText(StrToAdd)
InsertLines(4)
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "LastName")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Address")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "CityStateZip")
InsertLines(2)
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
wrdSelection.InsertDateTime(DateTimeFormat:="dddd, MMMM dd, yyyy", InsertAsField:=False)
InsertLines(2)
wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
wrdSelection.TypeText("Dear Sir / Medam")
wrdSelection.TypeText(",")
InsertLines(2)
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."
wrdSelection.TypeText(StrToAdd)
InsertLines(2)
wrdDoc.Tables.Add(wrdSelection.Range, NumRows:=9, NumColumns:=4)
With wrdDoc.Tables.Item(1)
.Columns.Item(1).SetWidth(51, Word.WdRulerStyle.wdAdjustNone)
.Columns.Item(2).SetWidth(170, Word.WdRulerStyle.wdAdjustNone)
.Columns.Item(3).SetWidth(100, Word.WdRulerStyle.wdAdjustNone)
.Columns.Item(4).SetWidth(111, Word.WdRulerStyle.wdAdjustNone)
.Rows.Item(1).Cells.Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdGray25
.Rows.Item(1).Range.Bold = True
.Cell(1, 1).Range.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
FillRow(wrdDoc, 1, "Emp ID", "Employee Name", "Designation", "Remarks")
FillRow(wrdDoc, 2, "1024", "Muni Hemadri Babu. Jogi", ".Net Developer", "")
FillRow(wrdDoc, 3, "1025", "Hemadri", "Web Developer", "")
FillRow(wrdDoc, 4, "1026", "Hemu", "PHP Developer", "")
FillRow(wrdDoc, 5, "1027", "Hem", "J2EE Developer", "")
FillRow(wrdDoc, 6, "1028", "Geetha", ".Net Developer", "")
FillRow(wrdDoc, 7, "1029", "Rathi", ".Net Developer", "")
FillRow(wrdDoc, 8, "1030", "Srilatha", ".Net Developer", "")
FillRow(wrdDoc, 9, "1031", "Srilatha", ".Net Developer", "")
End With
wrdApp.Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast)
InsertLines(2)
StrToAdd = "For additional information regarding the " & "CompIndia Infotech P LTD, " & "you can visit our Web site at "
wrdSelection.TypeText(StrToAdd)
wrdSelection.Hyperlinks.Add(Anchor:=wrdSelection.Range, Address:="http://www.compindia.comd") ' Create a string and insert it in the
StrToAdd = ". Sincerely," & vbCr & vbCr & "Muni Hemadri Babu .J" & vbCr & ".Net Developer" & vbCr
wrdSelection.TypeText(StrToAdd)
wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
wrdDoc.Saved = True
wrdDoc.Close(False)
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDoc = Nothing
wrdApp = Nothing
System.IO.File.Delete("C:\DataDoc.doc")
End Sub
Tell me if this is helpful in any way.
"A candle loses nothing by lighting another candle."
-
Jan 13th, 2006, 08:20 AM
#3
Thread Starter
Frenzied Member
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.
-
Jan 13th, 2006, 08:36 AM
#4
Thread Starter
Frenzied Member
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:
wrdSelection.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphLeft
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "Title")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "Surname")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Street")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Village")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Town")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "County")
wrdSelection.TypeParagraph()
wrdMergeFields.Add(wrdSelection.Range, "Postcode")
Last edited by FishGuy; Jan 16th, 2006 at 06:45 AM.
-
Jan 16th, 2006, 06:45 AM
#5
Thread Starter
Frenzied Member
Re: Help with formatting this word document
-
Jan 16th, 2006, 09:28 AM
#6
Thread Starter
Frenzied Member
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:
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "Title")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "Surname")
InsertLines(1)
wrdMergeFields.Add(wrdSelection.Range, "Street")
wrdSelection.TypeText("_ ")
InsertLines(1)
wrdMergeFields.Add(wrdSelection.Range, "Village")
wrdSelection.TypeText(", ")
InsertLines(1)
wrdMergeFields.Add(wrdSelection.Range, "Town")
wrdSelection.TypeText(" ")
InsertLines(1)
wrdMergeFields.Add(wrdSelection.Range, "County")
wrdSelection.TypeText("_ ")
InsertLines(1)
wrdMergeFields.Add(wrdSelection.Range, "Postcode")
-
Jan 17th, 2006, 04:14 AM
#7
Thread Starter
Frenzied Member
Re: Help with formatting this word document
-
Jan 18th, 2006, 05:09 AM
#8
Thread Starter
Frenzied Member
Re: Help with formatting this word document
-
Jan 18th, 2006, 07:11 AM
#9
Thread Starter
Frenzied Member
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:
wrdSelection.ParagraphFormat.Alignment = _
Word.WdParagraphAlignment.wdAlignParagraphLeft
wrdMergeFields = wrdMailMerge.Fields()
wrdMergeFields.Add(wrdSelection.Range, "Title")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "FirstName")
wrdSelection.TypeText(" ")
wrdMergeFields.Add(wrdSelection.Range, "Surname")
wrdSelection.Font.Color = Word.WdColor.wdColorWhite
wrdSelection.TypeText("_")
InsertLines(1)
wrdSelection.Font.Color = Word.WdColor.wdColorBlack
wrdMergeFields.Add(wrdSelection.Range, "Street")
wrdSelection.Font.Color = Word.WdColor.wdColorWhite
wrdSelection.TypeText("_")
InsertLines(1)
wrdSelection.Font.Color = Word.WdColor.wdColorBlack
wrdMergeFields.Add(wrdSelection.Range, "Village")
wrdSelection.Font.Color = Word.WdColor.wdColorWhite
wrdSelection.TypeText("_")
InsertLines(1)
wrdSelection.Font.Color = Word.WdColor.wdColorBlack
wrdMergeFields.Add(wrdSelection.Range, "Town")
wrdSelection.Font.Color = Word.WdColor.wdColorWhite
wrdSelection.TypeText("_")
InsertLines(1)
wrdSelection.Font.Color = Word.WdColor.wdColorBlack
wrdMergeFields.Add(wrdSelection.Range, "County")
wrdSelection.Font.Color = Word.WdColor.wdColorWhite
wrdSelection.TypeText("_")
InsertLines(1)
wrdSelection.Font.Color = Word.WdColor.wdColorBlack
wrdMergeFields.Add(wrdSelection.Range, "Postcode")
wrdSelection.Font.Color = Word.WdColor.wdColorWhite
wrdSelection.TypeText("_")
InsertLines(1)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|