Excel文档之外的Application.Volatile问题

时间:2018-04-06 09:41:13

标签: excel vba excel-vba

此代码在引用的Excel文档中起作用。

Application.Volatile
ThisWorkbook.CustomDocumentProperties("CLIENT").Value = CLIENT
ThisWorkbook.CustomDocumentProperties("Project").Value = PROJECT
ThisWorkbook.CustomDocumentProperties("DATE").Value = Date
ThisWorkbook.CustomDocumentProperties("Drawing/Job No").Value = JOB_NUMBER & " / " & NAMCON
ThisWorkbook.CustomDocumentProperties("Drawn By").Value = DRAWN_BY

但是当我尝试打开文档并编辑属性时它失败了。它在Application.Volatile失败了。我肯定错过了什么。如果我可以在引用的Excel文档中执行此操作,我当然应该能够打开文件并发送指令。

'excell api
Dim excelApp As Excel.Application
Dim targetWB As Workbook
Dim targetRange As Range
    'EXCELL , CUSTOM PROP

Sub PR_RUN()
    Call COPYFILE
    Call WRITE_TO_PRFORM
End Sub

Sub COPYFILE()
    JOB_NO_LP = "XXXX"
    Filepath = "C:\Users\User\Desktop\ADB NEW TEST\"
Dim SourceFile, DestinationFile
    SourceFile = "C:\TEST\XXXX - PRODUCTION FORM MARCO.xlsm" ' Define source file name.
    DestinationFile = Filepath & "PRODUCTION\01_CUT FILES\" & JOB_NO_LP & " - PRODUCTION FORM" & ".xlsm"    ' Define target file name."
    FileCopy SourceFile, DestinationFile ' Copy source to target.
End Sub

Sub WRITE_TO_PRFORM()
Dim excelApp As Excel.Application
Dim targetWB As Workbook
Dim targetRange As Range
    'Create new Excel Application
    Set excelApp = New Excel.Application
    'Keep hidden
    excelApp.Visible = False
    MsgBox "JOB_NO_LP: " & JOB_NO_LP
    Set targetWB = excelApp.Workbooks.Open(Filepath & "PRODUCTION\01_CUT FILES\" & JOB_NO_LP & " - PRODUCTION FORM" & ".xlsm") ' update with locaton
    'Keep hidden
    excelApp.Visible = False

' ------------------------------------------------------------------------------
' ghost Excel processes (may not be need with end statement)
' ------------------------------------------------------------------------------
Dim thisSheet As Worksheet
    Set thisSheet = ActiveWorkbook.Worksheets(1)
    thisSheet.Activate

'-----------------------------------------------------
' CUSTOM PROP SET
'-----------------------------------------------------
    Application.Volatile
    ThisWorkbook.CustomDocumentProperties("CLIENT").Value = CLIENT
    ThisWorkbook.CustomDocumentProperties("Project").Value = PROJECT
    ThisWorkbook.CustomDocumentProperties("DATE").Value = Date
    ThisWorkbook.CustomDocumentProperties("Drawing/Job No").Value = JOB_NUMBER & " / " & NAMCON
    ThisWorkbook.CustomDocumentProperties("Drawn By").Value = DRAWN_BY

'-----------------------------------------------------
'Close Save workbook
'-----------------------------------------------------
    excelApp.DisplayAlerts = False
    ActiveWorkbook.Save
    'excelApp.DisplayAlerts = True
    'Close Excel App
    ActiveWorkbook.Close
    excelApp.Quit
Dim BruteForce As String
    BruteForce = "TASKKILL /F /IM excel.exe"
    Shell BruteForce, vbHide
End Sub

0 个答案:

没有答案