none
Application.FileSearch not working in office2007 RRS feed

  • Question

  • Hello,

    I have the followiong code below, which wont work in 2007. And I cant work out how to do it. Can anyone help? (I keep going back to an old machine with 2003 on it !)

       

    Public Sub LoadPicture()

    Dim myRow As Integer
    Dim myCol As Integer

    myRow = InputBox("請輸入列數 (Rows)", "Row Size", 2)
    myCol = InputBox("請輸入欄位數 (Cols)", "Col Size", 2)


    With Application.FileSearch
    .Filename = "*.jpg"
    .lookin = ActiveDocument.Path
    .Execute
    For i = 1 To .FoundFiles.Count

    ''插入圖片
    Selection.InlineShapes.AddPicture Filename:= _
    .FoundFiles(i), LinkToFile:=False, SaveWithDocument:=True
    ''在圖片右邊輸入逗號

    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:=","
    Next i

    End With

    Selection.WholeStory
    '全選
    ''轉換文字形態成表格

    Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=myCol, _
    NumRows:=myRow, AutoFitBehavior:=wdAutoFitFixed
    ''最到第一頁,最前頭

    Selection.HomeKey Unit:=wdLine
    Selection.HomeKey Unit:=wdStory

    ''呼叫副程式調整所有圖片大小

    AllPictSize

    End Sub
    ''調整圖形大小
    Sub AllPictSize()

        Dim picWidth As Integer
        Dim picHeight As Integer
        Dim oIshp As InlineShape
        picHeight = InputBox("請輸入照片高度", "Resize Picture", 128)
        picWidth = InputBox("請輸入照片寬度", "Resize Picture", 120)
        For Each oIshp In ActiveDocument.InlineShapes
        With oIshp
        .Height = picHeight
        .Width = picWidth
        End With
        Next oIshp
        End Sub

    • Moved by Quist ZhangModerator Thursday, December 6, 2012 12:24 PM Word issue (From:Excel for Developers)
    Wednesday, December 5, 2012 2:26 PM

Answers

  • Sorry - forgot to remove a line from my example code that opens a workbook!

    Try this version

    Public Sub LoadPictureFSO2()

        Dim myRow As Integer
        Dim myCol As Integer
        Dim objFSO As Scripting.FileSystemObject
        Dim objFolder As Scripting.Folder
        Dim objFiles As Scripting.Files
        Dim objFile As Scripting.File


        myRow = InputBox("????? (Rows)", "Row Size", 2)
        myCol = InputBox("?????? (Cols)", "Col Size", 2)

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(ActiveDocument.Path)
        Set objFiles = objFolder.Files
        For Each objFile In objFiles
            If objFile.Name Like "*.jpg" Then
                Selection.InlineShapes.AddPicture Filename:= _
                        objFile.Name, LinkToFile:=False, SaveWithDocument:=True
                Selection.MoveRight Unit:=wdCharacter, Count:=1
                Selection.TypeText Text:=","

            End If
        Next objFile

        Selection.WholeStory
        '??
        ''?????????

        Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=myCol, _
                                 NumRows:=myRow, AutoFitBehavior:=wdAutoFitFixed
        ''?????,???

        Selection.HomeKey Unit:=wdLine
        Selection.HomeKey Unit:=wdStory

        ''?????????????

        AllPictSize

    End Sub


    Wednesday, December 5, 2012 3:27 PM
  • Hi Lu,

    Thank you for posting in the MSDN Forum.

    For the reason that Application.FileSearch not working in office2007, you can look into this page.

    To work around this issue, use the FileSystemObject object to recursively search directories and to find specific files. See the sample code that is provided in the following Microsoft Knowledge Base article:
    185601 How to recursively search directories by using filesystemobject

    I've read Bernie Deitrick's code, I think you'll be able to run the macro with a little bit adaptation.

    Selection.InlineShapes.AddPicture FileName:= _
                         objFolder + "\" + objFile.Name, LinkToFile:=False, SaveWithDocument:=True

    I've added objFolder + "\" + before the objFile.Name . Before you try the code, remember to add reference to MS Scripting Runtime. (In VBE, goto Tools->References  find Microsoft Scripting Runtime from the list.)

    Besides, since your thread is a Word related issue, I'm going to move it to Word for developers.

    Have a nice day.


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 6, 2012 12:23 PM
    Moderator

All replies

  • Add a reference to MS Scripting Runtime, and change your first macro to:

    Public Sub LoadPictureFSO()

        Dim myRow As Integer
        Dim myCol As Integer
        Dim objFSO As Scripting.FileSystemObject
        Dim objFolder As Scripting.Folder
        Dim colFiles As Scripting.Files
        Dim objFile As Scripting.File


        myRow = InputBox("????? (Rows)", "Row Size", 2)
        myCol = InputBox("?????? (Cols)", "Col Size", 2)

     

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(ActiveDocument.Path)
        Set colFiles = objFolder.Files
        For Each objFile In colFiles
            If objFile.Name Like "*.jpg" Then
                Selection.InlineShapes.AddPicture Filename:= _
                                                  objFile.Name, LinkToFile:=False, SaveWithDocument:=True
                Set wkbkTemp = Workbooks.Open(objFile.Name)
                Selection.MoveRight Unit:=wdCharacter, Count:=1
                Selection.TypeText Text:=","

            End If
        Next objFile

        Selection.WholeStory
        '??
        ''?????????

        Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=myCol, _
                                 NumRows:=myRow, AutoFitBehavior:=wdAutoFitFixed
        ''?????,???

        Selection.HomeKey Unit:=wdLine
        Selection.HomeKey Unit:=wdStory

        ''?????????????

        AllPictSize

    End Sub

    Wednesday, December 5, 2012 2:46 PM
  •  it is  can  not  run    
    Wednesday, December 5, 2012 3:01 PM
  • Sorry - forgot to remove a line from my example code that opens a workbook!

    Try this version

    Public Sub LoadPictureFSO2()

        Dim myRow As Integer
        Dim myCol As Integer
        Dim objFSO As Scripting.FileSystemObject
        Dim objFolder As Scripting.Folder
        Dim objFiles As Scripting.Files
        Dim objFile As Scripting.File


        myRow = InputBox("????? (Rows)", "Row Size", 2)
        myCol = InputBox("?????? (Cols)", "Col Size", 2)

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(ActiveDocument.Path)
        Set objFiles = objFolder.Files
        For Each objFile In objFiles
            If objFile.Name Like "*.jpg" Then
                Selection.InlineShapes.AddPicture Filename:= _
                        objFile.Name, LinkToFile:=False, SaveWithDocument:=True
                Selection.MoveRight Unit:=wdCharacter, Count:=1
                Selection.TypeText Text:=","

            End If
        Next objFile

        Selection.WholeStory
        '??
        ''?????????

        Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=myCol, _
                                 NumRows:=myRow, AutoFitBehavior:=wdAutoFitFixed
        ''?????,???

        Selection.HomeKey Unit:=wdLine
        Selection.HomeKey Unit:=wdStory

        ''?????????????

        AllPictSize

    End Sub


    Wednesday, December 5, 2012 3:27 PM
  • sorry, it is can not run

    can you help me,thanks

    if i save many picture(.jpg) in "C:\Users\Home\Desktop\ABC"

    and svae one word in this, it named A.docx

    how can i use  the macro

    Wednesday, December 5, 2012 3:46 PM
  • Sorry, but it worked perfectly for me - A.docx should be saved in the same folder and should be the active document when you run the macro.  A.docx should not have the macro code, because docx is not macro enabled - if you save A as a macro enabled document (A.docm), then that would work....
    Wednesday, December 5, 2012 4:21 PM
  • I saved  the   A.docx   in the same folder ("C:\Users\Home\Desktop\ABC")

    , but it can not run.

    i do not  know the macro, how to run it.

    so can you help me, please

    Thank you    

    Wednesday, December 5, 2012 4:30 PM
  • i saved .docx  to  .docm and .doc that it can not run,too 
    Wednesday, December 5, 2012 4:44 PM
  • Did you use the "Save As" and select the file type of "macro enabled document" rather than just typing the extension?

    Wednesday, December 5, 2012 5:08 PM
  • Hi Lu,

    Thank you for posting in the MSDN Forum.

    For the reason that Application.FileSearch not working in office2007, you can look into this page.

    To work around this issue, use the FileSystemObject object to recursively search directories and to find specific files. See the sample code that is provided in the following Microsoft Knowledge Base article:
    185601 How to recursively search directories by using filesystemobject

    I've read Bernie Deitrick's code, I think you'll be able to run the macro with a little bit adaptation.

    Selection.InlineShapes.AddPicture FileName:= _
                         objFolder + "\" + objFile.Name, LinkToFile:=False, SaveWithDocument:=True

    I've added objFolder + "\" + before the objFile.Name . Before you try the code, remember to add reference to MS Scripting Runtime. (In VBE, goto Tools->References  find Microsoft Scripting Runtime from the list.)

    Besides, since your thread is a Word related issue, I'm going to move it to Word for developers.

    Have a nice day.


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 6, 2012 12:23 PM
    Moderator