Word VBA,是否有一个原因可以解释为什么它在 Word 2003 Win XP 下工作但在 Word 2007 Win 7 下不起作用

发布于 2024-10-20 06:33:05 字数 1221 浏览 3 评论 0原文

我有一个可以在Win XP 上运行的Word 2003 中运行的宏,该宏位于normal.dot 模板中,相关部分如下。它从合并的 .rtf 运行,复制当前文档,打开一个新文档,粘贴复制的内容以及用户表单组合中的一些数据,将其保存在自定义命名文件中,然后将其关闭,让用户返回原来的位置开始了。然后继续通过 FTP 传输保存的文档。它按预期工作。

ActiveDocument.Content.Copy
Documents.Add
ActiveDocument.Content.Paste

'Add the data from the Userform
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
 .ClearFormatting
 .Text = "\<refDestination\>*\<End Synapse data\>"
 .Replacement.ClearFormatting
 .Forward = True
 .Format = False
 .MatchCase = False
 .MatchWholeWord = False
 .MatchWildcards = True
 .Replacement.Text = "<refDestination>" & ComboBox1 & "</refDestination>" & _
                     "<refType>" & ComboBox2 & "</refType>" & _
                     "<End Synapse data>"
 .Execute Replace:=wdReplaceAll, _
  Wrap:=wdFindContinue
End With


ActiveDocument.SaveAs savedName, wdFormatDocument
ActiveDocument.Close ' Now go on to FTP savedName

MsgBox "wait here"

我试图在 Win 7 和 Word 2007 上的不同站点上运行相同的宏,但失败了,我无法删除 savingName,因为它已经打开,以某种方式 activedocument.close 不会释放文件,并且进程挂起。如果我使用任务管理器强制 Word 关闭,我可以转到保存名称处的文件并正常打开它,它包含正确的数据,它似乎在关闭时停止。

I have a macro which works in Word 2003 running on Win XP, the macro is in normal.dot template the relevant part is below. It is run from a merged .rtf, copies the current document, opens a new document, pastes in the copied content along with some data from userform combos, saves it in a custom named file, and closes it out leaving the user back where they started. It then goes on to FTP the saved document. It works as expected.

ActiveDocument.Content.Copy
Documents.Add
ActiveDocument.Content.Paste

'Add the data from the Userform
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
 .ClearFormatting
 .Text = "\<refDestination\>*\<End Synapse data\>"
 .Replacement.ClearFormatting
 .Forward = True
 .Format = False
 .MatchCase = False
 .MatchWholeWord = False
 .MatchWildcards = True
 .Replacement.Text = "<refDestination>" & ComboBox1 & "</refDestination>" & _
                     "<refType>" & ComboBox2 & "</refType>" & _
                     "<End Synapse data>"
 .Execute Replace:=wdReplaceAll, _
  Wrap:=wdFindContinue
End With


ActiveDocument.SaveAs savedName, wdFormatDocument
ActiveDocument.Close ' Now go on to FTP savedName

MsgBox "wait here"

I am trying to run the same macro on a different site on Win 7 and Word 2007 and it fails, I cannot delete savedName as it is already open, in some way activedocument.close does not release the file, and the process hangs. If I use task manager to force word to close I can then go to the file at savedName and open it fine and it contains the right data, it just seems to stall on closing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2024-10-27 06:33:05

我无法解释为什么您的宏在 Windows XP/Office 2003 平台上工作以及为什么它在新平台上遇到错误。但我发现您正在将 ActiveDocument 对象用于两个不同的文档(源文档和目标文档)。在复制过程中,Word 会创建临时文件(请参阅 MSDN 中的本文,请参阅“文本粘贴在文件之间”)。
我不确定是否有联系,但我宁愿使用两个不同的对象,例如

Dim docSource as Word.Document
Dim docDest as Word.Document
Set docSource = ActiveDocument
docSource.Content.Copy
Set docDest = Documents.Add
docDest.Content.Paste
docSource.Close SaveChanges:=wdDoNotSaveChanges
<continue with docDest>

I cannot explain why your macro worked on the Windows XP/Office 2003 platform and why it runs into an error on the new platform. But I see that you're using the ActiveDocument object for two different documents, the source and the destination. During copy, Word creates temporary files (see this article in MSDN, see "Text Pasted Between Files").
I'm unsure if there's a connection, but I'd rather use two different objects, e.g.

Dim docSource as Word.Document
Dim docDest as Word.Document
Set docSource = ActiveDocument
docSource.Content.Copy
Set docDest = Documents.Add
docDest.Content.Paste
docSource.Close SaveChanges:=wdDoNotSaveChanges
<continue with docDest>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文