none
Runtime error 4160 bad file name RRS feed

  • Question

  • I get Runtime error 4160 bad file name.

    This error only recently started to show up after using the code for a few years.

    This error only occurs, however, when I have a windows file explorer open and have a word document selected. when i make sure to unselect the/any word file the error does not occur.

    here is the code in excel VBA that then runs a code in Word VBA

    Sub InsertFieldFILENAME()

    Dim WordApp As Object

    On Error Resume Next
    Set WordApp = GetObject(, "Word.Application")
    On Error GoTo 0

    If WordApp Is Nothing Then
    Set WordApp = CreateObject("Word.Application")
    End If

    WordApp.Visible = True

    WordApp.Run "InsertFieldFILENAME"
    End Sub

    WOrd VBA code: 

    Sub InsertFieldFILENAME()
    '
    ' insertfieldFILENAME Macro
    ' created in word
    '
        ActiveDocument.Windows(1).ActivePane.View.Type = wdPrintView
        Application.WindowState = wdWindowStateNormal
            WordBasic.ViewFooterOnly
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.EndKey Unit:=wdLine
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "FILENAME \* CHARFORMAT ", PreserveFormatting:=False
        ActiveWindow.ActivePane.View.NextHeaderFooter
        ActiveWindow.ActivePane.View.NextHeaderFooter
        Selection.MoveDown Unit:=wdLine, Count:=1
        Selection.EndKey Unit:=wdLine
        Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "FILENAME \* CHARFORMAT ", PreserveFormatting:=False
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End Sub

    Tuesday, September 11, 2018 6:34 PM

All replies

  • Hi Brycen,


    You can try this solution, please see the link below, Hopefully it helps you.

    run time error 4160 bad file name

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Wednesday, September 12, 2018 12:57 PM
    Moderator
  • I dont believe this would apply to my situation due to the fact that I dont have any file open besides the excel file when the VBA code is executed. That link talks about what to do if the word file is open.

    My issue is that in the window explorer if i just have clicked(but not opened) a word file, while running the excel vba code I get the bad file name code, which then prevents just the final VBA code from running, 90% of the code runs and merges info correctly. The word VBA code just doesnt run. However, if I make sure to not have a Word file highlighted/clicked the problem never happens.

    The funny thing is this is a recent issue as it has never happened in the 1000+ times it has been ran in the past. using same version of all applications.

    Friday, September 21, 2018 4:18 PM