I want to export an excel worksheet in text file with a specific template, I wrote some code, but it doesn't work.
the line
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Set ts = fso.CreateTextFile("C:\Users\cben\Documents\BKC\FinancialSecurity\test7.txt", True, True)</code>Cause an error 91 : Object variable or with block variable not set
For line :
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;"> ts.WriteLine ("<?xml version=""1.0"" ?>")</code>When I put this line under for each it always loading create the file text7.text but never stop loading...
Here is my code. I've also included an image of the end text file (PS : for the last line of the file, the template is different; at the end, instead of the comma ",", we have "]")
{
"cases": [
{
"AimsID": "1234567",
"Amount": 1500.25,
"CurrencyISO": "CHF",
"Reason": 0,
"ExpiryDate": ""
},
{
"AimsID": "9876543",
"Amount": 120000.00,
"CurrencyISO": "EUR",
"Reason": 1,
"ExpiryDate": "2014-05-09"
}]
}
the line
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Set ts = fso.CreateTextFile("C:\Users\cben\Documents\BKC\FinancialSecurity\test7.txt", True, True)</code>Cause an error 91 : Object variable or with block variable not set
For line :
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;"> ts.WriteLine ("<?xml version=""1.0"" ?>")</code>When I put this line under for each it always loading create the file text7.text but never stop loading...
Here is my code. I've also included an image of the end text file (PS : for the last line of the file, the template is different; at the end, instead of the comma ",", we have "]")
Code:
[/FONT][/COLOR][COLOR=#303336][FONT=Consolas][/FONT][/COLOR][COLOR=#101094][FONT=Consolas]
Option[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] Explicit
[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] [/FONT][/COLOR][COLOR=#101094][FONT=Consolas]Sub[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] txtFile[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]()[/FONT][/COLOR]<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">[COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] strPath [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]String[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] fso [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Object[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] ts [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Object[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] wsDest [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Worksheet
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] wsDest [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] Sheets[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Filter FS"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
wsDest[/COLOR][COLOR=#303336].[/COLOR][COLOR=#101094]Select[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] fso [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] CreateObject[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"Scripting.FileSystemObject"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] cellAimsID [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Variant[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] cellAmount [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Variant[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] cellCurrencyISO [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Variant[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] cellReason [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Variant[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] cellExpiryDate [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Variant[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Dim[/COLOR][COLOR=#303336] a [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Range[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] b [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] Range[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] cell [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]String[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] a [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] Selection
[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] ts [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] fso[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]CreateTextFile[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"C:\Users\cben\Documents\BKC\FinancialSecurity\test7.txt"[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]True[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]True[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#858C93]' for each cell in the worksheet create a line in text fil[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]For[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Each[/COLOR][COLOR=#303336] a [/COLOR][COLOR=#101094]In[/COLOR][COLOR=#303336] wsDest[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]UsedRange[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Rows
cellAimsID [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Row[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
cellAmount [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Row[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]2[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
cellCurrencyISO [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Row[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]3[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
cellReason [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Row[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]4[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
cellExpiryDate [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Cells[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]a[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Row[/COLOR][COLOR=#303336],[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]5[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#858C93]'AimsID, Amount, Currency, Reason, ExpiryDate are the name of columns in worksheet[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"<?xml version=""1.0"" ?>"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"<WorkflowIntegrationDictionary xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"cases"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]": ["[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"{"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"AimsID:"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] cellAimsID [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"Amount:"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] cellAmount [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"CurrencyISO:"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] cellCurrencyISO [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"Reason:"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] cellReason [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"ExpiryDate:"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] cellExpiryDate [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]Chr[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]9[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]"}"[/COLOR][COLOR=#303336] [/COLOR][COLOR=#303336]&[/COLOR][COLOR=#303336] [/COLOR][COLOR=#7D2727]","[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WriteLine [/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]"</WorkflowIntegrationDictionary>"[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
ts[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Close
[/COLOR][COLOR=#101094]Next [/COLOR]</code>[COLOR=#101094]End[/COLOR][COLOR=#303336] [/COLOR][COLOR=#101094]Sub
[/COLOR][COLOR=#242729][FONT=Arial]
{
"cases": [
{
"AimsID": "1234567",
"Amount": 1500.25,
"CurrencyISO": "CHF",
"Reason": 0,
"ExpiryDate": ""
},
{
"AimsID": "9876543",
"Amount": 120000.00,
"CurrencyISO": "EUR",
"Reason": 1,
"ExpiryDate": "2014-05-09"
}]
}