Solved

Problem with file

Posted on 2015-02-04
4
86 Views
Last Modified: 2015-02-04
Hi,
when running these

Sub CreateNewFile()
    Dim objShtSrc As Excel.Worksheet
    Dim objShtDst As Excel.Worksheet
    
    Dim intCurRow As Integer
    
    VActiveWorkbookName = ActiveWorkbook.Name
    
    If VActiveWorkbookName <> "Test.xlsm" Then
        Exit Sub
    End If


'--------------
' Retrieve data
'--------------
    Worksheets("Details").Select
    Range("E5").Select
    Selection.QueryTable.Refresh BackgroundQuery:=False

    Worksheets("Report").Select

    myPath = ActiveWorkbook.Path
    
    sFileName = myPath + "\Test " + Worksheets("Details").Range("B1").Value + ".xlsm"
    Set objFSO = Nothing
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(sFileName) Then
        objFSO.DeleteFile (sFileName)
    End If
    ActiveWorkbook.SaveAs Filename:= _
        sFileName _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
    MsgBox ("File saved!")
      
    Application.SendKeys "{ESC}"

End Sub

Open in new window


to create one other .xlsm file, using Excel 2010. The newly created file cannot be opened, as it says "... file format or file extension is not valid ..". what to adjust to the codes?
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 30

Accepted Solution

by:
gowflow earned 500 total points
ID: 40588313
replace this line
    ActiveWorkbook.SaveAs  Filename:= _
        sFileName _
        , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False


By this line
    ActiveWorkbook.SaveAs  Filename:= _
        sFileName _
        ,  Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False

gowflow
0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40588466
If that doesn't work, can you run & break the code on this line :

Set objFSO = Nothing

Open in new window


..and report back the value of sFileName
0
 
LVL 11

Expert Comment

by:jkpieterse
ID: 40588487
Replace xlNormal with xlOpenXMLWorkbookMacroEnabled
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 40588534
Might not be the case but line 25 of the code where you are creating the variable "sFileName" you have plus signs which in theory is adding the various items together; as they are text that will cause an error.

Try changing the plus signs to &:

sFileName = myPath & "\Test " & Worksheets("Details").Range("B1").Value & ".xlsm"

Thanks
Rob H
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question