2017-01-31 1 views
0

Mac 용 Excel에서 VBA 스크립트를 사용하여 XML 파일을 생성하는 다음 코드는 Mac 2016입니다. Mac에서 UTF-8 파일을 생성 할 수 없다는 것이 유감이지만 Excel VBA. ExcelScript 내에서 AppleScript를 호출하는 것이 좋습니다. AppleScript를 사용한 적이 없습니다. Excel에서 코드를 변환하여 AppleScript에 호출하여 UTF-8 파일을 생성 한 다음이 파일에 쓰기 만하면 큰 도움이 될 것입니다. .Mac Excel VBA에서 AppleScript를 사용하여 UTF-8 파일 생성

Sub createXMLFile() 
Dim My_Path1 As String 
My_Path1 = MacScript("return (path to desktop folder) as string") 

Dim rng1 As Range, rng2 As Range, cl As Range 
Set rng1 = ThisWorkbook.Sheets("upload").Range("A2") 
Set rng1 = ThisWorkbook.Sheets("upload").Range(rng1, rng1.End(xlDown)) 

Dim strPath As String 
strPath = "cancel_reasons" 
Dim fnum As Integer 
fnum = FreeFile() 

Open My_Path1 & Trim(strPath) & ".xml" For Output As #fnum 

Print #fnum, "<?xml version=""1.0""?>" 
Print #fnum, "<x:cancelShiftReasons xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation=""urn:cancelShiftReasons ./cancelshiftreasonmaster.xsd"" xmlns:x=""urn:cancelShiftReasons"">" 

For Each cl In rng1 
    Call write_xml_line_2(cl, fnum) 
Next cl 

Print #fnum, "</x:cancelShiftReasons>" 

Close #fnum 
End Sub 

답변

0

Applescript에서는 데이터를 쓸 때«class utf8»로 지정하면됩니다. 전반적으로 AS 단계는 VBA 단계와 매우 유사합니다.

스크립트가 Excel에서 실행되기 때문에 Excel 문서의 행 수를 확인하기 위해 "try"블록 (VBA의 "on error"와 유사)을 추가했습니다. 문서가 열려 있지 않으면 스크립트가 메시지와 함께 중지됩니다. 나는 그것을 명확하게하기 위해 많은 의견을 덧붙였다.

set H1 to "<?xml version=\"1.0\"?>" -- to insert " in Applescript, you must escape it by adding \ before 
set H2 to "<x:cancelShiftReasons xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation=\"urn:cancelShiftReasons ./cancelshiftreasonmaster.xsd\" xmlns:x=\"urn:cancelShiftReasons\">" 
set H3 to "</x:cancelShiftReasons>" 

set MyPath to (path to desktop folder) as string 
set myFile to "cancel_reasons1.xml" 

try -- try to count number of rows in the range 
    tell application "Microsoft Excel" to set NbRows to count of rows of used range of active sheet -- assume that no empty rows in middle 
on error 
    display dialog "Error : Impossible to count rows" -- may be no Excel document open,... 
    return 
end try 

set fnum to open for access file (MyPath & myFile) with write permission 
set eof of fnum to 0 --> empty file contents if already exists 
write (H1 & return) to fnum as «class utf8» 
write (H2 & return) to fnum as «class utf8» 

repeat with I from 2 to (NbRows + 1) -- loop through cells starting from A2 
    tell application "Microsoft Excel" to set Cl to value of range ("A" & I & ":A" & I) of active sheet 
    write (Cl & return) to fnum as «class utf8» -- write value of the cell I 
end repeat 

write H3 to fnum as «class utf8» -- not sure if return is required at end of xml ? 
close access funm 

마지막 줄에는 xml 파일에 대한 의문이 있습니다. 마지막에 반환이 필요한지 확실하지 않습니다. 필요하다면 다른 "write"라인과 같이 리턴을 추가하십시오.