Author Topic: Sending Cell values to AutoCAD  (Read 16194 times)

0 Members and 1 Guest are viewing this topic.

57gmc

  • Bull Frog
  • Posts: 358
Re: Sending Cell values to AutoCAD
« Reply #15 on: May 27, 2021, 11:45:32 AM »
You don't have any code that selects a range to get a cell's Value. When programming, you have to tell the application how to do every little step. It's like a recipe. You can't skip any steps. In good programming practice, it's common to first draw a flowchart of all the steps you need to perform. Think it out first before you start coding.

Right now, I don't have time to teach you Excel coding or to write all the code for you. But here is a place you can start to educate yourself. The Concepts section gives you samples of how to do common things.

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Sending Cell values to AutoCAD
« Reply #16 on: May 27, 2021, 12:38:16 PM »
Sounds good. Let me dig into this and see what I can come up with. I will hopefully get this figured out! Thank though, for everything.
Civil3D 2020

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Sending Cell values to AutoCAD
« Reply #17 on: May 27, 2021, 05:24:01 PM »
I have added code to copy the cell value. I think I am lost when taking the cell value into the USERS1 variable.

Again, I hope I am heading the right direction on this. lol.

Code: [Select]
Option Explicit


Public Sub PasteCurrentCell()

    Dim sh As Excel.Worksheet
    Dim rng As Excel.Range
   
    Set sh = GetExcelSheet()
    If sh Is Nothing Then
        MsgBox "Excel is not running, or" & vbCrLf & _
        "opened Excel file does not have ""SHEET1""."
        Exit Sub
    End If
   
    Set rng = sh.Range("A1") '<<<<<---- How can I make this select the current active cell?? it could be in any column or row.
    rng.Copy
   
    InsertCurrentCellValue

End Sub

Private Function GetExcelSheet() As Excel.Worksheet

    Dim theSheet As Excel.Worksheet
    Dim sh As Excel.Worksheet
    Dim xls As Excel.Application
   
    On Error Resume Next
   
    Set xls = GetObject(, "Excel.Application")
    If Not xls Is Nothing Then
   
        For Each sh In xls.ActiveWorkbook.Worksheets
            If UCase(sh.Name) = "SHEET1" Then
                Set xls.ActiveSheet = sh
                Set theSheet = sh
                Exit For
            End If
        Next
       
    End If
   
    Set GetExcelSheet = theSheet
   
End Function

Private Sub InsertCurrentCellValue()
 On Error Resume Next
 Set AcadApp = GetObject(, "AutoCAD.Application")
 If Err Then
 Err.Clear
 Set AcadApp = CreateObject("AutoCAD.Application")
 End If
 AppActivate AcadApp.Caption
 AcadApp.Visible = True
 AcadApp.Application.WindowState = acNorm
 AcadApp.ActiveSpace = acModelSpace
 If AcadApp.Documents.Count = 0 Then
 AcadApp.Documents.Add
 End If
    ThisDrawing.SetVariable "USERS1", strcname
    AcadApp.ActiveDocument.SendCommand "zm2st" & vbCr
   
End Sub
Civil3D 2020

57gmc

  • Bull Frog
  • Posts: 358
Re: Sending Cell values to AutoCAD
« Reply #18 on: May 28, 2021, 10:37:54 AM »
This is where you need to learn more. You can't always just paste code samples and use them as they are. This code is meant to run inside AutoCAD's VBA, not Excel. Which is probably a better place to do this anyway. You need to read the code and understand what it's doing. Then you can use the parts that you actually need.
A quick internet search for "excel get active cell" turned this up as the first hit.

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Sending Cell values to AutoCAD
« Reply #19 on: June 07, 2021, 08:11:06 AM »
I wanted to give you an update. I was able to dig around this weekend and look up some of the things you mentioned. With that, I was able to figure out how to send a activecell value to autocads command line and simplify the code.

I did have one more question on the AutoCAD side of things. The lisp I am calling (C:z2s). I am having trouble "hardcoding" that in the following line. I know its not shown correctly below, but that is the intent I hoping for. lol.

Code: [Select]
    If Val(acadApp.Version) < 20 Then
        Else
             acadDoc.SendCommand '''z2S''' acadCmd & vbCr
        End If       


Code: [Select]
Sub Commands()
    Dim acadApp     As Object
    Dim acadDoc     As Object
    Dim acadCmd     As String
    Dim sht         As Worksheet
   
    Set sht = ThisWorkbook.Sheets("Sheet1")
   
    With sht
        .Activate
         Set Rng = ActiveCell
    End With
       
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0
   
    acadCmd = ""
        If Not IsEmpty(ActiveCell.Value) Then
             acadCmd = acadCmd & ActiveCell.Value & vbCr
        End If

    If Val(acadApp.Version) < 20 Then
        Else
             acadDoc.SendCommand acadCmd & vbCr
        End If         
End Sub

again thanks for your time!
Civil3D 2020

57gmc

  • Bull Frog
  • Posts: 358
Re: Sending Cell values to AutoCAD
« Reply #20 on: June 08, 2021, 06:26:51 PM »
You must have modified (z2s), because what you have posted before (z2s) didn't have any arguments. Assuming that it does, it should look like this.
Code - Visual Basic: [Select]
  1. acadDoc.SendCommand "(z2s " & acadCmd & ")" & vbcr

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Sending Cell values to AutoCAD
« Reply #21 on: June 10, 2021, 07:17:29 AM »
Ok. That make alot more sense then what I cam up with. lol.

Again, thank you for the help! I think that takes care of my problem!
Civil3D 2020