Click here to Skip to main content
15,898,987 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Question regarding filesystemwatcher and existing files Pin
Ian Shlasko15-Dec-09 9:39
Ian Shlasko15-Dec-09 9:39 
QuestionBuilding an application that recieves emails Pin
Aptiva Dave15-Dec-09 4:25
Aptiva Dave15-Dec-09 4:25 
AnswerRe: Building an application that recieves emails Pin
dan!sh 15-Dec-09 5:08
professional dan!sh 15-Dec-09 5:08 
AnswerCheck this article out for reading email ... Pin
David Mujica15-Dec-09 5:24
David Mujica15-Dec-09 5:24 
AnswerRe: Building an application that recieves emails [modified] Pin
plecco22-Dec-09 8:49
plecco22-Dec-09 8:49 
Questionimage capture and object tracking Pin
voodoo_god15-Dec-09 3:46
voodoo_god15-Dec-09 3:46 
QuestionDatagridviewButtoncell accroding to the value of cell() Pin
Paramu197314-Dec-09 22:19
Paramu197314-Dec-09 22:19 
QuestionReplacing Bytes in an Office 2003 Word Document Pin
jonegerton14-Dec-09 3:25
jonegerton14-Dec-09 3:25 
I'm working on a utility to do a find replace operation on Word documents (2003 version not docx)

We've a version of this that uses automation, however it's too slow, and runs into problems with documents linked to excel, and containing macros (and anything else that causes messages to show).

I've got code sorted out that will do this finding and replacing the bytes in a byte array of the file (see below). This works perfectly as long as the length of the bytes I replace is the same as the length of the bytes I need to find. If I change the length of the file the document will no longer open in word.

When I make the change required manually (ie through MS Word), the byte length of the file doesn't change, so I'm assuming there must be a buffer somewhere in the file that is getting used.

Please give me feedback on how to update the file correctly.

Code:
(please excuse the rough nature of this code - I'm prototyping!)
(also DoReplace is based on code downloaded from T'Interweb. Can't remember where, but if its yours, thanks!)

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim find As String = "String To Find"
        Dim replace As String = "String To Replace"
        Dim path As String = "c:\InputPath.doc"
        Dim updatedpath As String = "c:\OutputPath.doc"
        Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(1252)

        Dim fi As New FileInfo(path)

        Dim fs As New FileStream(path, FileMode.Open)

        Dim bytes(CInt(fs.Length)) As Byte

        fs.Read(bytes, 0, CInt(fs.Length))
        fs.Close()

        Dim newBytes() As Byte = DoReplace(bytes, encoding.GetBytes(find), encoding.GetBytes(replace))

        fs = New FileStream(updatedpath, FileMode.Create)
        fs.Write(newBytes, 0, newBytes.Length)
        fs.Close()

    End Sub

    Public Function DoReplace(ByVal bytes As Byte(), ByVal findBytes As Byte(), ByVal replaceBytes() As Byte) As Byte()

        Dim newBytes As New Generic.List(Of Byte)
        Dim ndx As Integer = 0

        For x As Integer = 0 To bytes.Length - 1
            ' bytes is the original files bytes 
            If bytes(x) = findBytes(ndx) Then
                ' findBytes is a byte[] from the"find" string 
                If ndx = (findBytes.Length - 1) Then
                    For y As Integer = 0 To replaceBytes.Length - 1
                        'replaceBytes is a byte[] from the "replace" string 
                        newBytes.Add(replaceBytes(y))
                    Next
                    ndx = 0
                Else
                    ndx += 1
                End If
            Else
                If ndx > 0 Then
                    For y As Integer = 0 To ndx - 1
                        newBytes.Add(findBytes(y))
                    Next
                End If
                ndx = 0
                newBytes.Add(bytes(x))
            End If
        Next

        Return newBytes.ToArray

    End Function
End Class

AnswerRe: Replacing Bytes in an Office 2003 Word Document Pin
Richard MacCutchan14-Dec-09 5:05
mveRichard MacCutchan14-Dec-09 5:05 
GeneralRe: Replacing Bytes in an Office 2003 Word Document Pin
jonegerton14-Dec-09 6:32
jonegerton14-Dec-09 6:32 
GeneralRe: Replacing Bytes in an Office 2003 Word Document Pin
The Man from U.N.C.L.E.14-Dec-09 7:33
The Man from U.N.C.L.E.14-Dec-09 7:33 
AnswerRe: Replacing Bytes in an Office 2003 Word Document Pin
Eddy Vluggen14-Dec-09 5:50
professionalEddy Vluggen14-Dec-09 5:50 
GeneralRe: Replacing Bytes in an Office 2003 Word Document Pin
Luc Pattyn14-Dec-09 6:21
sitebuilderLuc Pattyn14-Dec-09 6:21 
GeneralRe: Replacing Bytes in an Office 2003 Word Document Pin
Eddy Vluggen14-Dec-09 7:16
professionalEddy Vluggen14-Dec-09 7:16 
GeneralRe: Replacing Bytes in an Office 2003 Word Document Pin
jonegerton14-Dec-09 6:35
jonegerton14-Dec-09 6:35 
GeneralRe: Replacing Bytes in an Office 2003 Word Document Pin
Member 368078514-Dec-09 8:54
Member 368078514-Dec-09 8:54 
QuestionFind out encoding of any file [modified] Pin
jonegerton14-Dec-09 3:08
jonegerton14-Dec-09 3:08 
AnswerRe: Find out encoding of any file Pin
Richard MacCutchan14-Dec-09 5:06
mveRichard MacCutchan14-Dec-09 5:06 
QuestionHow to auto expand combo box on focus. [modified] Pin
Mithun.Shitole13-Dec-09 23:05
Mithun.Shitole13-Dec-09 23:05 
GeneralIssue when create popup calendar.... Pin
Golden Jing13-Dec-09 14:49
Golden Jing13-Dec-09 14:49 
GeneralRe: Issue when create popup calendar.... Pin
EliottA14-Dec-09 3:34
EliottA14-Dec-09 3:34 
GeneralRe: Issue when create popup calendar.... Pin
Golden Jing14-Dec-09 20:07
Golden Jing14-Dec-09 20:07 
GeneralRe: Issue when create popup calendar.... Pin
EliottA15-Dec-09 3:25
EliottA15-Dec-09 3:25 
QuestionVB Form Commands in TextBoxes Pin
Dimz9313-Dec-09 4:10
Dimz9313-Dec-09 4:10 
AnswerRe: VB Form Commands in TextBoxes Pin
EliottA13-Dec-09 17:25
EliottA13-Dec-09 17:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.