如何在Excel和Word之间创建代码?

kmbjn2e3  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(157)

我正在尝试创建一个VBA代码来执行此操作:
1.打开一个对话框以选择路径中的word文件:C:\Add-in\Company A\模板(docx)
1.选择活动工作表导航文件:“检查表- Navette”如果没有使用此名称打开的文件,则会显示一条消息:“错误请先按命令检查表”并退出宏
1.填充所有的书签的Word文件与内容单元格的名称等于书签(使用工作表Navette)
1.如果Navette工作表有一个名为“Civilité”的单元格,并且内容等于“Female”,则必须转到路径中的excel文件:C:\Add-in\Mapping.xlsx,并在word文件中搜索A列中的所有单词,然后替换为B列中的单词,否则替换为C列中的单词
1.打开一个对话框,输入路径以保存名称为TEST的单词(word和pdf格式)
1.关闭初始文件而不保存
1.退出所有应用程序
我被困在代码中,它甚至工作。当我试着跑的时候,它也被卡住了:(

Sub TestProcess()

'Initial process
    Dim fd As FileDialog
    Dim strFile As String
    Dim wdDoc As Document
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim rng As Range
    Dim SaveAsFileName As String
    Dim SaveAsFileFormat As Integer
    
'Dialog box to pickup the docx file
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .InitialFileName = "C:\Add-in\Company\Templates"
        .Filters.Add "Word Files", "*.docx", 1
        If .Show = -1 Then
            strFile = .SelectedItems(1)
        End If
    End With
    
    Set wdDoc = Documents.Open(strFile)
    
'Identify the checklist
    On Error Resume Next
    Set wb = Workbooks("Company - Navette.xlsx")
    On Error GoTo 0

'Handling with errors
    If wb Is Nothing Then
        MsgBox "ERROR 'Please select the command *Open Navette*first"
        wdDoc.Close
        Set wdDoc = Nothing
        Exit Sub
    End If

'Active Worksheet
    On Error Resume Next
    Set ws = wb.Sheets("Navette")
    On Error GoTo 0
    
    'Handling with errors
    If ws Is Nothing Then
        MsgBox "Sheet 'Navette' not found in the workbook."
        
        'For each Bookmark equal name cell replace with the content
        
        For Each wdBookmark In wdDoc.Bookmarks
            wdBookmark.Range.Text = ws.Range(wdBookmark.Name).Value
        Next
        
        
        'Save file
        'Open a dialog box to input the path to save the Word file
        SaveAsFileName = Application.GetSaveAsFilename(FileFilter:="Word Files (*.docx), .docx; PDF Files (.pdf), *.pdf", Title:="Save As", InitialFileName:=strFile)
        
        'Check if a file name and format are selected
        If SaveAsFileName <> "False" Then
            'Determine the selected file format
            If Right(SaveAsFileName, 4) = ".pdf" Then
                SaveAsFileFormat = 17
            Else
                SaveAsFileFormat = 0
            End If
            'Save the file in the selected format
            objDoc.SaveAs SaveAsFileName, FileFormat:=SaveAsFileFormat
        End If
        
        'Close Doc & Excel
        wdDoc.Close
        wb.Close
        
        'Reset the documents
        Set wdDoc = Nothing
        Set wb = Nothing
        Exit Sub
    End If
    
End Sub
7y4bm7vi

7y4bm7vi1#

即使这可能有点晚,要成功地从Excel打开Word文档(假设这是您正在做的事情),您需要执行以下操作:
1.首先,您需要在“工具”->“引用”下引用Excel中的MS Word对象模型(例如,Microsoft Word 16.0对象库
1.接下来,在代码中创建Word应用程序的示例:

Dim WordApp As Word.Application
Set WordApp = CreateObject("Word.Application")

我还推荐以下选项:

WordApp.Visible = False
WordApp.Options.WarnBeforeSavingPrintingSendingMarkup = False

1.之后,您可以打开Word文档,但您必须始终确保引用正确的Word对象(即WordApp),因此以您的示例为例:

Dim mDoc As Word.Document
Set mDoc = WordApp.Documents.Open(filePath)

顺便说一句,同样的方法也适用于如果你做的是相反的方式,即从MS Word应用程序打开Excel文件。
1.当你完成处理文件,我建议也退出Word应用程序,即,使用这样的东西:

If Not WordApp Is Nothing Then
    WordApp.Quit False
    Set WordApp = Nothing
End If

最后一个提示:如果已经有一个Word示例在后台运行,您将遇到问题。在运行此类代码之前,请确保在任务管理器中没有MS Word示例正在运行。

相关问题