カレントフォルダの取得と変更
【オブジェクト】
_Application
【プロパティ】
[設定を取得]
_bstr_t object->DefaultFilePath
[設定の変更]
object->DefaultFilePath = _bstr_t
【内容】
ファイルを開く際のカレントフォルダ名の取得と変更をします。
[ツール]-[オプション]-[全般]タブの-[カレントフォルダ名]にあたります。
(注意)設定の変更を有効にするには、Excelの再起動が必要です。
【サンプル】
//Excelを操作するためのタイプライブラリを読みこむ(Excel2000用)
#import "C:\Program Files\Microsoft Office\Office\Mso9.dll" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\Vbe6ext.olb" no_namespace
#import "C:\Program Files\Microsoft Office\Office\excel9.olb" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
//名前空間の設定
using namespace Excel;
//オブジェクトの定義
_ApplicationPtr pXL;
//Excelの起動
void CImp_app_DefaultFilePathDlg::OnButton1()
{
//Excelの起動
pXL.CreateInstance(L"Excel.Application");
//Excelを表示
pXL->Visible = TRUE;
//WorkBookを新規で開く
pXL->Workbooks->Add();
}
//カレントフォルダの取得
void CImp_app_DefaultFilePathDlg::OnButton2()
{
//カレントフォルダの取得
_bstr_t buf = pXL->DefaultFilePath;
//取得データの表示
AfxMessageBox("カレントフォルダ名 = " + buf);
}
//カレントフォルダの変更
void CImp_app_DefaultFilePathDlg::OnButton3()
{
CString strBuf;
_bstr_t buf;
//変更先のディレクトリを取得
m_ed1.GetWindowText(strBuf);
//カレントフォルダの変更
buf = (_bstr_t)strBuf;
pXL->DefaultFilePath = buf;
//変更データの表示
buf = pXL->DefaultFilePath;
//取得データの表示
AfxMessageBox("カレントフォルダ名 = " + buf + "\n(設定を有効にするには、Excelを再起動する必要があります)");
}
//Excelを終了
void CImp_app_DefaultFilePathDlg::OnButton4()
{
pXL->Quit();
pXL = NULL;
}
[サンプル・ソース]