• 締切済み

クラスモジュールの処理

VB6.0で開発しています。 以下のようにクラスモジュールを作ったのですが フォームでコマンドボタンを押したら 処理されるようにしたいのですが どうすればいいかわかりません。 教えてください。 Class CExcel Public App Public WshShell Public ErrDescription Private WorkBook Public CurBook Private Sub Class_Initialize() Call InitSetting() End Sub Private Sub Class_Terminate() Call Quit() End Sub Public Default Function InitSetting() if IsEmpty( App ) then Set App = CreateObject("Excel.Application") end if if IsEmpty( WshShell ) then Set WshShell = CreateObject("WScript.Shell") end if App.DisplayAlerts = False Set CurBook = Nothing end function Public Function Quit() If not IsEmpty( App ) Then For Each Workbook In App.Workbooks WorkBook.Saved = True Next App.Quit Set App = Nothing App = Empty Set CurBook = Nothing End If End Function Public Property Let Visible( bFlg ) App.Visible = bFlg End Property Public Property Get Visible Visible = App.Visible End Property Public Function Open( strPath ) on error resume next Set Open = App.Workbooks.Open(strPath) if Err.Number <> 0 then Set Open = Nothing ErrDescription = Err.Description Exit Function end if on error goto 0 Set CurBook = Open ' アクティブなウィンドウを最大化 App.ActiveWindow.WindowState = xlMaximized End Function Public Function Create( strPath ) Dim nBooks App.Workbooks.Add nBooks = App.Workbooks.Count Set Create = App.Workbooks( nBooks ) Set CurBook = Create CurBook.Activate App.ActiveWindow.WindowState = xlMaximized if strPath <> "" then on error resume next CurBook.SaveAs( strPath ) if Err.Number <> 0 then MsgBox( Err.Description ) Exit Function end if on error goto 0 end if End Function Public Function Close( MyBook ) if IsObject( MyBook ) then MyBook.Saved = True MyBook.Close Set MyBook = Nothing MyBook = Empty else if CurBook is Nothing then else CurBook.Saved = True CurBook.Close Set CurBook = Nothing end if end if End Function Function Save( MyBook ) if IsObject( MyBook ) then MyBook.Save else CurBook.Save end if End Function Function SaveAs( MyBook, strPath ) if IsObject( MyBook ) then MyBook.SaveAs strPath else CurBook.SaveAs strPath end if End Function Function Load( strPath ) if not IsEmpty( App ) then MsgBox( "Excel をロードする前に、Quitを実行して下さい   " ) Exit Function end if Call WshShell.Run( _ "RunDLL32.EXE shell32.dll,ShellExec_RunDLL " & _ strPath _ ) End Function End Class

みんなの回答

  • Yes_No_F
  • ベストアンサー率40% (4/10)
回答No.2

ソースは全部読みませんでした。 クラスの使い方の簡単な例を クラスジュールに貼付け(オブジェクト名:Class1) Option Explicit Public Function test(a As Integer, b As Integer) As Integer    test = a + b End Function 標準モジュールに貼り付け Option Explicit Sub Sample() Dim M As New Class1 Dim x As Integer Dim y As Integer x = 1 y = 2 MsgBox M.test(x, y) End Sub 標準モジュールのSampleを実行すれば、1+3の計算を実行します。 参考になるでしょうか?

  • unamana19
  • ベストアンサー率62% (56/89)
回答No.1

>処理されるようにしたいのですが >どうすればいいかわかりません。 なにをどのように処理したいのでしょうか? クラスの利用方法がわからないのでしょうか?

kinoko731
質問者

補足

はい、利用方法がわかりません。

関連するQ&A

  • Excel VBAで確認なしで上書き保存

    起動中のBookでファイル名が Data.xlsのものを閉じたいと思いますが、 上書き確認メッセージを出さないように oExcel.DisplayAlerts = False を設定すると エラーになります。 CreateObject で作成した場合は、DisplayAlerts が使えるようですが、 すでに起動済みのBookを確認なしで上書き保存するにはどうしたらいい のでしょうか? Sub UnloadFile()   Dim myBook2 As Workbook   Dim myBook1 As Workbook    For Each myBook2 In Workbooks      If myBook2.Name = "Data.xls" Then        Set myBook1 = myBook2        myBook1.SaveAs myBook1.FullName        myBook1.Close      End If    Next myBook2    Set myBook1 = Nothing End Sub

  • ファイルが既に開いているかどうかを取得するには

    Sub Sample1() Dim App As Object Dim MyFileName As String Set App = CreateObject("Excel.Application") MyFileName = "C:\Users\test.xlsm" With App .Workbooks.Open Filename:=MyFileName .Visible = True If .ReadOnly Then MsgBox "既に開いています" App.Quit '既に開いているのなら、閉じる End If End With End Sub このようなコードを作ったのですが、どうやらIf .ReadOnly Thenの部分が間違っているようです。 エラーになります。 既にファイルが開いているか、読み取り専用かどうかを取得するコードをご教授ください。

  • VBA IE読み込み出来ないときの処理

    エクセルVBAで IE読み込み完了まで、というサンプルは有るのですが 時間来ても、読み込め無い時 中止の処理ですが、 下記で如何でしょうか(実験が出来ないので、お知恵拝借) さらに、もっと、スマートな方法が有ればご教示ください。 Set IE = CreateObject("internetExplorer.application") url = yobiurl ' "http://admin.blog.fc2.com/control.php?mode=editor&process=new" With IE .Navigate url .Visible = True i = 1 Do While .Busy = True Call Sleep(50) '0.05sec * 400 =20秒 DoEvents i = i + 1 If i > 399 Then .Quit ' End With Set IE = Nothing Exit Sub End If Loop i = 1 Do While .document.ReadyState <> "complete" Call Sleep(50) DoEvents i = i + 1 If i > 399 Then .Quit ' End With Set IE = Nothing Exit Sub End If Loop

  • VBA 新規にエクセルを開き既存のファイルを開く

    VBAで新規にエクセルのアプリケーションを起動し、 その中に既存のファイルを起動する方法は有りますか? Sub Sample() Dim appExcel As Excel.Application Dim WSH As Variant Dim strPath As String Set appExcel = New Excel.Application Set WSH = CreateObject("Wscript.Shell") strPath = ActiveWorkbook.Path With appExcel .Visible = True .Workbooks.Add .ActiveWorkbook.SaveAs (strPath & "\ test.xls") End With Set WSH = Nothing End Sub このコードは、ネットから拾ったサンプルコードなのですが 新しいアプリケーションでエクセルを立ち上げることはできたのですが 新規のブックが開いてしまい、 更に、開きたいファイルに上書き保存してしまいそうです。 新規のブックが開く原因は .Workbooks.Addで、 上書き保存する原因は .ActiveWorkbook.SaveAs だとわかってるのですが、 この部分を同変更すればいいのかがわかりません。 Workbooks.Open?Filename:="C:\Users\test.xlsx" だと、現在実行しているvbaファイルを同じ枠内で 該当のファイルが開いてしまいます。

  • vbスクリプトからexcelのVBAを起動したい。

    vbスクリプトからexcelのVBAを起動したいので、初心者ながら下記のようにコーディングしましたが、excelのVBA;main()が実行できません。  <excelのVBA;main()は単独では正常に起動します。> vbスクリプトからexcelのVBAの起動の方法を教えて下さい。 そのスクリプトをタスク(元旦)に起動したいです。 <vbスクリプト コーディング> dim exlApp On Error Resume Next set exlApp = createobject("Excel.Application") exlApp.visible = true exlApp.Workbooks.Open "C:\年賀状.xls" exlApp.run "Main" If Err Then WScript.Quit End IF If Not(exlApp Is Nothing) Then exlApp.Quit set exlApp = nothing End If

  • 「もし既にファイルを開いているなら、開かない」

    おはようございます。 accessからExcelのブックを開く処理をしています。 VBAでファイルを開く時に、「もし既にファイルを開いているなら、開かない」 という行為をしたいのですが、どのようにすればいいでしょうか? *************************************************************************** Sub サンプル() Dim xlApp As Object Dim xlBook As Object Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Open("C:\【Excel】\料理.xlsm") xlApp.Visible = True'ファイルを開く?表示する? Set xlApp = Nothing Set xlBook = Nothing End Sub *************************************************************************** このコードの、 xlApp.Visible = Trueを、 if xlBook がまだ開いてない then xlApp.Visible = True Else End If のような事がしたいのですが、可能でしょうか? ご教授よろしくお願いします。

  • 既に開いているエクセルを閉じるには?

    既に該当のファイルが開いているのなら閉じたいのですが xlBook.Quitだとエラーになります。 Sub test1() Dim xlApp As Excel.Application Dim xlBook As Workbook Dim FileName As String FileName = "C:\test.xlsm" Set xlApp = GetObject("", "Excel.Application") 'GetObjectで合ってるか不安 Set xlBook = xlApp.Workbooks.Open(FileName) If xlBook.ReadOnly = True Then 'ファイルが開いてるのなら MsgBox "既にファイルが開いているので閉じます。" xlBook.Quit 'エラー 438 End If xlApp.Quit 'これって何のために必要? Set xlBook = Nothing 'ココを通り過ぎるのにすごく時間がかかる。 Set xlApp = Nothing End Sub と言うコードを作りました。 xlBook.Quitがダメならどのコードを使えばいいでしょう? また、 GetObject("", "Excel.Application") と言う開き方でいいのでしょうか? あと、 xlApp.Quit は何のために必要なのでしょう? タスクマネージーのプロセスを見ると、 Set xlApp = GetObject("", "Excel.Application") を通る時に新しいEXCEL.EXEが作成され、 Set xlApp = Nothing を通り過ぎる時に、そのEXCEL.EXEが消えます。 だから xlApp.Quit は不要ですか?

  • vba 読み取り専用で他のファイルを開くには

    エクセルvbaで他のエクセルファイルを読み取り専用で開きたいのですが 「読み取り専用で!」とするにはどうすればいいですか? 今のままだとエラーになります。 ------------------------- Sub a() Set App = CreateObject("Excel.Application") Set xlBook = App.Workbooks.Open(MyFileName) With App .Visible = True .ReadOnly '実行時エラー438 End With Set App = Nothing End Sub

  • CreateObjectとGetObjectの違い

    当方エクセル2003です。 Sub test_CreateObject() Dim App As Excel.Application Dim MyFileName As String Set App = CreateObject("Excel.Application") MyFileName = ActiveWorkbook.Path & "\新規Microsoft Excel ワークシート.xls" With App .Workbooks.Open FileName:=MyFileName .Visible = True End With Set App = Nothing End Sub --------------------------------------------------------- Sub test_GetObject() Dim App As Excel.Application Dim MyFileName As String Set App = GetObject(, "Excel.Application") MyFileName = ActiveWorkbook.Path & "\新規Microsoft Excel ワークシート.xls" With App .Workbooks.Open FileName:=MyFileName .Visible = True End With Set App = Nothing End Sub この二つは何が違うのでしょうか? どちらも既存のエクセルファイルがが開きます。

  • VBscriptでIEスクリプト動かない

    VBScript初心者です。HTMLに書かれた文字を取ってきて環境変数に入れるスクリプトを作っているのですが、うまく動きません。かなりデバッグも頑張りましたしレファレンスも見ましたが解決に至らず。ご教示お願い致します。 Dim ie Set ie = CreateObject("InternetExplorer.Application") ie.visible = True Dim wshShell Dim wshUserEnv Set wshShell = CreateObject("WScript.Shell") Set wshUserEnv = wshShell.Environment("User") Main Public Function Main() Dim hirake Dim nakami Dim taagetto Dim kekka hirake = "etqw.dip.jp" ie.Navigate hirake Do Until ie.Busy = False Or ie.readystate <= 3 WScript.Sleep 100 Loop WScript.Sleep 100 nakami = ie.Document.getElementsByClassName("status-table") Set taagetto = nakami(2).innertxt Set kekka = Mid(taagetto,InStr(taagetto,"Password:") + 1) wshUserEnv.Item("_KOREDAYO") = kekka ie.Quit Set ie = Nothing Set hirake = Nothing Set nakami = Nothing Set taagetto = Nothing Set kekka = Nothing End Function