|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
相同的代码,在WPS与EXCEL上执行的结果不同,说明如下:
代码分2个部分,主程序部分
Sub NewCadDoc()
' Application.ScreenUpdating = False '关闭屏幕自动更新 无论关闭与否,不影响EXCEL的状态栏赋值语句的执行,且第二打开的表格文件都不显示
'无论是否关闭,WPS的状态栏赋值预计都不执行(只有采用Workbooks.Open打开时 显示第一次赋值,后面有不更新了,下面有说明)
Dim wb As Workbook
Dim sht As Worksheet
Dim sheetname As String
Dim sheetexist As Boolean
'
Set wb = openWb(stablefilename, path_father) ’ CreateObject 方式打开第二个表格文件
' Set wb = openWb(stablefilename, path_father,True) ’Workbooks.Open 方式打开第二个表格文件
Set cadDoc = open_cad_file(tfilename)
Application.StatusBar = "正在给" & tfilename & "进行文字替换"
For k = start_column To end_column
Dim replace_key As String
replace_key = Sheet5.Cells(4, k)
If Left(replace_key, 1) = "$" Then
Application.StatusBar = tfilename & "正在替换图纸内字符串" & replace_key
Call replacetext_cad_file(cadDoc, j, k) '普通字符串替换
End If
If Left(replace_key, 1) = "<" Then
Application.StatusBar = tfilename & "正在替换图框内字符串" & replace_key
Call replacekuaitext_cad_file(cadDoc, kuainame, j, k) '块内字符串替换
End If
If Left(replace_key, 1) = "[" And Sheet5.Cells(j, k) <> "" Then
Application.StatusBar = tfilename & "正在替换表格内容" & replace_key
Call replacetabletext_cad_file(cadDoc, j, k,wb) '更新图纸的表格
End If
Next
Application.StatusBar = ""
'Application.ScreenUpdating = True '屏幕自动更新
MsgBox "已生成全部图纸"
End Sub
'子程序代码是打开第二个xlsx文件
'希望不显示,Workbooks.Open 时WPS会显示,CreateObject时 WPS 不会显示
'Workbooks.Open ,CreateObject 在Excel中均不显示第二个打开的文件
'判断指定为文件是否已经打开
Function isWbOpen(filename As String) As Boolean
Dim w As Workbook
For Each w In Application.Workbooks
If w.Name = filename Then isWbOpen = True: Exit Function
Next
isWbOpen = False
End Function
'安全打开excel文件,需要把文件名和目录名分开传递
' Function openWb(filename As String, pth As String, Optional M As Boolean = False) As Workbook
Function openWb(filename As String, pth As String) As Workbook
If isWbOpen(filename) Then
Set openWb = Application.Workbooks(filename)
Else
On Error Resume Next
' Set openWb = Workbooks.Open(pth & filename, UpdateLinks:=0, ReadOnly:=M) '使用此方式打开,excel环境下不会显示打开的文件,且
'excel代码运行时,状态栏会更新,但是WPS环境下运行,会显示打开的文件,状态栏只会显示第一次打开新文件时的Application.StatusBar 赋值的内容,后面不会更新
Set openWb = CreateObject(pth & filename)'使用此方式打开,excel环境下不会显示打开的文件,且excel代码运行时,状态栏会更新,但是
'WPS环境下运行,不会显示打开的文件,状态栏不会更新
If Err <> 0 Then
Err.Clear
Set openWb = Nothing
End If
End If
End Function
|
|