在VBA中,我们可以使用例如Wb.VBProject.VBComponents来获取工作簿Private Sub Command1_Click()
Dim NumCopies, I
'rem Set Cancel to True
dlgDialog.CancelError = False
dlgDialog.Flags = cdlPDUseDevModeCopies 'Enables multiple-copy printing
Dim MarginsLR As Single
Dim MarginsTB As Single
Dim PrintableWidth As Single
Dim PrintableHeight As Single
Dim ScaleFactor As Double
Dim ScaledWidth As Double
Dim ScaledHeight As Double
MarginsLR = Printer.ScaleX(0.5, vbInches, Printer.ScaleMode)
MarginsTB = Printer.ScaleY(0.5, vbInches, Printer.ScaleMode)
PrintableWidth = Printer.Width - 2 * MarginsLR
PrintableHeight = Printer.Height - 2 * MarginsTB
ScaleFactor = PrintableWidth / Printer.ScaleX(Form1.Width, vbHiMetric, Printer.ScaleMode)
If ScaleFactor * Printer.ScaleY(Form1.Height, vbHiMetric, Printer.ScaleMode) > PrintableHeight Then
ScaleFactor = PrintableHeight / Printer.ScaleY(Form1.Height, vbHiMetric, Printer.ScaleMode)
End If
ScaledWidth = ScaleFactor * Printer.ScaleX(Form1.Width, vbHiMetric, Printer.ScaleMode)
ScaledHeight = ScaleFactor * Printer.ScaleY(Form1.Height, vbHiMetric, Printer.ScaleMode)
MoveX = (Printer.Width - ScaledWidth) / 2
MoveY = (Printer.Height - ScaledHeight) / 2
On Error GoTo ErrorHandler
' Display the Print dialog box
dlgDialog.ShowPrinter
NumCopies = dlgDialog.Copies
For I = 1 To NumCopies
'The following was experimental
'Form1.ScaleHeight = ScaledHeight
'Form1.ScaleWidth = ScaledWidth
'Form1.ScaleX = MoveX
'Form1.ScaleY = MoveY
'For the .PrintForm function. I can't figure out how to do all the centering stuff sadly...
Label1.Visible = True
Command1.Visible = False
'This following code enables resizing and positioning but it doesn't make both Label1 and Command1 statements above work
Printer.PaintPicture CaptureClient(Me), (Printer.Width - Me.Width) / 2, _
(Printer.Height - Me.Height) / 2
'Form1.PrintForm
Printer.EndDoc
Next I
Label1.Visible = False
Command1.Visible = True
Exit Sub
ErrorHandler:
MsgBox "The following error has occurred:" & vbNewLine _
& "Err # " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Print Error"
End Sub
的所有VBA宏。
有人知道VSTO是否提供任何对象或功能来访问它?
编辑1:根据Jim的回答,我收到了以下错误:
答案 0 :(得分:0)
您需要在项目中添加对Microsoft.Vbe.Interop
的引用。然后你可以使用
Imports Microsoft.Vbe.Interop
。
Dim components As VBComponents = Application.ActiveWorkbook.VBProject.VBComponents
或
Dim components As VBComponents = Globals.ThisAddIn.Application.ActiveWorkbook.VBProject.VBComponents
然后
For Each comp As VBComponent In components
Next
修改强>
我在C#中创建了以下方法来获取宏列表并将它们添加到List<string>
private static void GetMacros()
{
int startLine = 0;
vbext_ProcKind ProcKind;
List<string> macros = new List<string>();
VBComponents components = Globals.ThisAddIn.Application.ActiveWorkbook.VBProject.VBComponents;
try
{
foreach (VBComponent comp in components)
{
startLine = comp.CodeModule.CountOfDeclarationLines + 1;
while (startLine <= comp.CodeModule.CountOfLines)
{
string macroName = comp.CodeModule.get_ProcOfLine(startLine, out ProcKind);
macros.Add(macroName);
startLine += comp.CodeModule.get_ProcCountLines(macroName, ProcKind);
}
}
}
catch (Exception)
{
throw;
}
}