简体   繁体   English

Excel VBA复制粘贴形状并另存为1004错误

[英]excel vba copy-paste a shape and saveas 1004 error

I'm experiencing a 1004 runtime error when saving a workbook (wbk_New) on which I copy-pasted a group of shapes from another workbook (wbk_Old). 保存工作簿(wbk_New)时,我遇到了1004运行时错误,在该工作簿中我从另一个工作簿(wbk_Old)复制粘贴了一组形状。 I should tell that a macro from wbk_Old is assigned to this group. 我应该告诉您,来自wbk_Old的宏已分配给该组。

wbk_Old.Worksheets("DashBoard").Activate
ChartTop = ActiveSheet.Shapes("Group_VesselGraphics").Top
ChartLeft = ActiveSheet.Shapes("Group_VesselGraphics").Left
ActiveSheet.Shapes("Group_VesselGraphics").OnAction = ""
ActiveSheet.Shapes("Group_VesselGraphics").Copy

wbk_New.Worksheets("DashBoard").Activate
ActiveSheet.Shapes("Group_VesselGraphics").Delete
ActiveSheet.Paste
ActiveSheet.Shapes("Group_VesselGraphics").Top = ChartTop
ActiveSheet.Shapes("Group_VesselGraphics").Left = ChartLeft
ActiveSheet.Shapes("Group_VesselGraphics").OnAction = "'" & ActiveWorkbook.Name & "'!UpdateShipGraph"

wbk_Old.Close
wbk_New.SaveAs As path_Old

I can't figure out what is causing this error...Does anyone have already faced this issue ? 我不知道是什么原因导致此错误...是否有人已经遇到此问题?

Thanks a ton for your help ! 多谢您的帮助!

(I forgot to mention that a chart also belong to this group of shapes!) (我忘了说图表也属于这组形状!)

Ok, I don't understand why but it seems that some links were still existing between "wbk_New" and "wbk_Old" although I broke all the links and updated the chart to refer to intrinsic data. 好的,我不明白为什么,但是尽管我断开了所有链接并更新了图表以引用内部数据,但似乎在“ wbk_New”和“ wbk_Old”之间仍然存在某些链接。 Hence, closing of "wbk_Old" couldn't be properly performed and wbk_New became corrupted, so that it was impossible to save it. 因此,无法正确执行“ wbk_Old”的关闭并且wbk_New损坏,因此无法保存它。

The only way I found to work around this issue is to save (on itself) wbk_New before closing wbk_Old and then to call an external process that replace wbk_Old by wbk_New, using a delay of 4s, and to close wbk_New and Old before the delay completed. 我发现要解决此问题的唯一方法是先保存wbk_New,然后关闭wbk_Old,然后调用外部进程,将其用wbk_New替换wbk_Old,使用4s的延迟,并在延迟完成之前关闭wbk_New和Old 。 I have to admit that this is an ugly solution, if someone knows a better way it would be nice to share it! 我必须承认,这是一个丑陋的解决方案,如果有人知道更好的方法,那么最好将其共享!

Here below is the code of my solution, where BatchCmd creates a batch file of a command and shell it: 以下是我的解决方案的代码,其中BatchCmd创建命令的批处理文件并将其外壳:

{code above}
wbk_New.Save
cmd = "ping -n 4 127.0.0.1 >nul" + vbCrLf
cmd = cmd + "move /Y " & path_New & " " & path_Old
Call BatchCmd(cmd, status:=vbHide)
wbk_Old.Close
wbk_New.Close

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

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