BEAGLE-HC 医薬品、医療機器の研究・開発 ポータルサイト
 現在位置 : HOME > ITの活用 プログラミング > Word コードライブラリアン(ファイル操作)
くすりのこと
 治療薬の最前線
データブック
 ヘルスケア市場
 データブック
 (売上ランキング)
ニュース
 企業ニュース
 (パイプラインニュース)
 (財務・決算ニュース)
 (M&Aニュース)
 メディアニュース
ITの活用
 バリデーション
 セキュリティ
 WORD文書の作成
 プログラミング
 (Word/VBA)
 (Excel/VBA)
 (Access/VBA)
 (SAS)
 (SQL)
イベント
 イベントカレンダ
製薬会社研究
 製薬会社
 製薬会社研究
 決算短信一覧
 
 
●カレント文書のパスを取得する
●カレントディレクトリやフォルダを変更する
●(ダイアログボックス)選択したファイルのパスを取得する
●(ダイアログボックス)選択したファイルのディレクトリパスを取得する
●既存のWordファイルを開く
●Word文書を閉じる
●Word文書を一旦保存する
●名前をつけて保存する
●現在開かれている文書を処理
●フォルダの中のすべての文書を処理
●文書にパスワードを掛ける
●テキストファイルを書き出す
●開いている複数のWORD文書から特定の文書をActivateにする
 
本章では、ファイル操作に関するコードサンプルを例示します。
 
カレント文書のパスを取得する ↑ このページの最初へ
Sub カレント文書のパスを取得する()

  Dim mypath As String

  mypath = ActiveDocument.Path & Application.PathSeparator  '文書のパス

  mypath = CurDir() & Application.PathSeparator           'カレントのパス

End Sub
 
カレントディレクトリやフォルダを変更する ↑ このページの最初へ
Sub カレントディレクトリを変更する()

  Dim mypath As String
    mypath = ActiveDocument.Path & Application.PathSeparator

End Sub
 
(ダイアログボックス)選択したファイルのパスを取得する ↑ このページの最初へ
Sub FilePathGet1()

  Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)

  With dlgOpen

    .AllowMultiSelect = False
    .Title = "処理対象のフォルダーを指定してください。"
    .InitialFileName = "C:\Documents and Settings"
'初期表示パス
    .Filters.Clear
    
'[ファイルの種類]ボックスのリストに「Word 文書(*.doc)」を追加
    .Filters.Add "Word 文書(*.doc)", "*.doc"

    If .Show = -1 Then

      MsgBox .SelectedItems(1)

    End If

  End With

End Sub

'フルパスからファイル名を取り出す
MagBox .Mid(strPath, InStrRev(strPath, "\") + 1, Len(strPath) - InStrRev(strPath, "\"))
 
(ダイアログボックス)選択したファイルのディレクトリパスを取得する ↑ このページの最初へ
'表示するファイルの種類を指定しない方法
Sub FolderPathGet1()

  Dim strPath As String
  Dim dlgFind As Dialog

  Set dlgFind = Dialogs(wdDialogFileFind)

  With dlgFind

    Select Case .Display

      Case -1 'ファイルが選択されたとき
        'FileFind Dialogの更新
        .Update
        'FileFind Dialogからフォルダパスの取得
        strPath = .SearchPath

      Case Else 'キャンセルボタンが押されたとき
        End

    End Select

  End With

  MsgBox strPath

End Sub

  ※wdDialogFileOpen クラス : 
   wdDialogFileFind クラス : 
   FileSearch メソッド



表示するファイルの種類を指定する方法
Sub FolderPathGet2()

  Dim mydlgOpen As Dialog
  Dim mydlgFind As Dialog

  'ダイアログボックスを表示しファイル指定を要求します
  Set mydlgOpen = Dialogs(wdDialogFileOpen)
  Set mydlgFind = Dialogs(wdDialogFileFind)

  With mydlgOpen
    '表示されるファイルをワード文書に指定
    .Name = "*.doc"

    Select Case .Display

       Case -1 'ファイルが選択されたとき
        mydlgFind.Update
        strPath = mydlgFind.SearchPath

       Case Else 'キャンセルボタンが押されたとき
        End
    End Select

  End With

End Sub
 
既存のWordファイルを開く ↑ このページの最初へ
Sub FindFile1()

  Dim scrFileName As String

  scrFileName = "MyDoc.Doc"

  Documents.Open FileName:=.scrFileName, _
    ConfirmConversions:=(False), _
    ReadOnly:=(False), _
    AddToRecentFiles:=(False), _
    PasswordDocument:=(""), _
    PasswordTemplate:=(""), _
    Revert:=(False), _
    WritePasswordDocument:=(""), _
    WritePasswordTemplate:=(""), _
    Format:=wdOpenFormatAuto

End Sub

Sub FindFile2()

  scrPath = "C:\My Documents\"
  scrFileName = "MyDoc.Doc"

  ' Set the FileOpen dialog to display the criteria.
  Set dlg = Dialogs(wdDialogFileOpen)
  dlg.Name = scrFilePath & scrFileName

  ' If the file is not found, trap the error.
  On Error Resume Next

  ' Execute the dialog without displaying.If the file exists
  ' the dlg.Execute command will open the file.
  dlg.Execute

  ' If the file does not exist, display default error.
  If Err = 5174 Then
    MsgBox Err.Description
  End If

End Sub 
 
Word文書を閉じる ↑ このページの最初へ
Sub 文書を閉じる()

    ActiveDocument.Close

    Documents("Report.doc").Close SaveChanges:=wdDoNotSaveChanges

End Sub
 
Word文書を一旦保存する ↑ このページの最初へ
Sub 文書を保存()

    ActiveDocument.Save

End Sub
 
名前を付けて保存する ↑ このページの最初へ
Sub 名前を付けて保存()

  Dim newFileNM As String

    ChangeFileOpenDirectory ActiveDocument.Path & Application.PathSeparator
  newFileNM = "新規文書名称.doc"

  ActiveDocument.SaveAs FileName:=newFileNM, FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    SaveAsAOCELetter:=False

End Sub
現在開かれている文書を処理する ↑ このページの最初へ
Sub 開いている文書を処理する()

    For Each doc In Documents
        If doc.Name = "文書.doc" Then ・・・・・・
    Next doc

End Sub
フォルダの中のすべての文書を処理する ↑ このページの最初へ
Sub フォルダの中のすべての文書を処理する()

  Dim i As Integer
  Dim strPath As String
  Dim FC As Integer
  Dim fs As Variant
  Dim actDoc As Document
  Dim mydlgOpen As Dialog
  Dim mydlgFind As Dialog

  'ダイアログボックスを表示しファイル指定を要求します
  Set mydlgOpen = Dialogs(wdDialogFileOpen)
  Set mydlgFind = Dialogs(wdDialogFileFind)

  With mydlgOpen
    '表示されるファイルをワード文書に指定
    .Name = "*.doc"

    Select Case .Display

       Case -1 'ファイルが選択されたとき
        mydlgFind.Update
        strPath = mydlgFind.SearchPath

       Case Else 'キャンセルボタンが押されたとき
        End
    End Select

  End With

  '上記フォルダ中のワードファイルを検索します
  Set fs = Application.FileSearch
  With fs

    .LookIn = strPath
    .FileName = ".doc"

    If .Execute > 0 Then
      FC = .FoundFiles.Count

      For i = 1 To FC
        MsgBox .FoundFiles(i)  'n番目のファイル名を表示
      Next i
    Else
      MsgBox "ワードファイルは見つかりませんでした。"
    End If

  End With

End Sub
文書にパスワードを掛ける ↑ このページの最初へ
Sub 文書にパスワードを掛ける()

  Dim objApp As Object
  Dim newFileNM As String

  Set objApp = CreateObject("Word.Application")
  kizonFileNM = "既存文書名称.doc"

  With objApp

.    Documents.Open ActiveDocument.Path & Application.PathSeparator & kizonFileNM
      .ActiveDocument.Password = "読み取りパスワード"
      .ActiveDocument.WritePassword = "書き込みパスワード"

    .ActiveDocument.Save
    .Quit

  End With

  Set objApp = Nothing

End Sub
テキストファイルを書き出す ↑ このページの最初へ
Sub テキストファイルを書き出す()

  Dim n As Long
  Dim newFileNM As String

  newFileNM = "新規テキスト.txt"
  n = 1

  '本文書と同じディレクトリーにテキストファイルを書き出す
  Open ActiveDocument.Path & Application.PathSeparator & newFileNM For Output As #n

  Print #n, "あいうえお" ' 一行書き込む

  Close #n

End Sub
開いている複数のWord文書から特定の文書をActivateにする ↑ このページの最初へ
Sub DocActivate()

  Dim dc As Document

  For Each dc In Documents
    If dc.Name = "Word1.doc" Then Windows("Word1.doc").Activate
  Next dc

End Sub


Sub DocActivate2()

  Documents("Word1.doc").Activate

End Sub

↑ このページの最初へ

   ご意見・ご感想をお寄せください。 ‖ お問い合わせはこちらから ‖ このサイトについて           サイトマップ  ‖
  Copyright 2006 - 2013 uTRAM Corp. All Rights Reserved
 
PHARCIS(ヘルスケア最新情報提供)
Facebook for PHARCIS
ClinMark8
アクセスランキング
(2013年5月)

1位 企業ニュース
2位 売上ランキング
3位 メディアニュース
4位 製薬会社
5位 治療薬の最前線
6位 決算短信一覧
7位 データブック
8位 製薬会社研究
9位 イベントカレンダ
10位 パイプラインニュース