RE: docx file corrupted when saved and retrieved from SQL2005



Dear Jialiang,
That was the timeliest, most thorough and accurate response I have ever
received in any newsgroup. This problem has been bugging me for months (I
use this in many of my applications and have been saving files in doc format
to work around the bug) and in less than a few hours you have answered all my
questions, including the one why my original post was not answered.

You did the equivalent of ending an inning by jumping over the fence to
prevent a home run, and then got up to bat with the bases loaded and hit a
grand slam.

Thank you very much.

Haim
--
Haim Katz
BARD


""Jialiang Ge [MSFT]"" wrote:

Good morning Haim. Welcome to Microsoft Newsgroup Support Service! My name
is Jialiang Ge [MSFT]. It's my pleasure to work with your on this issue.

I have reproduced the symptom "The file is corrupted and cannot be open"
for docx with your code. After debugging the program and looking into the
binary of the resulting docx, I find that the symptom is caused by the
declaration of buffer: Dim myarray(myfile.ContentLength) As Byte

Let's first look at the resolution of the issue, then I will explain "WHY".
Last, I will share the skills to trouble-shoot this kind of issues, which
may benefit you in future.

--- RESOLUTION ---
Change the code line

Dim myarray(myfile.ContentLength) As Byte

in the function "uploadfile" to

Dim myarray(myfile.ContentLength - 1) As Byte

--- CAUSE ---
In the grammar of VB.NET, vb6 or vba, Dim abArray(n) defines an array sized
n+1. In other words, Dim myarray(myfile.ContentLength) declares an array
with (myfile.ContentLength + 1) elements and result in an extra byte at the
end of the file. To allocate an array with n elements, we need to use Dim
abArray(n-1)

--- SKILL ---
You may wonder how I found out the problem. I used your code to import &
export a docx file. The resulting docx cannot be opened, thus I used a
binary editor to compare the two docx. To my surprise, the resulting one
always has an extra byte (00) in the end. If I remove the byte, it opens
fine. That said, if we can determine where the extra byte is getting added
(import or export), we should be set. I started with the import procedure.
The source docx for test contained 1069bytes (it's deteced from my binary
editor). I saw the code myfile.ContentLength return 1069 as expected,
however, in.Parameters(4).Value = myarray, I detected that myarray
contained 1070 elements. Then I know where the problem is.

You may also wonder why the problem did not occur to the doc files. It's
because doc and docx have completely different file formats (see
http://msdn.microsoft.com/en-us/library/aa338205.aspx). Microsoft Word uses
different parser for the two files. The parser of doc does not regards the
extra 00 byte at the end of the file as a mistake. But the parser of docx
does.

Haim, please try the solution and let me know whether it works for you or
not. If you have any other questions or concerns, please DON'T hesitate to
tell me.

P.S.
I notice that you once posted this question in the
microsoft.public.office.developer.officedev.other newsgroup a few weeks
ago. Our managed newsgroup system did not capture that post and we
(Microsoft) did not jump in because
microsoft.public.office.developer.officedev.other is not a managed
newsgroup. For the list of queues that are managed by us, please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa974230.aspx.

Regards,
Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • RE: docx file corrupted when saved and retrieved from SQL2005
    ... Welcome to Microsoft Newsgroup Support Service! ... for docx with your code. ...
    (microsoft.public.office.developer.vba)
  • RE: Local Security Settings: Strange Behavior on Win2K
    ... but this newsgroup mainly focuses on the security related ... Microsoft Online Community Support ... You can send feedback directly to my manager at: ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.platformsdk.security)
  • Re: Visual Studion 2008 SP1 can not connect to sql 2008
    ... The sqlserver.connect newsgroup is better for your issue and our engineer will work on it with you there. ... We welcome your comments and suggestions about how we can improve the support we provide to you. ... Please feel free to let my manager know what you think of the level of service provided. ...
    (microsoft.public.dotnet.general)
  • Re: Windows Mobile sdk install???
    ... This newsgroup is dedicated for ASP.NET issue. ... Microsoft Online Support ... You can send feedback directly to my manager at: ... nature are best handled working with a dedicated Microsoft Support Engineer ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Problem with EnabelVisualStyles and displaying ToolBarButtons
    ... Thank you for using our MSDN Managed Newsgroup Support Service! ... You can send feedback directly to my manager at: ...
    (microsoft.public.dotnet.languages.csharp)

Loading