我想要制作一个.dotm模板,从它构建文档库。然后,我想使用Visual窗体填充它。
首先,我尝试使用AutoExec和AutoOpen来实现它,但是它没有成功。所以我搜索了一下,找到了一个解决方案--我需要使用Document_New程序。所以现在一切看起来都是这样的:
我有一个斑点模板。在模板项目中,我为ThisDocument和名为InputForm的表单定义了一个宏。宏如下所示
Private Sub Document_New()
InputForm.Show
End Sub在InputForm代码中,我从TextBox元素获得一些文本,并希望将其分配给新创建的文档中的某些字段。
问题是,在我填写表单之前,文档是从模板构造的,所以所有应该在新文档中进行的更改都是在模板中完成的。
编辑:InputForm的代码
Private Sub OkButton_Click()
If IsRight Then
ThisDocument.txt_Num.Value = txt_Num1.Value
ThisDocument.txt_Date.Value = txt_Date1.Value
ThisDocument.lbl_Num_Date.Caption = " ТКП № " & txt_Num1.Value & " от " & _
txt_Date1.Value & "г."
ThisDocument.txt_Project.Value = txt_Project1.Value
ThisDocument.lbl_Project.Caption = txt_Project1.Value
ThisDocument.txt_Author.Value = txt_Author1.Value
ThisDocument.txt_Org.Value = txt_Org1.Value
ThisDocument.lbl_Organization.Caption = txt_Org1.Value
ThisDocument.txt_Phone.Value = txt_Phone1.Value
ThisDocument.txt_Mail.Value = txt_Mail1.Value
InputForm.Hide
Else
MsgBox ("Неверно")
End If
End Sub
Public Function IsRight() As Boolean
If IsDate(txt_Date1.Value) Then
IsRight = True
Else
IsRight = False
End If
End Function
Private Sub CancelButton_Click()
InputForm.Hide
End Sub发布于 2016-07-09 14:20:38
您应该使用ActiveDocument而不是ThisDocument。
你可以尝试这样的方法
ActiveDocument.txt_Num.Value = txt_Num1.Valuehttps://msdn.microsoft.com/en-us/library/office/ff194846.aspx
另外,在这里查看我的答案,寻找更简单的替代方案Find and replace multiple words in Word。
发布于 2016-07-08 19:25:24
假设在打开文档的新实例后,宏可以填写表单,我将尝试以下Documents.Add Template:="C:/put absolute file path here, NewTemplate:= False, DocumentType:= 0'.,在我看来,您的代码打开的是实际模板,而不是“新文档”。
然后,我会将这个宏附加到键盘快捷方式,或者从模板中的一个按钮上触发它,然后运行宏来填充文档。根据我的经验(这是有限的),使宏尽可能地划分为我提供了最好的结果。
https://stackoverflow.com/questions/38251251
复制相似问题