De VB.Net a un Archivo Word

02/08/2006 - 16:20 por Guillermo | Informe spam
Que posibilidades hay de escribir o modificar en un archivo de microsoft
word, ya creado.

Gracias...

Preguntas similare

Leer las respuestas

#1 Squiur
03/08/2006 - 16:43 | Informe spam
SI SE PUEDE


Dim WordApp As New Word.Application()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim doc As New Word.Document()
doc = WordApp.Documents.Add()
Dim str As String
str = "Text Formatting:"
With WordApp.Selection
.Font.Size = WordApp.Selection.Font.Size + 2
.Font.Bold = True
.TypeText(str)
.Font.Size = WordApp.Selection.Font.Size - 2
.Font.Bold = False
.TypeParagraph()
.Font.Color = Word.WdColor.wdColorDarkRed
.Font.Italic = False
.TypeText("This sentence will appear in red. ")

.TypeParagraph()
.Font.Color = Word.WdColor.wdColorBlack
.Font.Italic = True
.Font.Size = WordApp.Selection.Font.Size + 2
.TypeText("Text color was reset to black, " & _
"but the font size was increased by two points")
End With
Dim fName As String
SaveFileDialog1.Filter = "Documents|*.doc"
SaveFileDialog1.ShowDialog()
fName = SaveFileDialog1.FileName
If fName <> "" Then
Try
doc.SaveAs(fName)
Catch exc As Exception
MsgBox("Failed to save document" & _
vbCrLf & exc.Message)
End Try
End If
MsgBox("The document contains " & doc.Paragraphs.Count & "
paragraphs " & vbCrLf & _
doc.Words.Count & " words and " & doc.Characters.Count & "
words")
doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
WordApp.Quit(False)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim doc As New Word.Document()
doc = WordApp.Documents.Add()
With doc.Range
.InsertAfter("A New Architecture " & _
"for Building Distributed Applications")
.InsertParagraphAfter()
.InsertAfter("ADO.NET is the latest data access " & _
"technology from Microsoft, geared towards " & _
"distributed applications. Unlike its predecessor, " &
_
"ADO.NET uses disconnected recordsets.")
.InsertParagraphAfter()
.InsertAfter("The disconnected recordsets are called " & _
"DataSets and they may contain multiple tables. " & _
"If the tables are related, the DataSet knows " & _
"how to handle the relations and it provides " & _
"methods that allow you to move from any row of " & _
"a table to the related rows of the other tables.")
.InsertParagraphAfter()
.InsertParagraphAfter()
.InsertAfter("ADO.NET uses XML to move rows between " & _
"the database and the middle tier, as well as " & _
"between the middle tier and the client. " & _
"XML passes through firewalls and it's an ideal " & _
"candidate for moving binary information between " & _
"layers and/or different operating systems.")
End With
Dim selRange As Word.Range
selRange = doc.Paragraphs.Item(1).Range
selRange.Font.Size = 14
selRange.Font.Bold = True
selRange.ParagraphFormat.Alignment Word.WdParagraphAlignment.wdAlignParagraphLeft
selRange = doc.Paragraphs.Item(2).Range.Sentences.Item(2)
selRange.Italic = True
selRange = doc.Paragraphs.Item(3).Range.Words.Item(6)
selRange.Font.Bold = True
selRange = doc.Paragraphs.Item(5).Range.Words.Item(5)
selRange.Font.Bold = True

Dim fName As String
SaveFileDialog1.Filter = "Documents|*.doc"
SaveFileDialog1.ShowDialog()
fName = SaveFileDialog1.FileName
If fName <> "" Then
Try
doc.SaveAs(fName)
Catch exc As Exception
MsgBox("Failed to save document" & _
vbCrLf & exc.Message)
End Try
End If
MsgBox("The document contains " & doc.Paragraphs.Count & "
paragraphs " & vbCrLf & _
doc.Words.Count & " words and " & doc.Characters.Count & "
words")
doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim thisDoc As Word.Document
Dim thisRange As Word.Range

WordApp.Documents.Open("c:\sample.doc")
WordApp.Visible = False
thisDoc = WordApp.ActiveDocument
thisDoc.Content.Find.Execute(FindText:="VB7", _
ReplaceWith:="VB.NET", _
Replace:=Word.WdReplace.wdReplaceAll)
While thisDoc.Content.Find.Execute(FindText:=" ", _
Wrap:=Word.WdFindWrap.wdFindContinue)
thisDoc.Content.Find.Execute(FindText:=" ", _
ReplaceWith:=" ", _
Replace:=Word.WdReplace.wdReplaceAll, _
Wrap:=Word.WdFindWrap.wdFindContinue)
End While
WordApp.Documents.Item(1).Save()
MsgBox("Replaced all instances of 'VB7' with 'VB.NET' and saved the
document")
WordApp.Documents.Item(1).Close()
End Sub


El Wed, 2 Aug 2006 07:20:02 -0700, Guillermo escribió:

Que posibilidades hay de escribir o modificar en un archivo de microsoft
word, ya creado.

Gracias...
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida