简体   繁体   English

VB .NET 自动化,Document.SaveAs 损坏 Word 文档

[英]VB .NET automation, Document.SaveAs corrupting Word Document

Just to start I try to summarize what I want to do:刚开始,我尝试总结一下我想做的事情:

My vb .net app done in vs2019 simply automates and fills a word template, it should at the end save it in a folder provided by a text box saved in the variable URL.我在 vs2019 中完成的 vb .net 应用程序只是自动化并填充了一个单词模板,最后它应该将它保存在由保存在变量 URL 中的文本框提供的文件夹中。

Everything is fine and dandy but if I try to open the word docx I get this:一切都很好,但如果我尝试打开 docx 这个词,我会得到这个:

在此处输入图片说明单词错误 . .

Also if I save using the saveAs from word itself,manually, the file isn't corrupted anymore.此外,如果我使用 word 本身的 saveAs 手动保存,则文件不再损坏。 And there is difference in size between the files that are pretty much the same:几乎相同的文件之间存在大小差异:

在此处输入图片说明word文档差异1 . .

在此处输入图片说明word文档差异2 . .

在此处输入图片说明word文档区别3 . .

SO, the file "Adresa OCPI2" is a corrupted file "Adresa OCPI" that was simply renamed.所以,文件“Adresa OCPI2”是一个损坏的文件“Adresa OCPI”,只是重命名了。 "Adresa OCPI" was saved manually. “Adresa OCPI”已手动保存。

WPS office can for some reason open the corrupted file, but I had to remove the app because it wasn't good for automation. WPS office 可以出于某种原因打开损坏的文件,但我不得不删除该应用程序,因为它不利于自动化。

Using Word 2007, and on another computer with 2019 it has the same issue :(. Technically if the references were bad I wouldn't be able to get to the save point :/ but its debatable.使用 Word 2007,在另一台装有 2019 的计算机上,它有同样的问题:(。从技术上讲,如果引用不好,我将无法到达保存点:/但这是有争议的。

Tried closing at the end with ("oDoc.Close() oWord.Quit() and Me. Close()") but no difference尝试在最后关闭 ("oDoc.Close() oWord.Quit() and Me.Close()") 但没有区别

Imports Microsoft.Office.Interop
Imports Word = Microsoft.Office.Interop.Word
Dim oWord As Word.Application
                Dim oDoc As Word.Document
                Dim URL As String 
URL = TextBox9.Text
oWord = CreateObject("Word.Application")
                oWord.Visible = True
oDoc = oWord.Documents.Open(URL & "\AdresaOCPITemplate.dotx")
oDoc.Activate()

...

oDoc.SaveAs(URL & "\Adresa OCPI.docx")

I simply don't want to manually save stuff, and sorry if it looks to simple :/ Edit 1, unzip docx, found xml diff that appeared also in details.我只是不想手动保存东西,如果它看起来很简单,很抱歉:/编辑 1,解压缩 docx,发现 xml diff 也出现在细节中。

在此处输入图片说明

在此处输入图片说明

Edit 2 out of curiosity I tried this combination of code at the end出于好奇编辑 2 我在最后尝试了这种代码组合

oDoc.Close()
oDoc = oWord.Documents.Open(URL & "\Adresa OCPI.docx")

and got:并得到:
"Word encountered an error processing the XML file Adresa OCPI" no code errors, this was received during debug “Word 在处理 XML 文件 Adresa OCPI 时遇到错误”没有代码错误,这是在调试期间收到的

Is it by any chance the fact that the dotx format is broken and I should use docx as template instead???? dotx 格式已损坏,我应该使用 docx 作为模板吗????

Edit 3 Head hurts, docx has no issue, but I wanted extra safety with dotx so the files don't get accidentally overridden or modified.编辑 3 头疼,docx 没有问题,但我希望 dotx 具有额外的安全性,这样文件就不会被意外覆盖或修改。 So technically I found a work around but I do wish to use dotx in the end, will leave this question open for our NA friends, maybe they wake up, will close it soon.所以从技术上讲,我找到了一个解决方法,但我确实希望最终使用 dotx,这个问题会留给我们的 NA 朋友开放,也许他们会醒来,很快就会关闭它。

Edit 4:编辑4:

How do I edit an existing MS Word template (.dotx) with VB.NET? 如何使用 VB.NET 编辑现有的 MS Word 模板 (.dotx)?

Mods please don't close my question yet... Mods请不要关闭我的问题...

The Open opens the template for editing, then you save it as docx while internally it's still dotx. Open 打开模板进行编辑,然后将其保存为 docx,而在内部它仍然是 dotx。 You should use Documents.Add instead of Documents.Open.您应该使用 Documents.Add 而不是 Documents.Open。 – GSerg from comments – 来自评论的 GSerg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM