Macro to generate a c++ header file using excel - vba

I am thinking to make a excel file which can generate a headre file for my c++ source file.
Previoulsy we used to generate .h files using excel but i dont know the logic behind that(Hope some macros using for that).
My header file contains this many data and my intention is to give "MYapp Alpha 0.0.3" through excel file because the version number changes for each release. If I used excel file then I can edit that excel and it creates .h file for me, later some more informations I can make configurable through excel file.
Is it possible to write macro that edit "MYapp Alpha 0.0.3" without touching other
#define APP_FLASH_APP_ID 0x123
#define APP_VERSION_NUM "MYapp Alpha 0.0.3 "
#define APP_PRODUCT_NAME "TPI "
#define APP_DESCRIPTION_STR APP_PRODUCT_NAME APP_VERSION_NUM
#define APP_RELEASE_DATE_STR "10/11/13"
#define APP_SOFTWARE_PARTNUM_LEN 10
Some valuable help or suggestions needed
Have a great day

I think this could help you:
Sub test()
Const strVerNumber As String = "0.0.4"
Dim FS, TSsource
Set FS = CreateObject("Scripting.FileSystemObject")
Set TSsource = FS.OpenTextFile("C:\test.txt", 1, 2)
Dim tmpString As String
tmpString = TSsource.ReadAll
TSsource.Close
tmpString = Replace(tmpString, _
"MYapp Alpha 0.0.3", _
"MYapp Alpha " & strVerNumber)
Dim TSout
Set TSout = FS.Createtextfile("C:\testOUT.txt", True)
TSout.Write tmpString
TSout.Close
End Sub
See these methods: OpenTextFile, CreateTextFile, ReadAll and Write

Are you talking about something like this:
Formulas:
Results:

Well basically a .h file is a text file. So you need to do some string processing, open a text file, print the data in it and close it.
To open and close a text file use this:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.CreateTextFile("D:\test.h")
oFile.WriteLine "tfadfadsest"
oFile.Close
Set fso = Nothing
Set oFile = Nothing

Related

Writeline in VBA breaks with too long text

I am automating a web page extraction and writting the contents to a text (HTML) file.
For that I set up a File System Object like this
Dim myHTMLfilepath As String
myHTMLfilepath = "C:\temp\MyFile.html"
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim myHTMLFile As Object
Set myHTMLFile = fso.createtextfile(myHTMLfilepath)
When I try to write the extracted content to the file sometimes I get an error 5 (invalid parameter). Here is th code:
myHTMLFile.writeline objIE.document.getElementsByClassName("cool-box")(0).innerHTML
It breaks when the length of the innerHTML is somewhere between 25800 and 28000 (I haven't yet figured the exact limit).
Does anyone know if the WriteLine limit can be increased or advise on a different way to do this?
Assuming the .innerHTML can successfully be read into a string (split up reading/writing to find out), you should be able to use an ADODB.Stream to write it to the file. WriteLine is intended to write a single line of text to a file, not a whole entire document.
Dim contents As String
contents = objIE.document.getElementsByClassName("cool-box")(0).innerHTML
With CreateObject("ADODB.Stream")
.Open
.Type = 1
.Write contents
.SaveToFile myHTMLfilepath, 2
.Close
End With

Is there an argument for copyfile that will change the hidden property?

I have a database that writes data into a copied excel template. The template is hidden to keep the end user from tampering with it, however the final result is also hidden. Is there a way to change the hidden property when saving the new file?
Currently, the db copies the template and renames it.
fso.CopyFile "C:\Upload\Rebate_Upload_Files\Standard Form (Template)
protected.xlsx", "C:\Upload\Rebate_Upload_Files\Rebate Contract " &
Contract_Number & " " & Date$ & ".xlsx"
After that, it transfers the appropriate table and saves the file.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml,
"export_table", "C:\Upload\Rebate_Upload_Files\Rebate Contract " &
Contract_Number & " " & Date$ & ".xlsx", False, "A12:L65000"
The process works fine, except that the final file is also hidden and I'd like it to be a normal file.
Thanks
Not for CopyFile-which is a FileSystemObject method, but there is one for a File object. We'll just update it after the copy is complete.
For simplicity i've replaced your file output path to a string variable.
originalFileName = "yourStartingFile"
copyFileName = "yourCopiedFile"
set fso.CopyFile OriginalFileName, CopyFileName
--after copying, get file that was copied
--set attributes value of file to 0. 0 = Normal, 2 = Hidden
f = fso.GetFile(copyFileName)
f.attributes = 0
More reading for additional details.
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/file-object
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/attributes-property
Mike's code above worked, however there was a few more steps involved for me so I wanted to provide the full code for anyone stumbling upon this.
First, in order to use FileSystemObject, you need to enable it in your VBA Editor by going to Tools->References and enabling "Microsoft Scripting Runtime". Then, using the code below, you can copy a hidden file and set the new file (via f.Attributes) to not be hidden:
' SourceFile is the full path name to my original file
' FileNAme is the full path name to my new file
Dim fso As Scripting.FileSystemObject
Dim f As File
Set fso = New Scripting.FileSystemObject
Call fso.CopyFile(SourceFile, FileName, False) ' Set to true to overwrite
Set f = fso.GetFile(FileName)
f.Attributes = 0

Convert multiple .xls files to .xlsx in ssis

I have a folder that receives multiple excel files in .xls format. I need to change the format type to .xlsx in order to load the excel data into SQLvia SSIS. I know how to rename the file using "File System Task" but that works for a specific file. but my file contains a file # and date as well that needs to stay same as source file, I only want the file type to change and the file move to a processed folder. Can anyone help me?
Source Path: C:\Documents\TestFolder
Source File: TestSegRpt_0001_2017_02_22.xls
Destination Path: C:\Documents\TestFolderProcessed
Destination File: TestSegRpt_0001_2017_02_22.xlsx
Hoping i understood your problem correctly.
I think below link will help.
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_other/batch-convert-xls-to-xlsx/1d9b3d78-daf0-4014-8fb2-930aca6493b0
You have to add a Script Task, loop over files, and use a function like the following to create precessed directory and convert files (code in Vb.net):
Public Sub ConvertXlsToXlsx(ByVal strpath as string)
Dim strDirectory as string = System.IO.Path.GetDirectoryName(strpath) & "Processed"
If Not System.IO.Directory.Exists(strDirectory) Then System.IO.Directory.CreateDirectory(strDirectory)
Dim xl As New Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
xlWorkBook = xl.Workbooks.Open(strpath)
xlBook.SaveAs(strDirectory & "\" & System.IO.Path.GetFilename(strpath) & "x")
xl.Application.Workbooks.Close()
xl.Application.Quit()
End Sub
Your code will look like:
Public Sub Main
Dim strFolder as string = Dts.Variables.Item("FolderPath").Value
Dim strXlsFiles() as string = IO.Directory.GetFiles(strFolder,"*.xlsx",SearchOption.TopDirectoryOnly)
For each strFile as String in strXlsFiles
If strFile.EndsWith("xlsx") The Continue For
ConvertXlsToXlsx(strFile)
Next
End Sub
Reference:
https://social.msdn.microsoft.com/Forums/office/en-US/a73f846c-91ee-4dad-bd7b-c04d418d0561/convert-xls-into-xlsx?forum=exceldev

tool or vbscript to add hardcoded text to top of multiple files?

Does anyone know a tool that can add predefined text such as the following 4 lines:
"My disclaimer:
For legal reasons -
1. Don't blah...
2. You must..."
to all .txt files in a directory: "C:/test/"
I have seen append functions but they only add to the bottom, not sure of a method for adding to the top of the file.
My OS is Win XP.
SrcHead is a free utility that can adjust the header of multiple sourcefiles. It is written for Windows and needs the .NET Framework 2.0 to work.
Haven't tested this, but it should put you on the right track.
Dim FSO, txs, fld, fil, content
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fld = FSO.GetFolder("C:\test\")
For Each fil In fld.Files
If Right(fil.Name, 3) = "txt" Then
Set txs = fil.OpenAsTextStream(1) ' 1 = for reading
content = txs.ReadAll
txs.Close
Set txs = fil.OpenAsTextStream(2) ' 2 = for writing
txs.Write "MyDisclaimer:" & vbCrLf & "stuff" & content
txs.Close
End If
Next

Visual Basic label

If i have information (for example a name) in a label on a form in Visual Basic, how do I save this information in a .txt file?
Thanks
You can use the classes in the System.IO namespace. Look at File and its methods.
This example uses one overload of WriteAllText:
File.WriteAllText("Path To Text File.txt", myLabel.Text)
It will write the text value of the myLabel control to the specifies text file.
You use StreamWriter to do that. Here's an example:
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\test.txt", True)
file.WriteLine("Here is the first string.")
file.Close()
If you want to know how to read from txt files, here's a example code:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(fileReader)
You can use file system object for ealier versions of Visual basic.
' VBScript
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine(label.caption)
MyFile.Close
http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx
or
Sub Create_File()
Dim fso, txtfile
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("c:\testfile.txt", True)
txtfile.Write (lable.caption) ' Write a line.
' Write a line with a newline character.
txtfile.WriteLine("Testing 1, 2, 3.")
' Write three newline characters to the file.
txtfile.WriteBlankLines(3)
txtfile.Close
End Sub
http://msdn.microsoft.com/en-us/library/aa263346(VS.60).aspx
Put it directly into the place you needed
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("filename.txt", True)
file.WriteLine("Your Text Here~")
file.Close()