Could anyone help with the issue I'm having? I'm using VBA and and fairly new to it. What I want to have happen is for me to be able to open a new document and then insert a block into the new document. It will work the first time I run it but after opening the second document I get a runtime error when attempting to insert the block again. The end goal is to be able to do this many times via loops but you gotta start small... Any help would be appreciated!
Option Explicit Dim acadApp As AcadApplication Dim blk As AcadBlock Dim entRef As AcadBlockReference Dim aDoc As AcadDocument Dim point(0 To 2) As Double Dim tempFile As String Public Sub Main() tempFile = "C:\Users\Caden\AppData\Local\Autodesk\AutoCAD 2014\R19.1\enu\Template\acad.dwt" point(0) = 1 point(1) = 1 point(2) = 1 Set acadApp = ThisDrawing.Application 'Open new doc Set aDoc = acadApp.Documents.Add(tempFile) 'Save new doc aDoc.SaveAs ("E:\1") 'Insert block Set entRef = aDoc.ModelSpace.InsertBlock(point, "E:\PLC_Terminal.dwg", 1, 1, 1, 0) aDoc.Save 'Open new doc Set aDoc = acadApp.Documents.Add(tempFile) 'Save new doc aDoc.SaveAs ("E:\2") 'This is causing the run-time error 'Insert block Set entRef = aDoc.ModelSpace.InsertBlock(point, "E:\PLC_Terminal.dwg", 1, 1, 1, 0) End Sub
I don't have your whole file, so I can't guarantee this will work but I would try starting your aDoc as an array. When declaring, use these lines in place of the current one:
Dim aDoc() as AcadDocument Redim aDoc(1 to [total amount of documents you'll need]) as AcadDocument
you can redim to a different amount of documents at any time, but if you'd like to keep the old documents already loaded into your object variable (aDoc()), you'll want to use ReDim Preserve aDoc(1 to [total]) as AcadDocument
Here's a nice little tutorial on arraying, if you need it: http://patorjk.com/programming/tutorials/vbarrays.htm#typesofarrays
So when you get to opening them, declare an integer value. I used "i" in this example. Then you'll want to use something like this code:
For i = 1 to Ubound(aDoc) 'basically saying that it will run the code inside the For/Next
'once for each different aDoc you ReDim'ed for. Set aDoc(i) = acadApp.Documents.Add(tempFile) 'on first run, it will set aDoc(1), on the
'next aDoc(2), and so on until it hits the
'"Ubound" of the array aDoc(i).SaveAs ("E:\" & i) 'will save the first file as E:\1, then E:\2 and so on set entRef = aDoc(i).ModelSpace.InsertBlock(point, "E:\PLC_Terminal.dwg",1,1,1,0) aDoc(i).save Next
If you open your documents this way, each is assigned an instance of the array so that aDoc(i) isn't already filled with another document when it comes time to load one. I can't guarantee you this will work for your particular issue, but it seemed like you may have needed an intro to arrays and For/Next.
Here's the first For/Next loop I found in my code samples where I actually didn't even have to plan ahead on how many objects my array has. You can ReDim inside of loops, if need be.
For i = 0 To 26 If cdsheet.Cells(8 + (i * 2), 74) > 0 Then noelecct = noelecct + 1 ReDim Preserve elecstr(0 To i) As TElec elecstr(i).Quantity = cdsheet.Cells(8 + (i * 2), 74) elecstr(i).Description = cdsheet.Cells(8 + (i * 2), 77) End If Next
In this case, the loop runs a definite number of times (27). If it detects a value in a given Excel spreadsheet (cdsheet) cell (in row,column format), it will add another instance to the array then fill that instance with a value. That's good practice with arrays so that they aren't full of empty values.
Good luck, let me know if this was of 0 help to your problem haha, maybe I can take a closer look.