您好我最近在Excel中创建了一个vba宏来发送电子邮件电子表格。我想要发送电子邮件的电子表格是使用另一个vba程序创建的。程序将一个部件号作为输入,然后弹出excel打开并从sql server数据库中获取相应的数据。
我的问题是在检索信息期间弹出的excel不包含我创建的宏。如果我复制数据并打开一个新的空白excel工作簿,我的宏就在那里。如果我保存电子表格然后重新打开它,我的宏也会出现。
那么vba是否以某种方式创建一个新的excel工作簿导致我的宏不出现?有没有什么办法解决这一问题?最后,我想在vba程序完成创建它的那一刻通过电子邮件发送电子邮件。任何帮助将不胜感激。
编辑 - 这是关于我如何创建excel工作簿的代码片段。
' Create new Workbook
xlApp = New Excel.Application
xlApp.Visible = True
xlWorkBook = xlApp.Workbooks.Add
xlWorkSheet = xlWorkBook.Worksheets(1)
Array.Sort(snarry)
On Error Resume Next
' Selecct Column Widths
For i = 0 To inxms - 1
xlWorkSheet.Range(xlWorkSheet.Cells(1, i + 2), xlWorkSheet.Cells(2, i + 2)).EntireColumn.ColumnWidth = 10
Next i
' Display Measurement Types
offst = 7
xlWorkSheet.Cells(1 + offst, 1) = "S/N"
For i = 0 To inxms - 1
xlWorkSheet.Cells(1 + offst, i + 2) = measures(i)
Next
xlWorkSheet.Cells(1 + offst, inxms + 2) = "Ref#"
frst = True
For ims = 0 To inxms - 1
frstmeas(ims) = True
Next ims
strSheetRev = ""
strSheetRef = ""
strskiprev = ""
strskipref = ""
offst = offst + 1
For i = 0 To inx - 1
snskiprev = False
snskipref = False
For ims = 0 To inxms - 1
Call FillMeasurements(Conn, xlWorkSheet, pn, snarry(i), measures(ims), meastyp(ims), 1 + offst, ims + 2, inxms + 2, frst, frstmeas(ims), tester(ims), testdate(ims), numdg(ims), strRef, ckbNotes.Checked)
Next
' If snskiprev Then
' strskiprev = strskiprev & "," & Trim(Str(snarry(i)))
' End If
' If snskipref Then
' strskipref = strskipref & "," & Trim(Str(snarry(i)))
' End If
' If ((Not snskiprev) And (Not snskipref)) Then
xlWorkSheet.Cells(1 + offst, 1) = snarry(i)
offst = offst + 1
'End If
Next
' Add Tolerances
offst = offst + 1
xlWorkSheet.Cells(1 + offst, 1) = "Max"
xlWorkSheet.Cells(2 + offst, 1) = "Nom"
xlWorkSheet.Cells(3 + offst, 1) = "Min"
For ims = 0 To inxms - 1
Call FillTolerances(Conn, xlWorkSheet, pn, measures(ims), meastyp(ims), 1 + offst, ims + 2, numdg(ims))
Next ims
offst = offst + 3
' Add Tester
offst = offst + 1
xlWorkSheet.Cells(1 + offst, 1) = "Tested By"
xlWorkSheet.Cells(1 + offst + 1, 1) = "Test Date"
For i = 0 To inxms - 1
If (Not frstmeas(i)) Then
xlWorkSheet.Cells(1 + offst, i + 2) = tester(i)
xlWorkSheet.Cells(1 + offst + 1, i + 2) = Format(testdate(i), "MM/dd/yyyy")
End If
Next
Conn.Close()
' Display Header
midloc = CInt((inxms + 1) / 2.0)
If (midloc < 4) Then
midloc = 4
End If
xlWorkSheet.Cells(1, midloc) = "XXXXXXXXXXXX"
xlWorkSheet.Cells(2, midloc) = txtTitle.Text
xlWorkSheet.Cells(3, 1) = "XXXXXXXXXXXXX"
xlWorkSheet.Cells(4, 1) = "XXXXXXXXXXXXXXXXX"
xlWorkSheet.Cells(5, 1) = "XXXXXXXXXXXXXX"
xlWorkSheet.Cells(3, inxms) = "XXXXXXXXXXX"
xlWorkSheet.Cells(3, inxms + 1) = pn & "-" & strSheetRev
xlWorkSheet.Cells(3, inxms + 2) = vendor
'xlWorkSheet.Cells(4, inxms) = "Reference #"
'xlWorkSheet.Cells(4, inxms + 1) = strSheetRef
' Display messages for skipped sns
strSkip = ""
If (strskiprev <> "") Then
strSkip = strSkip & "S/Ns skipped for Rev mismatch" & Chr(10) & Mid$(strskiprev, 2) & Chr(10) & Chr(10)
End If
If (strskipref <> "") Then
strSkip = strSkip & "S/Ns skipped for Ref mismatch" & Chr(10) & Mid$(strskipref, 2)
End If
If (strSkip <> "") Then
MsgBox(strSkip)
End If
'
'xlWorkSheet.SaveAs("C:\Users\XXX\" + pn + "-" + Format(Now(), "yyyy-MM-dd-hh-mm-ss") + ".xlsx")
' xlWorkBook.Close()
'xlApp.Quit()
txtPN.Text = ""
' txtSNs.Text = ""
txtTitle.Text = ""
ckbNotes.Checked = False
GoTo Dne
BadSN: MsgBox(“Bad SN规范”) GoTo Dne DNE: 结束子
答案 0 :(得分:0)
“加载项”是安装在特定计算机上并位于特定计算机上的内容。它不会嵌入到工作簿中。而是有一个应用程序范围的设置,指示加载项是否“已安装”。在VBA中,属性Application.AddIns(index).Installed可用于启用或禁用加载项(通过索引标识,可以是加载项的名称)。不幸的是,“已安装”是用词不当;实际上它意味着启用/禁用,安装完全是另一回事。如果您的加载项是必不可少的,则需要在需要它的每台计算机上安装它。