简体   繁体   English

将Word文档用户表单复制到另一个

[英]Copy Word document Userform to another

I have this code below to copy VBA codes from one word document to another (I'm using C#). 我下面有这段代码可将VBA代码从一个Word文档复制到另一个(我正在使用C#)。 It works for modules however I can't seem to get it to work with userforms. 它适用于模块,但我似乎无法使用userforms。

VBComponent sourceVBC = GetSourceDocVB();
VBComponent targetVBC = document.VBProject.VBComponents.Add(sourceVBC.Type);
string codes = sourceVBC.CodeModule.get_Lines(1, sourceVBC.CodeModule.CountOfLines);
targetVBC.CodeModule.AddFromString(codes);
targetVBC.Name = sourceVBC.Name;

Yes, the userform is copied to the target document but its fields are not. 是的,用户表单已复制到目标文档,但其字段未复制。 Like if it contains labels and textboxes. 就像它包含标签和文本框一样。 Those fields are not copied. 这些字段不会被复制。 Am I missing something here? 我在这里错过了什么吗?

Yes, you are missing something. 是的,你错过了什么。 Forms are not defined in the code file only, but need a binary file too. 表单不仅在代码文件中定义,而且还需要一个二进制文件。 You don't tell anything about the way the source files are generated. 您没有告诉任何有关源文件生成方式的信息。 Normally, in VBA, you use the "Export" statement of the VBComponent object. 通常,在VBA中,您使用VBComponent对象的“Export”语句。 Of course one can do it manually by going to the VBA Editor in Word, right-clicks the project component and selects "Export". 当然,可以通过转到Word中的VBA编辑器手动执行此操作,右键单击项目组件并选择“导出”。 If you have a look into the export folder, you'll see that a form is saved as two files "Form1.frm" (containing the code) and "Form1.frx" (containing binary form data, as labels and other stuff). 如果您查看导出文件夹,您将看到一个表单被保存为两个文件“Form1.frm”(包含代码)和“Form1.frx”(包含二进制表单数据,作为标签和其他内容) 。 In the other project, you can use maually the File, Import function, which takes care of the binary definition if you import a form. 在另一个项目中,您可以单独使用File,Import函数,如果您导入表单,它将处理二进制定义。

In VBA, you may use something like this to export from a project: 在VBA中,您可以使用类似的东西从项目中导出:

For Each vbC In ActiveDocument.VBProject.VBComponents
  Select Case vbC.Type

    Case vbext_ct_StdModule
        strVbcExt = ".bas"
    Case vbext_ct_ClassModule
        strVbcExt = ".cls"
    Case vbext_ct_MSForm
        strVbcExt = ".frm"
    Case Else
  End Select

  strvbCName = vbC.Name
  strFilename = strPath & "\" & strvbCName & strVbcExt
  vbC.Export strFilename
(omitted the rest)

And to import you'll use 并导入你将使用

ActiveDocument.VBProject.VBComponents.Import strFilename

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

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