|
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!
|
|
|
|
|
I know it will run somewhat slower, however ...
How about using Office Automation to open each doc, do the string substitution and save the resulting doc. We did some work recently that thankfully involved docs saved in WordML format. Resaving in the middle of your process as WordML may make the substitution easier.
|
|
|
|
|
The '95 versions didn't save an entire document, but they appended changes to the last part of the document. That's a bit faster than writing the entire document anew.
You can find the documentation through the link below. Be warned though, there's dragons there
http://download.microsoft.com/download/0/B/E/0BE8BDD7-E5E8-422A-ABFD-4342ED7AD886/Word97-2007BinaryFileFormat(doc)Specification.pdf
I are Troll
|
|
|
|
|
Eddy Vluggen wrote: there's dragons there
They are everywhere[^].
|
|
|
|
|
Now there's a gem that I still need to revisit.
..I'll plan it right after the course on time-management
I are Troll
|
|
|
|
|
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!)
|
|
|
|
|
jonegerton wrote: Unfortunately this has to work on thousands of documents (corporate rebrand!)
So I guess you have been tasked with replacing all occurences of "Tiger Woods" with "Tom Watson" or something along those lines?
I tried to make something like that work years ago, i.e. something that tried to re-create the Word "save" logic at a low level. My recollection is that somewhere in the file, there is a field holding the length of the data or (less likely) a checksum. I thought I was adjusting that properly, but never did manage to create "valid" Word documents. Apparently there was some other checksum somewhere that I did not know about. Eventually I ended up doing the job with automation. I managed to work through the message-box-related issues... I think there are ways to detect the error condition and kill Winword.exe. In the worst case, you could just assume an error occured after a certain length of time.
None of this is beautiful, but in the end it proved more workable than manually messing around with the file. And I did try mightily to make that work... I was just out of college, and had been immersed in a thesis that used Intel assembly, and the low-level approach was definitely the one I preferred.
One more thought: the "DOCX" format of Office 2007 is much more regular and well-documented than the old melange of DOC formats. I think a DOCX is basically a zipped-up collection of XML documents and embedded image files. Have you considered converting to DOCX as the first step of the process? It might make your life easier.
|
|
|
|
|
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
|
|
|
|
|
|
Hi,
I want to automatically expand Combo box on focus event.
I have set the Droppeddown = True in gotfocus event, but this has a side effect. When click event gets fired, it expands dropdown and closes immediately. How can I avoid it?
Mithun Shitole
"Free Your Mind"
http://www.technoyaari.com
modified on Monday, December 14, 2009 5:11 AM
|
|
|
|
|
Dear All,
I have problem with calendar.
I had created popup calendar with text box.
when i select month or year it always close the calendar like when i select day.
It is differenc datetime packer.
Do you have any idea or source code for my issue ?
Thanks you for your help ...
VB.Net
|
|
|
|
|
I totally don't understand your question, if one exists at all. Can you please rephrase your question so that a suitable answer can be thought of? Until then, liquid nitrogen.
|
|
|
|
|
Thanks,
That issue i solved it already.
But i have another thing that.
like DateTime Packer i want to show only Month & Year.
doese it has property for do that or not ?
How can resovle it ?
VB.Net
|
|
|
|
|
I would explore the relevant classes in the documentation at http://msdn.microsoft.com
|
|
|
|
|
Strange title... but i wan't to explain your what i mean.
I have a form, with one textbox. This textbox should be useable with commands such as examples:
/title TITLE OF FORM
so the form title is TITLE OF FORM (without /title infront) - I have tried with splits, serveral of string formats and much more but without any luck and i don't seem to find it on google with search tags: TextBox, Command, Strings etc.
I hope you can help me with this, if you don't have any idea what i am talking about, you can ask in this thread and i can try to explain.
I use Visual Studio 2008 VB Form
|
|
|
|
|
Should be relatively easy, just use a select case statement on the first word in a text box, and use a default case for unknown commands. I did something similar for a chatbot...
|
|
|
|
|
A strange title deserves a strange answer. I've divided this into three smaller tasks, each with code;
dim input as string = "/title HELLO"
This is where you get the command from the user. Next, you'd need to split this in the portion that represents the command (e.g. "/title"), and the portion that represent the arguments (e.g., "HELLO")
dim indexOfSpace as integer = input.IndexOf(" ")
dim command as string = input.SubString(1, indexOfSpace - " ".Length)
dim args as string = input.SubString(indexOfSpace)
Lastly, you'd need something to apply these new values.
Select Case command
Case "title": Text = args
Case "color": Color = Color.FromName(args)
End Select
Good luck
I are Troll
|
|
|
|
|
I'm trying to make a client to a text based game. I've asked the question before, but now I've got a little bit more to work with. What I need is some help with it. I'll show the code first.
Code for my 'tcpListener' which I've actually added to my overall project as a Console Application - Module1
Imports System.Net.Sockets
Imports System.Text
Module Module1
Sub Main()
Dim tcpListener As New TcpListener(6100)
Dim requestCount As Integer
Dim clientSocket As TcpClient
tcpListener.Start()
msg("Server Started")
clientSocket = tcpListener.AcceptTcpClient()
msg("Accept connection from client")
requestCount = 0
While (True)
Try
requestCount = requestCount + 1
Dim networkStream As NetworkStream = _
clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = _
System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = _
dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
msg("Data from client - " + dataFromClient)
Dim serverResponse As String = _
"Server response " + Convert.ToString(requestCount)
Dim sendBytes As [Byte]() = _
Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End While
clientSocket.Close()
tcpListener.Stop()
msg("exit")
Console.ReadLine()
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
End Module
And this is my code for the actual client
Imports System.Net.Sockets
Imports System.Text
Public Class lokMudClient
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Private Sub btnConnect_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnConnect.Click
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim buffSize As Integer
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes("Message from Client$")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
Dim returndata As String = _
System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " + returndata)
End Sub
Private Sub lokMudClient_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
msg("Client Started")
clientSocket.Connect("204.209.44.6", 6100)
outputBox.Text = "Client Socket Program - Server Connected ..."
End Sub
Sub msg(ByVal mesg As String)
outputBox.Text = outputBox.Text + Environment.NewLine + " >> " + mesg
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOptions.Click
dialogsForm.Show()
End Sub
End Class
Now, for some reason, I can't see any output in my 'outputBox' which I've set inside a panel labelled 'outputHolder.' So, I'm a little lost as to which direction I need to be going with this. I thought I had it down, but I guess I don't....
P.S. I'm not getting any errors, but I am getting a warning which reads:
"'Public Sub New(port As Integer)' is obsolete. 'This method has been depracated. Please use TcpListener(IPAddress localaddr, int port).'
And it can be found on line 5, column 13 of the tcpListener project. Could this be the reason I'm not finding any text in the rich text box I'm trying to use to hold the output of the server? Suggestions, fixes, please provide and thank you in advance.
|
|
|
|
|
Hello,
I am new to this forum, and a begginer programer.
I am looking for some advice on how to use a button control to open the openfiledialog which displays a picture on a form.
Here is the code i currently have, it works except for the fact that the openfile dialog just repeatedly comes up even after i choose the file to open. I only want the file to open when i hit button2.
Public Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim button2 As Boolean = False
If button2 = True Then
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
End If
End If
' Create image.
Dim Newimage As Image = Image.FromFile(user)
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(100, 100)
' Draw image to screen.
e.Graphics.DrawImage(Newimage, ulCorner)
End Sub
modified on Friday, December 11, 2009 3:59 AM
|
|
|
|
|
The MyBase.Paint event happens a lot, that's why your dialog gets openend constantly.
Setting aside the question of how wise it is to try and do this kind of operation in that particular event, you are checking to see if the boolean button2 is false, which would be the correct way to prevent this code from being executed all the time. However, because you dim the boolean inside the same event with an explicit value of False, it will never ever be true.
If this really does what you say it does, than the code you posted is not the code that you are running, or you just didn't post the entire code here. If the boolean somehow gets set to True between its creation and checking it's value, the Paint event may fire again before the boolean gets set to false (which, according to this code, only happens at the moment you create the boolean). Which is the main reason not to use this event for what you are doing here.
So the questions are:
- did you create a boolean with the exact same name outside this event, particularly at the class level (outside any sub or function) ?
- Is there any code anywhere in your application that should set that boolean to True?
Tips:
- Use the Try catch clase and run your app in visual studio, and step through it line by line (put a break point at the first line, and press F11 to advance one statement).
- Try to run your code inside a controlled event, like the button click event, first, before using it in volatile events like Paint.
My advice is free, and you may get what you paid for.
|
|
|
|
|
Hello Johan,
Thanks for the reply!
Maybe i worded my problem wrong...im pretty new at this so it is slightly hard for me to accurately describe what is wrong.
What happens is:
I go to the correct form, and automatically, without me clicking on button2, the openfiledialog pops up and allows me to choose an image which then gets displayed on the form. Then the dialog pops up again and wants a new file etc etc in a continuous loop, somehow just completely bypassing my button. I have since tried some new code, this however does the exact same thing. I will post my entire code for that form now, maybe that will help you understand it more.
Thanks again!
Imports System.IO
Imports System.Windows.Forms.MouseEventArgs
Imports System.Windows.Forms.Form
Imports System.Windows.Forms.PaintEventArgs
Public Class Edtor
Private gfx As Graphics
Public user As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Visible = True
Me.Visible = False
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
End Sub
Private Sub Edtor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
gfx.Dispose()
End Sub
Private Sub Edtor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gfx = Me.CreateGraphics()
End Sub
Public Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If (Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Me.SaveFileDialog1.FileName
End If
End Sub
Public Sub Edtor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If RadioButton1.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Red, rect)
End With
End If
If RadioButton2.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Blue, rect)
End With
End If
If RadioButton3.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Black, rect)
End With
End If
End Sub
Public Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Call Button2_Click(sender, e)
' Create image.
Dim Newimage As Image = Image.FromFile(user)
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(100, 100)
' Draw image to screen.
e.Graphics.DrawImage(Newimage, ulCorner)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonpic.Click
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
End If
End Sub
End Class
The bolded is where the problem is. I dont want the openfiledialog to appear UNTIL i click button2.
|
|
|
|
|
First please use the 'code block' or 'inline code' for posting code. Makes it easier to read the code.
As to your problem, you don't need the paint event. Using this event is why you have the endless loop.
Paint event is triggered every time the form repaints (logic isn't it ) and since you call a dialog in your paint event every time that dialog closes your form is repainted so your dialog is opened again and so on...
Suggestion:
Remove your paint event completely.
Draw your image in your button click event and go from there (might I suggest a picture box to display your image.)
|
|
|
|
|
Oh sorry, i will post in code block next time.
The reason i cant do that, is because im using this application as a sort of paint...and once i loaded the photo into a picture and started painting on the form, i couldnt paint over top of the picturebox...but if i load the picture directly onto the form, i am able to pain over it, and thats where my problem is.
If there is a way to paint on top of a picturebox, i would have done that, but i couldn't see a way to do that. I can load a picture onto the form and paint on it just fine if i explicitly tell it where the file loacation is, but using the openfiledialog into the mix seems to really mess it up and I just can't figure out how to fix it..
Thanks for your help!
|
|
|
|
|
Ah so your not only painting the image in that case your right you can't use the picture box.
you might want to check out Christian Graus his articles here[^]
I think it was him who had a couple on this sort of thing.
As to your immediate problem I would do the following:
In your button click (and only there) open your file dialog, and save the selected image into a global variable for that form.
In the paint event draw that image and do nothing more than that, so don't open the file dialog in there.
Pseudo code:
private img as image
private sub button_click ...
if openfiledialog.showdialog = ok then
img = image.fromfile(openfiledialog.getpath)
end if
end sub
private sub paint_event....
'draw the img object
end sub
|
|
|
|
|
I figured it out with the help of several different forums thanks alot guys! Heres the code i ended up with in case your curious.
Imports System.IO
Imports System.Windows.Forms.MouseEventArgs
Imports System.Windows.Forms.Form
Imports System.Windows.Forms.PaintEventArgs
Public Class Edtor
Private gfx As Graphics
Public user As String
Private pathset As Boolean
Dim button2 As Global.System.Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.Visible = True
Me.Visible = False
End Sub
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
End Sub
Private Sub Edtor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
gfx.Dispose()
End Sub
Private Sub Edtor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gfx = Me.CreateGraphics()
End Sub
Public Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
If (Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Me.SaveFileDialog1.FileName
End If
End Sub
Public Sub Edtor_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If RadioButton1.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Red, rect)
End With
End If
If RadioButton2.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Blue, rect)
End With
End If
If RadioButton3.Checked = True Then
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
Dim rect As Rectangle
With rect
.X = e.X - 1
.Y = e.Y - 1
.Height = 2
.Width = .Height / .Height
gfx.DrawRectangle(System.Drawing.Pens.Black, rect)
End With
End If
End Sub
Private Sub drawonform(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If pathset = True Then
If button2 = True Then
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
End If
End If
' Create image.
Dim Newimage As Image = Image.FromFile(user)
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(100, 100)
' Draw image to screen.
e.Graphics.DrawImage(Newimage, ulCorner)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonpic.Click
If (Form1.openDiag.ShowDialog() = Windows.Forms.DialogResult.OK) Then
user = Form1.openDiag.FileName
pathset = True
End If
End Sub
End Class
|
|
|
|
|