Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Bailey Evans   on Mar 09 In MS Office Category.

  
Question Answered By: Khadeeja Malik   on Mar 09

I worship C++ programers, and consider VB (me)
programmers an inferior bunch. Nonetheless, maybe the
following VB / VBA code will get you started. It is
intended as a generic sketch to cover various issues you
may encounter.

01.. fpath = "c:\autocad\drawing22.dwg"
02.. targt = "c:\temp\text.txt"
03.. Close #1
04.. Close #2
05.. Open fpath For Input As #1
06.. Open targt For Output As #2
07.. For jj = 1 to 100
08..... Line Input #1, txt
09..... extr = Left(txt, 4)
10..... If extr = "ROOM" Then
11........ Write #2, txt
12..... End If
13.. Next jj
14.. Close #1
15.. Close #2

OK, what have I done here??

Line 1 sets the source file name (with full path
specified). In the example, it is hardwired. You could
certainly set them from an array inside of a higher level
loop that populated the array with all file names of
interest.

Line 2 sets the target file name. Here, I am using
the simplifying approach of just writing to a text
file. You could enhance this by writing directly to
Excel.

Lines 3 and 4 merely close file pointers (good practice,
as during debugging, if the file is still open, you'll
get an error).

Lines 5 and 6 open the respective files  and associate
a file pointer to each.

Line 7 is a simple For .. Next construct to get you
through the source file, line by line. Many enhancements
possible here -- I'm just trying to sketch the general flow.
But, yes, as written, the loop would only interate 100
times.

Line 8 reads the a line of the source file (up to CrLF -
carriage return + line feed), and puts the results in
the variable txt. Random length lines should be handled
by this approach, assuming there is always a CrLF at the
end of each line).

Line 9 extracts the left-most 4 characters, and assigns
them to the variable extr. This is in anticipation of
you needing to skip over a block of lines in the source
file until some particular point of interest is reached.
Natch, modify to meet your needs.

Line 10 tests to see of the desired block has been
reached.

Line 11 is where the extracted line of text is written
to your target file (and/or Excel).

The rest should be pretty self explanatory. A minor
disclaimer --- the foregoing code is VB. I'm pretty sure
the same statements are available in VBA.

Let me know if you need me to expand on any of the
foregoing.

Share: 

 

This Question has 28 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on AutoCAD Table Data Extraction Or get search suggestion and latest updates.


Tagged: