Vba AI access
보이기
AI access를 엑셀에서 접근하려고 할 때
VBA사용
크롬 사용
selenium 활용
Dim options As New ChromeOptions '객체 인식 오류 나는 코드
Sub OpenChromeWithProfile()
Dim driver As New ChromeDriver
Dim options As New ChromeOptions
Dim profilePath As String
' 사용하려는 Chrome 프로필 경로 설정
' 일반적인 프로필 경로 예시 (사용자 환경에 맞게 수정 필요)
profilePath = "C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Profile 1"
' Chrome 옵션 설정
options.AddArgument "--user-data-dir=" & profilePath
' 추가 옵션 (필요시 주석 해제하여 사용)
'options.AddArgument "--start-maximized" ' 창 최대화
'options.AddArgument "--disable-extensions" ' 확장 프로그램 비활성화
' 설정된 옵션으로 Chrome 시작
driver.Start "chrome", options
' 웹사이트 열기 (예: Google)
driver.Get "https://www.google.com"
' 여기에 필요한 추가 작업 코드 작성
' 브라우저 유지 (즉시 종료하지 않음)
' 나중에 브라우저를 닫으려면 driver.Quit 사용
' driver.Quit
End Sub
Function GetChromeProfiles() As Collection
' Chrome 프로필 목록 가져오기
Dim profiles As New Collection
Dim userDataDir As String
Dim fso As Object
Dim folder As Object
Dim subFolder As Object
' FileSystemObject 생성
Set fso = CreateObject("Scripting.FileSystemObject")
' Chrome 사용자 데이터 디렉토리 경로 (일반적인 위치)
userDataDir = Environ("LOCALAPPDATA") & "\Google\Chrome\User Data"
' 디렉토리 존재 확인
If fso.FolderExists(userDataDir) Then
Set folder = fso.GetFolder(userDataDir)
' Default 프로필 추가
profiles.Add userDataDir & "\Default", "Default"
' 추가 프로필 폴더 탐색
For Each subFolder In folder.SubFolders
If InStr(subFolder.Name, "Profile") > 0 Then
profiles.Add userDataDir & "\" & subFolder.Name, subFolder.Name
End If
Next subFolder
End If
Set GetChromeProfiles = profiles
End Function
Sub SelectProfileAndLaunchChrome()
' 프로필 선택하고 Chrome 실행하기
Dim profiles As Collection
Dim profile As Variant
Dim selectedProfile As String
Dim profileNames() As String
Dim i As Integer
Dim selectedIndex As Integer
' 프로필 목록 가져오기
Set profiles = GetChromeProfiles()
' 프로필이 없으면 종료
If profiles.Count = 0 Then
MsgBox "Chrome 프로필을 찾을 수 없습니다.", vbExclamation
Exit Sub
End If
' 프로필 이름 배열 만들기
ReDim profileNames(1 To profiles.Count)
i = 1
For Each profile In profiles
profileNames(i) = profiles(i)
i = i + 1
Next profile
' 사용자에게 프로필 선택 요청
selectedIndex = Application.InputBox("사용할 Chrome 프로필 번호를 선택하세요 (1-" & profiles.Count & "):" & vbNewLine & _
Join(profileNames, vbNewLine), Type:=1)
' 취소 확인
If selectedIndex < 1 Or selectedIndex > profiles.Count Then
MsgBox "유효한 프로필을 선택하지 않았습니다.", vbExclamation
Exit Sub
End If
' 선택된 프로필로 Chrome 실행
selectedProfile = profiles(selectedIndex)
' Chrome 드라이버 생성 및 실행
Dim driver As New ChromeDriver
Dim options As New ChromeOptions
options.AddArgument "--user-data-dir=" & selectedProfile
driver.Start "chrome", options
' 테스트용 URL 열기
driver.Get "https://www.google.com"
MsgBox "Chrome이 " & profileNames(selectedIndex) & " 프로필로 실행되었습니다.", vbInformation
End Sub