2
votes

I'm trying to automate a macro that converts an Excel table to LaTeX source code with another simple macro. Basically I don't want to use the userform, so I'm trying to call the button click of the desired output I want, but I'm having problems.

It is the Excel2Latex macro on ctan.org

Userform here

Project layout here

Here is the main sub, as you can see my commented parts are all the different things I've been trying after searching for help.

Sub latex()
With NewController
    Set .View = NewView
    Set .Model = NewDefaultModel
    Set .Storage = NewStorage
    .Run
    Application.Wait Now + TimeValue("00:00:02")
   frmConvert.cmdSave.Object.Value = True

   'UserForm2.CommandButton1.Object.Value = True


    'frmConvert!cmdSave.SetFocus

    'SendKeys "{Enter}"

    'Call frmConvert.cmdCopy_Click
    'Call frmConvert.cmdSv
    'Call frmConvert.cmdCopy_Click
    'Call Memento.SaveConversionResultToFile
    'Application.Wait Now + TimeValue("00:00:03")
    'Application.Run "frmConvert.cmdCopy_Click", txtResult
    'frmConvert.cmdCopy = True
    'frmConvert.cmdCopy = vbClick
    'frm 'Application.Run frmConvert.cmdSave
    'Call frmConvert.cmdCopy_Click
    'frmConvert.cmdSave = vbClick

End With



End Sub

The code behind the button I want is a Private Sub

Private Sub cmdSave_Click()
  SaveConversionResultToFile mModel
  Hide
End Sub

When I try to call the cmdSave_Click sub it returns run time error 91, object variable or with block variable not set. I've tried setting the sub to Public too.

The most luck I've had so far is activating the copy to clipboard button on the userform, but it just copies the default text in the userform, not the LaTeX table that is generated in the window.

Any suggestions?

Public Sub SaveConversionResultToFile(ByVal pModel As IModel)
 Dim sFileName As String
 sFileName = pModel.AbsoluteFileName
 If sFileName = "" Then Exit Sub

 Open sFileName For Output As 1
 Print #1, pModel.GetConversionResult;
 Close #1
End Sub

I get the error on the sFileName = pModel.AbsoluteFileName line in reference to MatthewD's question.

1
Instead of making an attempt to call the Event why don't you put the event's functionalities in a public Sub and then call it.cyboashu
I've tried this as well, I'll report back with the exact errorzpb21
Does it have something to do with it being a modeless userform?zpb21
Most likely pModel is not intialized. Before this line sFileName = pModel.AbsoluteFileName check if pModel is nothing. Msgbox pModel Is Nothing. What's the result? True or False?cyboashu
So you need to intialize pModel first and then only you can access its property AbsoluteFileNamecyboashu

1 Answers

0
votes

Just call the controls event.

Sub latex()
    With NewController
        Set .View = NewView
        Set .Model = NewDefaultModel
        Set .Storage = NewStorage
        .Run
        Application.Wait Now + TimeValue("00:00:02")

        cmdCopy_Click
        cmdSave_Click

    End With
End Sub