The solution provided here by Transistor1 works perfect except for the below issue I am facing.
The output file is including quotes (") at the beginning and ending of the HTML code and its also adding an extra quote if the quote is already present.
For Example, This Code: <div style="background-color:rgba(0,0,0,0.2);padding:60px;">
Becomes Like This: <div style=""background-color:rgba(0,0,0,0.2);padding:60px;"">
I don't want any extra quotes to get added, just want the text as it is.
Please help me resolve this.
It must be the Write method in FileSystemObject that's doing it. VBA has built in file writing ability, so I'm not sure I understand the benefit of using FSO. Here's how I would do it in VBA and it handles quotes as expected.
Public Sub ExportFile()
Dim sFile As String, lFile As Long
Dim rCell As Range
Dim sFldr As String
sFldr = Environ$("userprofile") & "\My Documents\"
For Each rCell In Sheet1.UsedRange.Columns(1).Cells
sFile = sFldr & rCell.Value & ".html"
lFile = FreeFile
Open sFile For Output As lFile
Print #lFile, rCell.Offset(0, 1).Value
Close lFile
Next rCell
End Sub
To convert your Excel data to HTML, perform the following steps. These instructions apply to all "ribboned" versions of Excel 2016, 2013, 2010 and 2007:
On the workbook, go to the File tab and click Save As.
If you want to export some portion of data only, e.g. a range of cells, pivot table or graph, select it first.
In the Save As dialog, choose one of the following:
Web Page (.htm; .html). This will save your workbook or the selection to a web page and create a supporting folder that will store all of the page's supporting files such as images, bullets and background textures.
Single File Web Page (.mht; .mhl). This will save your workbook or the selection to a single file with supporting files embedded into the web page.
Related
I have a word document which I am using as a template for other projects. The file is saved as an .docx and includes a Macro to save the document with a file name dependent on content control box titled "STATE". I would prefer the macro save the document without the macro included. When I run the macro, the document saves to the correct file location however when opened I get an error that reads:
We're Sorry. We can't open FILE LOCATION because we found a problem with its contents. No error details available.
I assume there is an error somewhere in my code. I am using Word 2013. Any help would be appreciated!
Sub Silent_save_to_DOC()
Dim strText As String, strDate As String, strDrop As String
strText = ActiveDocument.SelectContentControlsByTitle("STATE")(1).Range.Text
Dim strFilename As String
strFilename = "C:\Users\Tom\Desktop\Quote - " & strText & ".docx"
ThisDocument.SaveAs strFilename
End Sub
The problem is that a *.docx file may not contain macros, only *.docm files may contain macros. Your code is forcing Word to save the document with macros and an incompatible file extension. As a security measure Word will not open a *.docx file that contains macros - as the message is informing you.
Use a true template file - *.dotm - to create your documents. The template can contain the macros, which will not be copied to the document.
If you want to unlink the template (with the macros) from the document you can attach the Normal template to the document (ActiveDocument.AttachedTemplate = NormalTemplate).
When you use SaveAs be sure to also specify the file format using the FileFormat parameter, not just the file name. For a docx file that would be ^wdFormatXMLDocument`.
Do not use ThisDocument.SaveAs as ThisDocument refers to the document containing the macro. Instead, use ActiveDocument if you cannot otherwise use a specific Document object.
It's also helpful if you specify the file format should there be any possibility that the save format isn't the same as that of the file that was opened. Hence:
Dim strText As String, strFilename As String
With ActiveDocument
strText = .SelectContentControlsByTitle("STATE")(1).Range.Text
strFilename = "C:\Users\Tom\Desktop\Quote - " & strText & ".docx"
.SaveAs2 FileName:=strFilename, FileFormat:=wdFormatXMLDocument
End With
I have some files in a folder that seems to be formatted in a strange way when accessing them in VBA.
The code below is a simple test. It scans the Excel-files in a folder and simply opens and closing them again (just to test the problem). It works fine even if the filename contains Swedish characters such as å,ä,ö.
Sub ScanFiles()
Dim ExcelFile As String
Dim WB As Workbook
ExcelFile = Dir("c:\Somepath\*.xlsx")
Do While ExcelFile <> ""
Set WB = Workbooks.Open(ExcelFile)
WB.Close False
Loop
ExcelFile = Dir()
End Sub
This works fine... until... this happens:
One filename is listed as "Sömething.xlsx". Even if I take a detailed look in the file properties. It is really "Sömething.xlsx"!
But the variable ExcelFile stores "So¨mething.xlsx". The Workbooks.Open command fails since the file with that name can't be found
It seems that the specific file has been saved in an Apple environment.
Why is this happening and how do I manage it?
I have a excel macro routine that I need to prep a file for another routine which is run daily. This prepping involves changes the links of t-1 file to a t0 file.
The code I usually do is:
ActiveWorkbook.ChangeLink Name:= _
"file path", NewName:=new_file_path, Type:=xlExcelLinks
My trouble now is that for this particular routine the file path to be changed to a new routine is not always the same, thus I would need a way to automatize finding out what are the current links to replace them all. The new file path I now because it is the worksheet that is calling this routine and opening this file, so first thing I do Is
new_file_path = "C:\...."& ActiveWorkbook.Name & ".xlsm"
What would help me is if there is a trick to replace all links for a new one, without the need to say the name/path of the old links. Does any one know?
Thanks
To change all the excel links in a workbook try this procedure:
Sub WbThs_ChangeLink_Excel()
Dim wbTrg As Workbook
Dim sLinkNew As String
Dim aLinks As Variant, vLink As Variant
sLinkNew = "##VBA Links Replace - Target 3.xlsb" 'Change as required
Set wbTrg = ThisWorkbook 'Change as required
Rem Set array with all excel links
aLinks = ActiveWorkbook.LinkSources(xlExcelLinks)
Rem Replace each excel Link
If Not IsEmpty(aLinks) Then
For Each vLink In aLinks
wbTrg.ChangeLink Name:=vLink, NewName:=sLinkNew, Type:=xlExcelLinks
Next: End If
End Sub
See the following pages for additional information on the resources used:
Workbook.ChangeLink Method (Excel)
Workbook.LinkSources Method (Excel)
XlLink Enumeration (Excel)
I have a macro setup to automatically open/save a file that I am opening from the web. The web format is a #csv.gz format. I have code that currently just saves the file in the default location (which I have changed to c:\files). I want to write a macro that will keep the filename of the file, but change the extention to just file.xlsm. Is there a way to do this with VBA/excel? The reason while I need to change the extension is because it currently does not work with my formulas. The default save code I have just saves the file as a #csv.txt.
Is this possible?
Integrate this code into your own:
Sub SaveIt()
Dim wkb As Workbook
Set wkb = ActiveWorkbook 'change to your workbook reference
wkb.SaveAs Replace(wkb.Name, ".txt", ""), 52 'change ".txt" to ".csv" if need be
End Sub
See Excel File Type Enum for more information the 52.
I need help with a coding requirement that I've not previously experienced. I just browsed a similar issue raised here a couple of years ago - VBA to Copy files using complete path and file names listed in Excel Object.
My issue is similar but somewhat simpler than the OP.
I have a number of folders that each contain about 100 small .csv files; for each folder I need to copy the path for each file to an open worksheet. Each folder of .csv files has its own associated workbook.
As one example, the open workbook is F:\SM\M400AD.xlsm and the active worksheet is CSV_List. The folder containing the .csv files is F:\SM\M400AD.
Doing it manually, my sequence is then:
Open folder F:\SM\M400AD
Select all
Copy path
Paste to Range("B11") of worksheet CSV_List
When I do it manually, as described above, I get a list that looks like:
"F:\SM\M400AD\AC1.csv"
"F:\SM\M400AD\AC2.csv"
"F:\SM\M400AD\AE.csv"
"F:\SM\M400AD\AF.csv"
"F:\SM\M400AD\AG.csv"
"F:\SM\M400AD\AH1.csv"
"F:\SM\M400AD\AH2.csv"
"F:\SM\M400AD\AJ.csv"
and on down the page until I have a list of 100 paths. This single column list is then pasted into worksheet CSV_List, starting at Range("B11").
I need to automate this and would be grateful if a VBA guru could kindly code this for me.
Such of question has been asked before, for example:
Loop through files in a folder using VBA?
List files in folder and subfolder with path to .txt file
The difference is you want to "automate" it, which means you want to execute code on workbook Open event.
How to achieve that?
Open F:\SM\M400AD.xlsm file.
Go to Code pane (ALT+F11)
Insert new module and copy below code
Option Explicit
Sub EnumCsVFilesInCurrentFolder()
Dim sPath As String, sFileName As String
Dim i As Integer
sPath = ThisWorkbook.Path & "\"
i = 11
Do
If Len(sFileName) = 0 Then GoTo SkipNext
If LCase(Right(sFileName, 4)) = ".csv" Then
'replcae 1 with proper sheet name!
ThisWorkbook.Worksheets(1).Range("B" & i) = sPath & sFileName
i = i + 1
End If
SkipNext:
sFileName = Dir(sPath)
Loop While sFileName <> ""
End Sub
Now, go to ThisWorkbook module and insert below procedure:
Private Sub Workbook_Open()
EnumCsVFilesInCurrentFolder
End Sub
Save and close workbook
The workbook is ready to use. Whenever you open it, EnumCsVFilesInCurrentFolder macro will be executed.
Note: you have to change above code to restrict the number of records.