excelApp.Visible=false作用

本文介绍如何使用Visual Basic for Applications (VBA)创建一个宏,用于打开指定路径的Excel文件,并设置其为不可见状态。适用于需要自动化处理Excel文件场景。
部署运行你感兴趣的模型镜像

Set excelApp=CreateObject("excel.application")
excelApp.Workbooks.Open("D:\WORK\Testing Work - Eva\SIT\Coming Renewal Testing\Siemense  ELC - CNHBBAALL-1156\2012 Renewal\FlexPlan.xls")
excelApp.Visible=false

可以使打开的excel显示或者不显示

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

Option Explicit ' 声明变量 Dim partDocument As PartDocument Dim part As Part Dim customProperties As CustomProperties Dim excelApp, excelWorkbook, excelSheet Dim i As Integer Dim prop As CustomProperty Dim savePath As String ' 错误处理 On Error Resume Next ' 获取当前活动的CATIA零件文档 Set partDocument = CATIA.ActiveDocument If Err.Number <> 0 Then MsgBox "错误: 无法获取活动文档。请确保在CATIA中打开了零件文档。错误: " & Err.Description, vbCritical GoTo CleanUp End If ' 验证是否为零件文档 If Not TypeName(partDocument) = "PartDocument" Then MsgBox "错误: 当前活动文档不是零件文档。", vbCritical GoTo CleanUp End If ' 获取零件对象 Set part = partDocument.Part If Err.Number <> 0 Then MsgBox "错误: 无法获取零件对象。错误: " & Err.Description, vbCritical GoTo CleanUp End If ' 获取自定义属性集合 Set customProperties = part.CustomProperties If Err.Number <> 0 Then MsgBox "错误: 无法获取自定义属性。错误: " & Err.Description, vbCritical GoTo CleanUp End If ' 创建Excel应用程序实例 Set excelApp = CreateObject("Excel.Application") If Err.Number <> 0 Then MsgBox "错误: 无法创建Excel实例。请确保已安装Excel。错误: " & Err.Description, vbCritical GoTo CleanUp End If ' 配置Excel应用程序 excelApp.Visible = True excelApp.DisplayAlerts = False ' 创建新工作簿 Set excelWorkbook = excelApp.Workbooks.Add If Err.Number <> 0 Then MsgBox "错误: 无法创建Excel工作簿。错误: " & Err.Description, vbCritical GoTo CleanUp End If ' 获取第一个工作表 Set excelSheet = excelWorkbook.Sheets(1) If Err.Number <> 0 Then MsgBox "错误: 无法获取Excel工作表。错误: " & Err.Description, vbCritical If Not excelWorkbook Is Nothing Then excelWorkbook.Close False GoTo CleanUp End If ' 设置表头 excelSheet.Cells(1, 1).Value = "属性名称" excelSheet.Cells(1, 2).Value = "属性值" excelSheet.Cells(1, 3).Value = "属性类型" excelSheet.Cells(1, 4).Value = "创建日期" excelSheet.Cells(1, 5).Value = "是否只读" ' 表头样式设置 With excelSheet.Range("A1:E1") .Font.Bold = True .Interior.ColorIndex = 15 ' 灰色背景 .Borders.LineStyle = 1 ' 边框 End With ' 初始化行计数器 i = 2 ' 遍历所有自定义属性并写入Excel For Each prop In customProperties ' 写入属性名称 excelSheet.Cells(i, 1).Value = prop.Name ' 写入属性值(处理不同数据类型) On Error Resume Next Select Case prop.Type Case 1 ' CATBSTR (字符串) excelSheet.Cells(i, 2).Value = prop.Value Case 2 ' CATI1 (字节) excelSheet.Cells(i, 2).Value = prop.Value Case 3 ' CATI2 (短整型) excelSheet.Cells(i, 2).Value = prop.Value Case 4 ' CATI4 (长整型) excelSheet.Cells(i, 2).Value = prop.Value Case 5 ' CATR4 (单精度浮点) excelSheet.Cells(i, 2).Value = prop.Value Case 6 ' CATR8 (双精度浮点) excelSheet.Cells(i, 2).Value = prop.Value Case 7 ' CATBOOL (布尔) excelSheet.Cells(i, 2).Value = IIf(prop.Value, "True", "False") Case 8 ' CATDATE (日期) excelSheet.Cells(i, 2).Value = prop.Value excelSheet.Cells(i, 2).NumberFormat = "yyyy-mm-dd hh:mm:ss" Case 9 ' CATVARIANT (变体) excelSheet.Cells(i, 2).Value = CStr(prop.Value) Case Else excelSheet.Cells(i, 2).Value = "[未知类型]" End Select On Error GoTo 0 ' 写入属性类型描述 Select Case prop.Type Case 1: excelSheet.Cells(i, 3).Value = "字符串" Case 2: excelSheet.Cells(i, 3).Value = "字节" Case 3: excelSheet.Cells(i, 3).Value = "短整型" Case 4: excelSheet.Cells(i, 3).Value = "长整型" Case 5: excelSheet.Cells(i, 3).Value = "单精度浮点" Case 6: excelSheet.Cells(i, 3).Value = "双精度浮点" Case 7: excelSheet.Cells(i, 3).Value = "布尔" Case 8: excelSheet.Cells(i, 3).Value = "日期" Case 9: excelSheet.Cells(i, 3).Value = "变体" Case Else: excelSheet.Cells(i, 3).Value = "未知类型" & prop.Type End Select ' 写入创建日期(如果可用) On Error Resume Next excelSheet.Cells(i, 4).Value = prop.CreationTime If Err.Number <> 0 Then excelSheet.Cells(i, 4).Value = "N/A" End If On Error GoTo 0 ' 写入是否只读 excelSheet.Cells(i, 5).Value = IIf(prop.ReadOnly, "是", "否") ' 行计数器递增 i = i + 1 Next ' 自动调整列宽 excelSheet.Columns("A:E").AutoFit ' 设置表格样式 With excelSheet.Range("A1:E" & (i - 1)) .Borders.LineStyle = 1 .HorizontalAlignment = 1 ' 居中对齐 End With ' 提示用户保存位置 savePath = partDocument.Name & "_属性导出.xlsx" savePath = Replace(savePath, ".CATPart", "") savePath = InputBox("请输入保存路径:", "保存Excel文件", savePath) ' 保存Excel文件 If savePath <> "" Then On Error Resume Next excelWorkbook.SaveAs savePath If Err.Number <> 0 Then MsgBox "警告: 保存文件失败。请检查路径和权限。错误: " & Err.Description, vbExclamation Else MsgBox "导出完成! 文件已保存至: " & savePath, vbInformation End If On Error GoTo 0 End If CleanUp: ' 释放资源 If Not excelSheet Is Nothing Then Set excelSheet = Nothing If Not excelWorkbook Is Nothing Then If Err.Number <> 0 Then excelWorkbook.Close False Set excelWorkbook = Nothing End If If Not excelApp Is Nothing Then excelApp.DisplayAlerts = True ' 如果出错则不关闭Excel,方便查看问题 If Err.Number = 0 Then excelApp.Quit Set excelApp = Nothing End If ' 重置错误处理 On Error GoTo 0
07-31
Sub ConvertToPDF() Dim wordApp As Object Dim wordDoc As Object Dim originalPath As String Dim fileName As String Dim pdfPath As String Dim fileExt As String Dim fso As Object Dim folder As Object Dim file As Object With Application.FileDialog(msoFileDialogFolderPicker) .AllowMultiSelect = False If .Show = -1 Then Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(.SelectedItems(1)) For Each file In folder.Files originalPath = file.Path Set wordApp = CreateObject("Word.Application") wordApp.Visible = False fileExt = Mid(originalPath, InStrRev(originalPath, ".") + 1) fileName = Mid(originalPath, InStrRev(originalPath, "\") + 1) fileName = Left(fileName, InStrRev(fileName, ".") - 1) pdfPath = Left(originalPath, InStrRev(originalPath, "\")) & fileName & ".pdf" If LCase(fileExt) Like "doc*" Or _ LCase(fileExt) = "voc" Or _ LCase(fileExt) = "lg" Or _ LCase(fileExt) = "tcf" Then Set wordDoc = wordApp.Documents.Open(originalPath) wordDoc.ExportAsFixedFormat _ OutputFileName:=pdfPath, _ ExportFormat:=17, _ OpenAfterExport:=False wordDoc.Close SaveChanges:=False wordApp.Quit ElseIf LCase(fileExt) Like "xlsx" Or _ LCase(fileExt) = "xls" Or _ LCase(fileExt) = "xlsm" Then Call ExportWorkbookToPDF(originalPath, pdfPath) Else Debug.Print "Error File: ." & fileExt End If Next file MsgBox "Complete! ", vbInformation End If End With Cleanup: On Error Resume Next If Not wordDoc Is Nothing Then wordDoc.Close False If Not wordApp Is Nothing Then wordApp.Quit Set wordDoc = Nothing Set wordApp = Nothing End Sub Sub ExportWorkbookToPDF(execlpath As String, pdfPath As String) Dim wb As Workbook Set wb = Workbooks.Open(execlpath) wb.ExportAsFixedFormat _ Type:=xlTypePDF, _ fileName:=pdfPath, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False wb.Close End Sub 上面这段代码有什么问题吗
最新发布
11-21
%% 步骤1:准备数据(示例数据,可替换为实际数据) % 生成测试数据:x为0到10的100个点,y1为正弦波,y2为余弦波 x = linspace(0, 10, 100); % 创建0到10的等间距数组 y1 = sin(x); % 正弦函数值 y2 = cos(x); % 余弦函数值 %% 步骤2:创建图形并设置属性 figure; % 创建新图形窗口 hold on; % 保持当前图形,允许多条曲线叠加 % 绘制两条曲线并设置样式 plot(x, y1, 'b-', 'LineWidth', 2); % 蓝色实线,线宽2 plot(x, y2, 'r--', 'LineWidth', 2); % 红色虚线,线宽2 % 添加图形标签和标题 xlabel('X轴数值', 'FontSize', 12); % X轴标签 ylabel('Y轴数值', 'FontSize', 12); % Y轴标签 title('正弦与余弦函数对比', 'FontSize', 14); % 图形标题 legend('sin(x)', 'cos(x)', 'Location', 'best'); % 添加图例 grid on; % 显示网格线 %% 步骤3:保存图形到临时图片文件 % 定义临时文件名(使用时间戳避免重复) tempImgName = ['temp_plot_' datestr(now, 'yyyymmdd_HHMMSS') '.png']; saveas(gcf, tempImgName); % 将当前图形保存为PNG文件 %% 步骤4:将图片插入Excel文件 excelFileName = '绘图结果.xlsx'; % 定义Excel文件名 % 创建Excel COM对象(需要安装Microsoft Excel) try excelApp = actxserver('Excel.Application'); % 启动Excel后台进程 excelApp.Visible = false; % 隐藏Excel界面 % 检查文件是否存在,不存在则创建工作簿 if ~exist(excelFileName, 'file') workbook = excelApp.Workbooks.Add(); else workbook = excelApp.Workbooks.Open([pwd '\' excelFileName]); end % 添加新工作表(命名为当前日期) sheetName = datestr(now, 'yyyy-mm-dd'); sheets = workbook.Sheets; newSheet = sheets.Add([], sheets.Item(sheets.Count)); newSheet.Name = sheetName; % 插入图片到Excel targetRange = 'B2'; % 图片插入起始位置 excelSheet = workbook.ActiveSheet; excelPictures = excelSheet.Shapes; excelPictures.AddPicture(... [pwd '\' tempImgName], ... % 图片完整路径 false, true, ... % 链接文件, 随文档保存 excelSheet.Range(targetRange).Left, ... % 水平位置 excelSheet.Range(targetRange).Top, ... % 垂直位置 400, 300); % 图片宽度和高度(单位:磅) % 在图片下方添加描述文本 excelSheet.Range('B12').Value = '图形说明:'; excelSheet.Range('B13').Value = '1. 蓝色实线: sin(x)函数曲线'; excelSheet.Range('B14').Value = '2. 红色虚线: cos(x)函数曲线'; % 保存并关闭Excel workbook.SaveAs([pwd '\' excelFileName]); workbook.Close; excelApp.Quit; % 删除临时图片文件 delete(tempImgName); disp(['成功保存到Excel文件: ' excelFileName]); catch ME disp('操作失败,错误信息:'); disp(ME.message); % 确保清理COM对象 if exist('excelApp', 'var') excelApp.Quit; delete(excelApp); end end
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值