用于生成word和pdf发票的宏的宏

时间:2018-01-24 13:15:59

标签: excel excel-vba pdf ms-word excel-2010 vba

我想知道是否有人可以帮助我。我不是完全掌握计算机知识,所以如果回应请尽量简单。

我在excel电子表格(Windows 7)上使用宏来生成发票;在运行时产生一个单词和pdf文件。我刚刚换了一台Windows 10笔记本电脑,它不再工作了,我无法联系最初为我编写代码的人(下图):

Sub merge1record_at_a_time() '
' merge1record_at_a_time Macro
'
'
    Dim fd As FileDialog

        'Create a FileDialog object as a Folder Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd


        'Use the Show method to display the Folder Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then
                For Each vrtSelectedItem In .SelectedItems


                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
        SelectedPath = vrtSelectedItem

        Next vrtSelectedItem

        Else
        MsgBox ("No Directory Selected.  Exiting")
        Exit Sub
        End If
    End With


    'Set the object variable to Nothing.
    Set fd = Nothing


Application.ScreenUpdating = False


MainDoc = ActiveDocument.Name
    'ChangeFileOpenDirectory SelectedPath
    For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = i
                .LastRecord = i
                .ActiveRecord = i
                docname = .DataFields("PATIENT_NAME_").Value & " " & .DataFields("Company_name").Value _
            & " " & .DataFields("Invoice_Number").Value & ".docx"     ' ADDED CODE - Use ".docx" for Word Documents
                docname2 = .DataFields("PATIENT_NAME_").Value & " " & .DataFields("Company_name").Value _
                & " " & .DataFields("Invoice_Number").Value & ".pdf"
            End With
            .Execute Pause:=False
    Application.ScreenUpdating = False

        End With
    '*****************************************************************
    '** NOTE: \/\/\/\/ BELOW IS TO SAVE AS A WORD DOCUMENT \/\/\/\/ **
    '*****************************************************************
    ActiveDocument.SaveAs FileName:=docname, FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False
    'ActiveDocument.Close

    '*****************************************************************
    '** NOTE: \/\/\/\/ BELOW IS TO SAVE AS A PDF \/\/\/\/           **
    '*****************************************************************
    'set OpenAfterExport to False so the PDF files won't open after mail merge
    ActiveDocument.ExportAsFixedFormat OutputFileName:=docname2, _
    ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False


    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges


    Windows(MainDoc).Activate
    Next i


    Application.ScreenUpdating = True

End Sub

我得到的错误是Run-Time Error 5941 - requested members of the collection does not exist

如果您可以提供帮助或在代码中看到错误,请告诉我们?显示错误的行是:

视窗(MainDoc).Activate

亲切的问候,

约瑟夫

0 个答案:

没有答案