{"id":4426,"date":"2014-05-03T23:44:11","date_gmt":"2014-05-03T14:44:11","guid":{"rendered":"http:\/\/www.ka-net.org\/blog\/?p=4426"},"modified":"2014-05-03T23:44:11","modified_gmt":"2014-05-03T14:44:11","slug":"%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%9f%e3%82%bb%e3%83%ab%e7%af%84%e5%9b%b2%e3%82%92utf-8%e3%82%84euc-jp%e7%ad%89%e3%81%ae%e3%83%86%e3%82%ad%e3%82%b9%e3%83%88%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%a8","status":"publish","type":"post","link":"https:\/\/www.ka-net.org\/blog\/?p=4426","title":{"rendered":"\u6307\u5b9a\u3057\u305f\u30bb\u30eb\u7bc4\u56f2\u3092UTF-8\u3084EUC-JP\u7b49\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u51fa\u529b\u3059\u308bExcel\u30de\u30af\u30ed"},"content":{"rendered":"<p>Excel\u30d5\u30a1\u30a4\u30eb\u3092UTF-8\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3067\u51fa\u529b\u3059\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u306e\u3067\u3001Stream\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u3063\u305f\u7c21\u5358\u306a\u30de\u30af\u30ed\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Option Explicit\r\n\r\nPublic Sub Sample()\r\n  If LCase(TypeName(Application.Selection)) &lt;&gt; &quot;range&quot; Then Exit Sub\r\n  '\u9078\u629e\u7bc4\u56f2\u3092&#x5B;UTF-8,\u30bf\u30d6\u533a\u5207\u308a,CRLF\u6539\u884c,\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u56f2\u307f\u6709]\u3067&#x5B;C:\\Test\\Test.csv]\u3068\u3057\u3066\u51fa\u529b\r\n  RangeToText Application.Selection, &quot;C:\\Test\\Test.csv&quot;, &quot;utf-8&quot;\r\n  '\u9078\u629e\u7bc4\u56f2\u3092&#x5B;EUC-JP,\u30ab\u30f3\u30de\u533a\u5207\u308a,LF\u6539\u884c,\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u56f2\u307f\u7121]\u3067&#x5B;C:\\Test\\Test2.csv]\u3068\u3057\u3066\u51fa\u529b\r\n  RangeToText Application.Selection, &quot;C:\\Test\\Test2.csv&quot;, &quot;euc-jp&quot;, &quot;,&quot;, 10, False\r\n  MsgBox &quot;\u51e6\u7406\u304c\u7d42\u4e86\u3057\u307e\u3057\u305f\u3002&quot;, vbInformation + vbSystemModal\r\nEnd Sub\r\n\r\nPrivate Sub RangeToText(ByVal TargetRange As Excel.Range, _\r\n                        ByVal FilePath As String, _\r\n                        ByVal CharacterSet As String, _\r\n                        Optional ByVal Separator As String = vbTab, _\r\n                        Optional ByVal LnSeparator As Long = -1, _\r\n                        Optional ByVal FlgQuotes As Boolean = True)\r\n  '\u6307\u5b9a\u3057\u305f\u30bb\u30eb\u7bc4\u56f2\u3092\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u51fa\u529b\r\n  '----------------------------------------\r\n  'TargetRange:\u51fa\u529b\u5bfe\u8c61\u30bb\u30eb\u7bc4\u56f2\r\n  'FilePath:\u51fa\u529b\u5148\u306e\u30d5\u30a9\u30eb\u30c0\u30d1\u30b9\r\n  'CharacterSet:\u6587\u5b57\u30bb\u30c3\u30c8(HKEY_CLASSES_ROOT\\MIME\\Database\\Charset \u53c2\u7167)\r\n  'Separator:\u533a\u5207\u308a\u6587\u5b57\r\n  'LnSeparator:\u884c\u533a\u5207\u308a\u6587\u5b57(adCR:13, adCRLF:-1, adLF:10)\r\n  'FlgQuotes:&#x5B;&quot;]\u3067\u56f2\u3080\u304b\u3069\u3046\u304b\u3092\u6307\u5b9a\r\n  Dim str As String\r\n  Dim i As Long, j As Long\r\n  Const adTypeText = 2\r\n  Const adWriteChar = 0\r\n  Const adWriteLine = 1\r\n  Const adSaveCreateOverWrite = 2\r\n  \r\n  On Error Resume Next\r\n  With CreateObject(&quot;ADODB.Stream&quot;)\r\n    .Type = adTypeText\r\n    .Charset = CharacterSet\r\n    .LineSeparator = LnSeparator\r\n    .Open\r\n    For i = 1 To TargetRange.Rows.Count\r\n      For j = 1 To TargetRange.Columns.Count\r\n        If j = 1 Then\r\n          If FlgQuotes = True Then\r\n            str = ChrW(&amp;H22) &amp; TargetRange(i, j) &amp; ChrW(&amp;H22)\r\n          Else\r\n            str = TargetRange(i, j)\r\n          End If\r\n        Else\r\n          If FlgQuotes = True Then\r\n            str = str &amp; Separator &amp; ChrW(&amp;H22) &amp; TargetRange(i, j).Value &amp; ChrW(&amp;H22)\r\n          Else\r\n            str = str &amp; Separator &amp; TargetRange(i, j).Value\r\n          End If\r\n        End If\r\n      Next\r\n      If i = TargetRange.Rows.Count Then\r\n        .WriteText str, adWriteChar\r\n      Else\r\n        .WriteText str, adWriteLine\r\n      End If\r\n    Next\r\n    .SaveToFile FilePath, adSaveCreateOverWrite\r\n    .Close\r\n  End With\r\n  If Err.Number &lt;&gt; 0 Then\r\n    MsgBox &quot;Err:&quot; &amp; Err.Description, vbCritical + vbSystemModal\r\n    Exit Sub\r\n  End If\r\n  On Error GoTo 0\r\nEnd Sub<\/pre>\n<p>\u6587\u5b57\u30bb\u30c3\u30c8\u3084\u533a\u5207\u308a\u6587\u5b57\u3092\u5f15\u6570\u306b\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u4f8b\u3048\u3070\u3001[<em>LF\u533a\u5207\u308a\u306e\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u56f2\u307f\u7121\u3057EUC-JP\u30c6\u30ad\u30b9\u30c8<\/em>]\u3068\u3044\u3063\u305f\u30d5\u30a1\u30a4\u30eb\u3082\u7c21\u5358\u306b\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Excel\u30d5\u30a1\u30a4\u30eb\u3092UTF-8\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3067\u51fa\u529b\u3059\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u306e\u3067\u3001Stream\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u3063\u305f\u7c21\u5358\u306a\u30de\u30af\u30ed\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002 Option Explicit Public Sub Sample() If L [&hellip;]","protected":false},"author":1,"featured_media":7111,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,48],"tags":[],"class_list":["post-4426","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-office","category-excel-office"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.ka-net.org\/blog\/wp-content\/uploads\/eyecatch-Excel.png","jetpack_shortlink":"https:\/\/wp.me\/p4UZZr-19o","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4426"}],"version-history":[{"count":1,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4426\/revisions"}],"predecessor-version":[{"id":4427,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4426\/revisions\/4427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=\/wp\/v2\/media\/7111"}],"wp:attachment":[{"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ka-net.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}