0

I would like to find out if anyone can help me. I am not completely computer literate so if responding please make it as simple as possible.

I was using macro on an excel spreadsheet (windows 7) to produce invoices; which when run produced a word and pdf file. I have just changed to a windows 10 laptop and it is no longer working and I cant contact the person who initially wrote the code for me (below):

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

The error I am getting is Run-Time Error 5941 - requested members of the collection does not exist.

Please let me know if you can help or can see an error in the code? The line that is showing the error is:

Windows(MainDoc).Activate

Kind Regards,

Joseph

Community
  • 1
  • 1
DJKAPA
  • 1
  • 2
  • Can you please add at least on which line you get the error message? Here are some ressources to help you ask a great question: http://idownvotedbecau.se/nodebugging/ - https://stackoverflow.com/help/mcve - https://stackoverflow.com/help/how-to-ask – rollstuhlfahrer Jan 24 '18 at 13:30
  • Windows(MainDoc).Activate Next i – DJKAPA Jan 24 '18 at 13:47
  • My guess is that `MainDoc` no longer exists, because you've saved as `docname`. You should [read this](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) about how to avoid using `Activate` and `Select`. If you make an object variable reference to the document, you can avoid the error. – David Zemens Jan 24 '18 at 18:35

0 Answers0