从Word到Excel的VBA

加贝

我的Word模板中的某些VBA代码存在问题。目的是使代码打开Excel书籍,引用名为“ Log”的工作表,并根据Word文档的名称查找行。名称匹配后,我想将单元格值更改为“ completed”。然后保存并关闭Excel工作簿。我尝试了下面的代码,它将打开正确的工作簿,但不会将单元格更新为“已完成”,但出现错误:

Private Sub CommandButton1_Click()
'***********************************************************************************************
'Message box asking if you are sure you are ready to submit report for completion
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Are you sure you are ready to submitt this Smart Learning Report?"        ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
Title = "Submit SLR"    ' Define title.
Help = "DEMO.HLP"    ' Define Help file.
Ctxt = 1000    ' Define topic context.
    ' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbNo Then    ' User chose Yes.
    MyString = "No"    ' Perform some action.
    Exit Sub
End If

'***********************************************************************************************
'File name to be used to update the status in the Log
Dim CONum As String

'File name to be used to save report as PDF
Dim PDF As String

CONum = ActiveDocument.FullName
PDF = Replace(CONum, ".docm", ".pdf")

'***********************************************************************************************
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'***********************************************************************************************
'Open excel, find cell with matching Word document name (CONum) in column K, 
_and change the cell value in column J (-1) to COMPLETED, save and close excel when completed.
Dim excelApp As Excel.Application
Dim openExcel As Excel.Workbook

  Set excelApp = Excel.Application
  Set openExcel = excelApp.Workbooks.Open("C:\Users\ggonzales\Desktop\SLR's\GPT SLR Submission.xlsm")
  excelApp.Visible = True

With openExcel
    Dim CRow As Excel.Range
    Set CRow = Sheets("Log").Range("K:K").Find(What:=CONum,     LookIn:=xlFormulas, LookAt:=xlPart, MatchCase:=False)
    If Not CRow Is Nothing Then
    CRow.Offset(, -1).Value = "COMPLETED"
    End If
ActiveWorkbook.Save 'Filename:=COFile, FileFormat:=52
ActiveWorkbook.Close
End With

'***********************************************************************************************
Application.ScreenUpdating = True
Application.DisplayAlerts = True

'***********************************************************************************************
'Delete Command Button so it does not show on the final report, _
and so no one can submit the same report twice.
For Each o In ActiveDocument.InlineShapes
   If o.OLEFormat.Object.Caption = "Complete & Submit Report" Then
        o.Delete
    End If
Next

'***********************************************************************************************
'Save a copy of the report as a PDF
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        PDF, ExportFormat:= _
        wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument

'***********************************************************************************************
'Close and save report (Word Document)
ActiveDocument.Close SaveChanges:=True

End Sub
大本钟

您需要Set对象变量:

Set CRow = Sheets("Log").Range("K:K").Find(What:=CONum, LookIn:=xlFormulas, LookAt:=xlPart, MatchCase:=False)

然后,您应该测试Find能否成功。您可以Offset在这里使用以简化操作。

If Not CRow Is Nothing Then
    CRow.Offset(,1).Value = "COMPLETED"
End If

请注意,您实际上并没有使用With...End With

修改后的代码:

With openExcel
    Dim CRow As Excel.Range
    Set CRow = .Sheets("Log").Range("K:K").Find(What:=CONum, LookIn:=xlFormulas, LookAt:=xlPart, MatchCase:=False)

    If Not CRow Is Nothing Then
        CRow.Offset(,1).Value = "COMPLETED"
    End If

    .Save 'Filename:=COFile, FileFormat:=52
    .Close
End With

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel 到 Word VBA 导出 - 未创建 Word 文档

来自分类Dev

VBA Word表到Excel段落和错误

来自分类Dev

将表格从Word复制到Excel-VBA

来自分类Dev

将多个表格从 Excel 插入到 Word VBA

来自分类Dev

VBA脚本将Excel图表复制到Word在更高版本的Word中不起作用

来自分类Dev

使用VBA将Excel范围复制到Word文件时遇到问题

来自分类Dev

将单元格数据从Excel复制到Word VBA

来自分类Dev

如何在VBA中将Excel变量从宏传递到Word宏

来自分类Dev

使用VBA将Excel范围复制到Word文件时遇到问题

来自分类Dev

通过VBA将Excel数据复制到格式化的Word表中?

来自分类Dev

VBA如何将格式文本从Excel传递到MS Word

来自分类Dev

VBA将word doc中表格中的文本复制到excel?

来自分类Dev

VBA 问题将特定行/列从 Word 表(带有合并行)复制到 Excel

来自分类Dev

Excel VBA 将 Word Doc 复制到 Outlook - 图像消失

来自分类Dev

从Excel将VBA插入Word

来自分类Dev

Excel VBA退出Word文档

来自分类Dev

从Word VBA阻止Excel提示

来自分类Dev

使用Excel宏VBA将Word文档中的第一个表复制到Excel

来自分类Dev

Excel VBA到C#

来自分类Dev

文本到行VBA Excel

来自分类Dev

Excel VBA文本到数字

来自分类Dev

Excel如果公式到VBA

来自分类Dev

Excel VBA例程到UDF

来自分类Dev

VBA Excel从Word表复制内容

来自分类Dev

从Excel调用Word VBA MsgBox函数

来自分类Dev

VBA Excel关闭Word Doc中的灰度

来自分类Dev

VBA Excel Copy Content From Word Table

来自分类Dev

VBA Excel / Word查找和替换

来自分类Dev

在Excel VBA中保留Word表的格式