Ms-Excel export form worksheet to specific template text file Object variable or with block variable not set

BKChedlia

New Member
Joined
Jun 15, 2016
Messages
41
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 "]")
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"
}]
}
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What is AsObject? Must be "As Object" etc.
 
Upvote 0
Still running, it's so slow, and it begins with the second line instead of the 1rst low (after the header) and doesn't end in the last row, it continues even if rows are empty
 
Upvote 0
I tried : For Each a In wsDest.UsedRange.EntireRow but it begins with the second line and don't stop at the last line, so it's the same problem
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top