Domanda

I'm having difficulties finding an answer for the following problem. What I'm trying to do is call a word macro from Excel with an argument, a filename to be specific. Excel creates a .txt file, I then need word to process it. The problem is how do I get word to recive the filename?

I have some code to start with and when using a hardcoded filename in the word macro I get it to work, but I need the filename to be passed as a variable.

The code so far:

    Public Function convertTxt(txtFile As String)

      Dim WD As Object
        Set WD = CreateObject("Word.Application")

        WD.Documents.Open ThisWorkbook.Path & "\Word\" & "far.docm"

        ' Note that the project name and module name are required to
        ' path the macro correctly.
        WD.Run "runTxtConversion(txtFile)"

End Function

Any suggestions?

This is the first time I post a question, so any advice on how I ask questions is also appreciated.

È stato utile?

Soluzione

Try using:

WD.Run "runTxtConversion", txtFile

The Run method has 31 arguments - the first is the macro name and the rest are any parameters you need to pass (all of those 30 are optional).

Your code was passing the literal text "runTxtConversion(txtFile)" as the name of the macro.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top