Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
J

jonegerton

@jonegerton
About
Posts
4
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Replacing Bytes in an Office 2003 Word Document
    J jonegerton

    Thanks for the info. There are indeed Dragons! - Unfortunately this has to work on thousands of documents (corporate rebrand!) and there's no way I can guarantee that every doc will work within my timescales. We'll just have to get a temp to click on the message boxes that pop up (that are unstoppable!)

    Visual Basic tools announcement data-structures testing beta-testing

  • Replacing Bytes in an Office 2003 Word Document
    J jonegerton

    Yeah - got it to work after a fashion - by adjusting the empty bytes that are found after the main content of the doc. However handing headers footers and everything else is (as you say) horrible. I think if I had another couple of years I might take this further, however, I'm off Xmas shopping instead! :)

    Visual Basic tools announcement data-structures testing beta-testing

  • Replacing Bytes in an Office 2003 Word Document
    J jonegerton

    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
    
    Visual Basic tools announcement data-structures testing beta-testing

  • Find out encoding of any file [modified]
    J jonegerton

    I need to find the encoding of a file that I've opened through code so that I can convert strings to byte arrays using the same encoding. This includes word documents - the one I'm testing is Windows (1252) - according to EditPad, The normal examples that return the variations on UTF8/Unicode are not sufficient. Is there a way to acheive this? In the course of my code I'm opening the documents as byte arrays via a filestream, so some way of extracting the encoding from there would be most useful. Thanks in advance for your help For more information on what I'm actually trying to acheive, see my other post: http://www.codeproject.com/Messages/3303167/Replacing-Bytes-in-an-Office-2003-Word-Document.aspx[^]

    modified on Monday, December 14, 2009 9:29 AM

    Visual Basic com testing beta-testing help question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups