如何读取自动生成的复选框,如果复选框的值为 "真",则将复选框的名称保存到自动生成的变量中。
宏的目标:对于连环信的单次保存,我希望用户能够定义不同文件的名称。因此,我创建了一个UserForm,上面列出了所有可能的名称(Excel中的所有列)。例如,如果一个人选择了两列(名和姓),他或她将使用这列的内容进行保存。
例如:名和姓--> Liam_Smith.pdf--> Emma_Johnson.pdf。
重要代码部分
'################################################ --> Hauptteil / Userform Name
With ActiveDocument.MailMerge
Dim myLabel As Object
Dim myCheckBox As Object
Dim y As Integer
Dim ColumnCount As Integer
Dim CaptionValue As String
Load UserForm3
'ColumnCount = ActiveDocument.MailMerge.DataSource.RecordCount
ColumnCount = ActiveDocument.MailMerge.DataSource.FieldNames.Count
'MsgBox (ColumnCount)
For y = 1 To ColumnCount
CaptionValue = (ActiveDocument.MailMerge.DataSource.DataFields(y).Name)
Set myCheckBox = UserForm3.Controls.Add("Forms.CheckBox.1", "Test" & 1, True)
With myCheckBox
.Name = "myCheckBox" & y
.Left = 24
.Top = (17.5 + (y * 20))
End With
Set myLabel = UserForm3.Controls.Add("Forms.Label.1", "Test" & 1, True)
With myLabel
.Caption = (CaptionValue)
.Left = 54
.Top = (20 + (y * 20))
.Width = 50
.Height = 12
End With
Next y
Load UserForm3
UserForm3.Show
'################################################ --> CheckBox auswertung
'For y = 1 To ColumnCount
'If UserForm3.CheckBox.Value = True Then
'MsgBox "True"
'Else
'MsgBox "False"
'End If
整个代码。
Sub SerienbriefOneDoc()
'
' SerienbriefOneDoc Makro
'
'
Dim Dateiname As String
Dim LetzterRec As Long
Application.ScreenUpdating = True
Application.Visible = False
'################################################ --> Speicherort
'Variable declaration
Dim sFolderName As String
Dim sDesktopPath As String, sFolderPath As String
'Find Desktop path location
sDesktopPath = Environ("USERPROFILE") & "\Desktop\"
'Define folder name to create on the desktop
sFolderName = "Serienbrief"
'Folder Path
sFolderPath = sDesktopPath & sFolderName
'Create FSO Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
'Check Specified Folder exists or not
If oFSO.FolderExists(sFolderPath) Then
'If folder is available on the desktop
MsgBox "Der angegebene Ordner existiert bereits auf dem Desktop!", vbInformation, "VBAF1"
GoTo PDFsave
Else
'Create Folder
MkDir sFolderPath
'Diplay messafe on the screen
MsgBox "Ordner erstellt : " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "VBAF1"
End If
'################################################ --> Speicherort UserForm
'################################################ --> Makro einstellungen
PDFsave:
Dim isUpdating As Boolean
isUpdating = Application.ScreenUpdating
'we need ScreenUpdating toggled on to do this:
If Not isUpdating Then Application.ScreenUpdating = True
'if msg is empty, status goes to "Ready"
Application.StatusBar = msg
'make sure the update gets displayed (we might be in a tight loop)
DoEvents
'if ScreenUpdating was off, toggle it back off:
Application.ScreenUpdating = isUpdating
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
LetzterRec = Word.ActiveDocument.MailMerge.DataSource.ActiveRecord
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
'################################################ --> Hauptteil / Userform Name
With ActiveDocument.MailMerge
Dim myLabel As Object
Dim myCheckBox As Object
Dim y As Integer
Dim ColumnCount As Integer
Dim CaptionValue As String
Load UserForm3
'ColumnCount = ActiveDocument.MailMerge.DataSource.RecordCount
ColumnCount = ActiveDocument.MailMerge.DataSource.FieldNames.Count
'MsgBox (ColumnCount)
For y = 1 To ColumnCount
CaptionValue = (ActiveDocument.MailMerge.DataSource.DataFields(y).Name)
Set myCheckBox = UserForm3.Controls.Add("Forms.CheckBox.1", "Test" & 1, True)
With myCheckBox
.Name = "myCheckBox" & y
.Left = 24
.Top = (17.5 + (y * 20))
End With
Set myLabel = UserForm3.Controls.Add("Forms.Label.1", "Test" & 1, True)
With myLabel
.Caption = (CaptionValue)
.Left = 54
.Top = (20 + (y * 20))
.Width = 50
.Height = 12
End With
Next y
Load UserForm3
UserForm3.Show
'################################################ --> CheckBox auswertung
'For y = 1 To ColumnCount
'If UserForm3.CheckBox.Value = True Then
'MsgBox "True"
'Else
'MsgBox "False"
'End If
'################################################ --> Progressbar
'MsgBox (.DataSource.RecordCount)
.DataSource.ActiveRecord = wdFirstRecord
Dim RecordCount As Integer
Dim i As Integer, percent As Integer, ActiveDoc As Integer, ActivePercent As Integer
Dim widthUpdate As Double, j As Double
UserForm2.Label1.BackColor = &HFF00&
percent = 100
UserForm2.Label1.Width = 0
RecordCount = .DataSource.RecordCount
ActiveDoc = .DataSource.ActiveRecord
i = 1
Do
i = i + 1
j = i * percent / RecordCount
widthUpdate = j * 2
ActivePercent = j
UserForm2.Label1.Width = widthUpdate
UserForm2.Label2.Caption = ActivePercent & "% Complete"
If .DataSource.ActiveRecord > 0 Then
'Prueft ob es mehrere Seiten fuer den Serienbrief gibt
If RecordCount <> "0" Then
'zaehlt die Anzahl Datensaetz in der Spalte "Name"
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
If Dir(sFolderPath, vbDirectory) <> "" Then
'prueft ob es das Verzeichnis gibt.
Else
MsgBox "Verzeichnis existiert nicht"
'Fehlermeldung falls das Verzeichniss nicht existiert
End If
With .DataSource
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
dname = sFolderPath & "\" & Name1 & "_" & Name2 & ".pdf"
'erstellt eine Variable mit dem Pfad und dem Namen
End With
.Execute Pause:=False
ActiveDocument.SaveAs2 FileName:=dname, FileFormat:=wdFormatPDF
'benennt die Datei und aendert das Dateiformat auf PDF
ActiveDocument.Close False
'schliesst das Fenster
End If
End If
If .DataSource.ActiveRecord < LetzterRec Then
'prueft ob es noch eine Seite gibt im Serienbrief
.DataSource.ActiveRecord = wdNextRecord
'nimmt die naechste Seite des Serienbriefes
Else
Exit Do
'wenn es keine Seite im Serienbrief mehr gibt wird die Schleife beendet
End If
UserForm2.Show (0)
Load UserForm2
DoEvents
UserForm2.Repaint
Loop
Unload UserForm2
End With
Application.Visible = True
Application.StatusBar = False
Application.DisplayStatusBar = sBar
Application.ScreenUpdating = True
End Sub
我的用户表格
要保存的名称是 CheckBox
到一个变量,你可以做一些类似的事情。
Dim myCheckboxName as string
Dim Ctrl as Control
For each Ctrl in <UserForm>.MSForms.Controls
If TypeName(Ctrl) = "CheckBox" then
If Ctrl.Value = True Then
myCheckboxName = Ctrl.Name
End If
End If
Next Ctrl
记住,要用你的表单的引用来代替,这将会循环执行。都 控件,所以如果有超过1个复选框被选中,这将最终存储最后一个被选中的复选框,并在循环中被访问--但这让你知道如何找到选中的复选框并将其名称分配给一个变量。
我建议使用标题上的 CheckBox
而不是单独 Label
以使您的代码更容易引用和定位。比如说
Set myCheckBox = UserForm3.Controls.Add("Forms.CheckBox.1", "Test" & 1, True)
With myCheckBox
.Name = "myCheckBox" & y
.Caption = CaptionValue '<~~
.Left = 24
.Top = (17.5 + (y * 20))
End With
然后你就可以找到哪个 CheckBox
勾选,并直接引用它的标题,而不是试图找出一个很好的方法来命名应用你的标签来定位它们,一旦你找到了被勾选的腐蚀复选框。
比如说
With UserForm3.CheckBox
For y = 1 To ColumnCount
If .Value = True Then
MsgBox .Caption & " is checked."
Else
MsgBox .Caption & " is not checked."
End If
Next y
End with
我不知道你打算在你的代码中用这个标题做什么 所以我在这里的例子中使用了你的测试循环。
如果你想保持原来的样子,你可以应用同样的原理,使用一个 CheckBox
和一个单独的 Label
的标题,但你需要想出一个好办法,以便能够参考正确的。Label.Caption
当你发现选中的 CheckBox
比如用相同的数字命名它们,并检查它们的数量。.Name
属性匹配相同的数字,使用 Mid()
函数或类似的东西。