+ Reply to Thread
Results 1 to 4 of 4

VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code

Hybrid View

rain4u VBA for excel 2003. Open... 06-21-2011, 07:11 PM
MarvinP Re: VBA for excel 2003. Open... 06-21-2011, 09:54 PM
rain4u Re: VBA for excel 2003. Open... 06-22-2011, 02:17 PM
rain4u Re: VBA for excel 2003. Open... 06-25-2011, 12:12 AM
  1. #1
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code

    Hi folks
    At work we have excel 2003. Every time when I open some 2007 files with extensions .xlsm or xlsx for a split second it shows a little window and says its converting the file or something and it has cancel option. So we have Excel 2003 and I suppose the machine has the support package to convert the files.
    Now I need to have a macro that is capable to open up a 2007 file with extension xlsx and xlsm (also regular xls file as well please) and then carry on with the remaining code (concatenate some columns, index/match lookup from the file where the macro was launched).
    At the moment I have the following code which should allow me to open a xlsx and write in some text to header row:

    Option Explicit
     Sub testme()
    Dim Finfo As String
     Dim FilterIndex As Integer
     Dim Title As String
     Dim FileName As Variant
     ' Set up list of file filters
     Finfo = "Workbook (*.xls),*.xls," & "Office 2007 Excel Workbook (*.xlsx),*.xlsx,"
     ' Display *.* by default
     FilterIndex = 1
     ' Set the dialog box caption
     Title = "Select a File to Import"
     ' Get the filename
    FileName = Application.GetOpenFilename(MultiSelect:=True)
     ' Handle return info from dialog box
     If FileName = False Then
     MsgBox "No file was selected."
     Else
     'MsgBox "You selected " & FileName
     ' opens an the excel file at the location: FileName
     Workbooks.Open (FileName)
     End If
     
     
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "test"
        Range("B1").Select
        ActiveCell.FormulaR1C1 = "test"
        Range("C1").Select
        ActiveCell.FormulaR1C1 = "test"
     End Sub
    If I use this code on regular xls it works. But as soon as I try to use .xlsx it prompts me with error:

    "this file is not in recognizable format."

    If you know the file is is from another program which is incompatible with Microsoft Office Excel, click Cancel, then open this file in its original application.
    If you suspect the file is damaged, click Help for more information about solving the problem.
    If you still want to see what text is contained in the file, click OK. Then click Finish in the Text Import Wizard.

    When clicking OK it does what it says. Its all text and its all gibberish.


    To make things even more harder the file that I would try to open will always have a dynamic name so it can be different name every single occasion. I guess I could change it to something specific if there is no way around it. Some of the files may prompt that it has macros so do you want to enable the macros (Macro security is on medium setting).

    Is there any solution to my problem? I have tried bunch of different stuff and also Googled about the subject with no luck.

    Any help would be immensely appreciated.
    Cheers
    Rain

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,372

    Re: VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with cod

    Hi rain4u,

    Is the answer SP2?

    http://www.techrepublic.com/forum/questions/101-322285
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with cod

    Hi Marvin
    I wish I could try something easy as this. But the way our computers are locked out to install anything at all on our computers, I think there will be a bigger chance that I will become the next Loto millionaire than our IT department willing to actually try to improve the software on our desktops. Beside from the same link someone mentioned he had the XP3 installed and he still had the problem. The article claims this will help with a read only issue so I think it might not necessarily resolve the issue ti begin with.

    Any other ideas?


    Cheers
    Rain


    EDIT:
    I approached this problem from adifferent angle and Googled again. And the following caught my eye

    Taken from another forums on another help site


    *Moc.exe is Open XML format converter provided after Office 2007 Compatibility Pack installed.

    The Office 2007 Compatibility Pack will do the conversion job 1st before proceed to open the converted file (in temp folder). If the xlsx file is being open via [Excel 2003 > File > Open], it will be the same issue as via programmatically.
     Sub testme()
    
    Call Shell("C:\Program Files\Microsoft Office\Office12\Moc.exe " & "X:\Book1.xlsx")
     End Sub
    This actually works in the sense of getting the file open without problem. I tried it and it did open xlsx file on my USB memory stick. But how we could use this to open file via open dialog box so I could choose which file I would like to open.

    I think this could potentially help to resolve the problem and would help anyone in future if one has the same issue .

    Cheers
    Rain
    Last edited by rain4u; 06-22-2011 at 07:51 PM.

  4. #4
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with cod

    I found the following code from the Internet (see below). I have no clue if this could be used together with GetOpenFilename. I wonder if it would be possible to use GetOpenFilename to find the xlsx you want to open, and somehow define files name and full path as FullPathAndFileName and use this code to ope the file.

    Maybe someone smarter person could look into this. So far I haven't found anything on the Internet that would allow to use getopenfilename and open xlsx files. If some one can crack this tough nut, I bet there would at least few people who might like this macro.



    ' Headers/declarations
     
     Option Explicit
     Dim ExcelApp As Object
     Dim ExcelWorkbook As Object
     
     Private Declare Function OpenProcess Lib "kernel32" _
     (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
     ByVal dwProcessId As Long) As Long
     
     Private Declare Function GetExitCodeProcess Lib "kernel32" _
     (ByVal hProcess As Long, lpExitCode As Long) As Long
     
     Private Const STATUS_PENDING = &H103&
     Private Const PROCESS_QUERY_INFORMATION = &H400
     
     '
     
     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     '
     ' Process entire Excel file and extract plots from charts and worksheets
     '
     ' This can be done from a machine running VB with Excel 2003
     ' that will read Excel 2007 files (.xlsx).
     '
     ' It uses the Microsoft Moc.exe utility (which is installed when the MS file
     ' converter is installed) to convert and open up the .xlsx *without* the
     ' pesky popup.
     '
     ' Use at your own risk.
     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
     Sub ProcessExcelFile(sFullPathAndFileName As String)
     
     On Error GoTo ehandler:
     
     Dim ExcelApp As Object
     Dim ExcelWorkbook As Object
     Dim ICnt As Integer
     Dim Start As Single
     Dim i As Integer
     Dim j As Integer
     Dim sType As String
     
     Dim Worksheet_Count As Integer
     
     ' PNG, GIF, JPG
     sType = "PNG"
     
     Start = Timer
     
     Set ExcelApp = CreateObject("Excel.Application")
     
     ' Note in using Excel 2003 files:
     ' To open workbook *directly* (if you knew the file was an Excel 2003 format)
     ' instead of using the ShellAndWait
     '
     ' Set ExcelWorkbook = ExcelApp.Workbooks.Open(sFullPathAndFileName)
     '
     ' You would convert things like ExcelApp.Activeworkbook.ActiveSheet.Name to
     ' ExcelWorkbook.ActiveSheet.Name
     '
     ' You can't seem to use the ShellandWait method, because Excel will open the file, but ShellAndWait will
     ' only continue *after* the Excel application is shut down!
     '
     
     
     ' this opens a workbook (Excel 2007)
     Call ShellandWait("C:\Program Files\Microsoft Office\Office12\Moc.exe " & sFullPathAndFileName)
     
     
     Debug.Print ExcelApp.ActiveWorkbook.Name
     
     ExcelApp.Visible = False
     
     
        ' Begin the loop through all CHARTS in the workbook
        For i = 1 To ExcelApp.ActiveWorkbook.Charts.Count
        
           ' Select the chart
           ExcelApp.ActiveWorkbook.Charts(i).Select
           
     '     Debug.Print "--->" & ExcelApp.ActiveWorkbook.ActiveSheet.Name
     '     Debug.Print "--->" & ExcelApp.ActiveWorkbook.ActiveSheet.ChartObjects.Count
           
            ' single chart
            ' Extract the *entire* chart as a single JPG
            ExcelApp.ActiveWorkbook.ActiveChart.Export FileName:=CurDir & "\output\" & _
                ExcelApp.ActiveWorkbook.ActiveChart.Name & "." & sType, FilterName:=sType
           
           ' Extract individual charts, if they exist
           For j = 1 To ExcelApp.ActiveWorkbook.ActiveSheet.ChartObjects.Count
           
           ExcelApp.ActiveWorkbook.ActiveSheet.ChartObjects(j).Chart.Export FileName:=CurDir & "\output\" & _
                ExcelApp.ActiveWorkbook.ActiveChart.Name & _
               "_plot" & Format(j, "000") & "." & sType, FilterName:=sType
           
           Next j
           
        Next i
        
        ' Begin the loop through all WORKSHEETS in the workbook
        For i = 1 To ExcelApp.ActiveWorkbook.Worksheets.Count
        
           ' Select the chart
           ExcelApp.ActiveWorkbook.Worksheets(i).Select
           
     '     Debug.Print "--->" & ExcelApp.ActiveWorkbook.ActiveSheet.Name
     '     Debug.Print "--->" & ExcelApp.ActiveWorkbook.ActiveSheet.ChartObjects.Count
        
           ' Extract individual charts, if they exist
           For j = 1 To ExcelApp.ActiveSheet.ChartObjects.Count
           
           ExcelApp.ActiveWorkbook.ActiveSheet.ChartObjects(j).Chart.Export FileName:=CurDir & "\output\" & _
                ExcelApp.ActiveWorkbook.ActiveSheet.Name & _
               "_plot" & Format(j, "000") & "." & sType, FilterName:=sType
           
           Next j
           
        Next i
             
     
     ExcelApp.ActiveWorkbook.Close SaveChanges:=False
     
     ExcelApp.Quit
     
     MsgBox "Done; it took " & Timer - Start & " seconds."
     
     Exit Sub
     
    ehandler:
     
     MsgBox "Error = " & Error
     
     End Sub
     
     
     
     
     Private Sub cmdProcessExcel_Click()
     
     Call ProcessExcelFile("c:\test\test_03.xlsx")
     
     
     End Sub
     
     Public Function ShellandWait(ExeFullPath As String, _
     Optional TimeOutValue As Long = 0) As Boolean
        
        Dim lInst As Long
        Dim lStart As Long
        Dim lTimeToQuit As Long
        Dim sExeName As String
        Dim lProcessId As Long
        Dim lExitCode As Long
        Dim bPastMidnight As Boolean
        
        On Error GoTo ErrorHandler
     
        lStart = CLng(Timer)
        sExeName = ExeFullPath
     
        'Deal with timeout being reset at Midnight
        If TimeOutValue > 0 Then
            If lStart + TimeOutValue < 86400 Then
                lTimeToQuit = lStart + TimeOutValue
            Else
                lTimeToQuit = (lStart - 86400) + TimeOutValue
                bPastMidnight = True
            End If
        End If
     
        lInst = Shell(sExeName, vbMinimizedNoFocus)
        
     lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
     
        Do
            Call GetExitCodeProcess(lProcessId, lExitCode)
            DoEvents
            If TimeOutValue And Timer > lTimeToQuit Then
                If bPastMidnight Then
                     If Timer < lStart Then Exit Do
                Else
                     Exit Do
                End If
        End If
        Loop While lExitCode = STATUS_PENDING
        
        ShellandWait = True
        
    ErrorHandler:
     ShellandWait = False
     Exit Function
     End Function
    Any help would be greatly appreciated.
    Cheers
    Rain

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1