Dim thisapplication as new word.application () Dim thisDocument as word.document
ThisDocument = thisapplication.documents.open ("c: /new.doc")
ThisDocument.Range.delete () thisdocument.sections.pagesetup.orientation = wdorientation.wdorientLandscape
DIM Tabstops () as single = {4, 6}
'============================================== Create Header
Thisapplication.screenupdating = false Dim Rng as word.range rng = thisdocument.range (0, 0) RNG.Insertbefore ("SQL Server Data Table Name Exported.") RNG.Font.Name = "Verdana" RNG.Font.Size = 16 rng.insertparagraphafTer () rng.insertparagraphafter ()
RNG.SetRange (start: = rng.end, end: = rng.end)
Dim fmt As Word.ParagraphFormat = rng.ParagraphFormat fmt.TabStops.ClearAll () fmt.TabStops.Add (_ ThisApplication.InchesToPoints (tabStops (0)), _ Word.WdTabAlignment.wdAlignTabLeft, _ Word.WdTabLeader.wdTabLeaderSpaces) fmt. Tabstops.add (_ thisapplication.inchestopoints (tabstops (1)), _ word.wdtabalignment.wdaligntableft, _ word.wdtableader.wdtableaderspaces)
RNG.Text = _ "Author ID" & controlchars.tab & _ "last name" & controlchars.tab & _ "first name" & controlchars.tab '& _' "Phone" & controlchars.tab & _ '"address" & Controlchars.tab & _ '"" & "& _'" "&" & "" "zip" & "" "" contract "&" "rng.font.name =" verdana "RNG .Font.name = 10 rng.font.bold = clng (true) RNG.Font.underline = WDUNDERLINE.WDUNDERLINESINGLERNG.INSERTPARAGRAFTER () RNG.SetRNG.End, end: = rng.end) FMT = RNG .ParagraphFormat
'Set up the tabs for the columns. Fmt.TabStops.ClearAll () fmt.TabStops.Add (_ ThisApplication.InchesToPoints (tabStops (0)), _ Word.WdTabAlignment.wdAlignTabLeft, _ Word.WdTabLeader.wdTabLeaderDots) fmt.TabStops .Add (_ thisapplication.inchestopoints (tabstops (1)), _ word.wdtabalignment.wdaligntableft, _ word.wdtableader.wdtableaderdots
'INSERT A Bookmark To Use for the Insert Data.' Thisdocument.bookmarks.add (_ 'Name: = "DATA", RANGE: = DirectCast (RNG, System.Object) RNG.InsertParagraphafter ()
Dim dr As SqlDataReader Dim cmd As SqlCommand Dim sw As New System.IO.StringWriter () cmd = New SqlCommand ( "select au_id, au_lName, au_fname, phone," & _ "address, city, state, zip, contract from authors" CNN) cmd.connection.open () DR = cmd.ExecuteReader (Commandbehavior.CloseConnection) 'loop through the data, create tab-delimited output: while Dr.read () sw.writeline ("{0} {1} { 2} {3} {4} ", _ DR (0), Controlchars.tab, _ DR (1), Controlchars.tab, _ DR (2), controlchars.tab) End while
'Work with the previously created bookmark:' RNG = thisdocument.bookmarks ("data"). Range rng.text = sw.tostring () RNG.Font.Name = "verdana" RNG.Font.Size = 10
IF not dr is nothing kil.close () end if cmd.connection.close ()
Thisapplication.screenupdating = TRUE
ThisApplication.displayalerts = false thisdocument.save ()
Thisapplication.visible = true 'thisapplication.documents.close ()' thisapplication.quit (word.wdsaveoptions.wdsavechanges)
DIM FileWord As String Dim Currentdate As Date Fileword = "Output" & currentdate.tolongdateString & ".doc"
Page.Response.ContentType = "Application / unknown" Response.AppendHeader ( "Content-Disposition", "attachment; filename =" HttpUtility.UrlEncode (fileWord, System.Text.Encoding.UTF8)) Response.WriteFile ( "c: //new.doc ") Response.end ()