Results 1 to 7 of 7

Thread: at a loss: Setting Field Values with VBA

  1. #1
    Member
    Join Date
    2015-12
    Location
    Santa Clarita, CA
    Posts
    8
    Login to Give a bone
    0

    Default at a loss: Setting Field Values with VBA

    I am using Autocad 2016 and excel 2016 I am trying to set my field values based on excel cell values.

    The problem is i dont know how to call or reference an autocad field with vba. I have searched everywhere. Disregarding the excel part can someone tell me how to reference my fields in autocad with VBA? They are already created and I just want to set their values with VBA.

    Thanks in advance

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: at a loss: Setting Field Values with VBA

    Welcome to AUGI.

    Have you tried invoking the FieldCode() Method?



    Perhaps Ed, or Opie can offer something more complete... I pretty much skipped VBA, jumping from Visual LISP (also ActiveX COM) to .NET API.


    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Member
    Join Date
    2015-12
    Location
    Santa Clarita, CA
    Posts
    8
    Login to Give a bone
    0

    Default Re: at a loss: Setting Field Values with VBA

    i have been playing with the fieldcode() method but cant get it to work properly it returns a run-time error '91'

    Code:
    Sub popFields()
    On Error Resume Next
    '*2016
    Set acad = GetObject(, "Autocad.Application.20.1")
       If Err <> 0 Then
           Err.Clear
           Set acad = CreateObject("AutoCAD.Application.20.1")
      End If
    '*BricsCad
    'Set acad = GetObject(, "BricscadApp.AcadApplication")
    Dim textObj As AcadMText, text As String, Pt(0 To 2) As Double, height As Double
    On Error GoTo 0
    text = "%<\AcVar JOBNAME \f ""%tc1""%>%"
    Pt(0) = 2: Pt(1) = 2: Pt(2) = 0
    height = 0.5
    Set textObj = acad.ActiveDocument.ModelSpace.AddText(text, Pt, height)
    ZoomAll
    End Sub
    Last edited by BlackBox; 2016-11-09 at 09:21 PM. Reason: Please use [CODE] Tags

  4. #4
    Member
    Join Date
    2015-12
    Location
    Santa Clarita, CA
    Posts
    8
    Login to Give a bone
    0

    Default Re: at a loss: Setting Field Values with VBA

    Quote Originally Posted by BlackBox View Post
    Welcome to AUGI.

    Have you tried invoking the FieldCode() Method?



    Perhaps Ed, or Opie can offer something more complete... I pretty much skipped VBA, jumping from Visual LISP (also ActiveX COM) to .NET API.


    Cheers
    i can add the date as shown in the example code:
    Sub Example_FieldCode()
    ' This example creates a text object in model space.
    ' It then returns the field code string for the object.

    Dim textObj As AcadText
    Dim text As String
    Dim insertionPoint(0 To 2) As Double
    Dim height As Double

    ' Define the text object
    text = "%<\AcVar Date \f ""M/d/yyyy""%>%"
    insertionPoint(0) = 2: insertionPoint(1) = 2: insertionPoint(2) = 0
    height = 0.5

    ' Create the text object in model space
    Set textObj = ThisDrawing.ModelSpace.AddText(text, insertionPoint, height)
    ZoomAll

    ' Return the current text string for the object
    text = textObj.FieldCode
    MsgBox "The FieldCode for the text object equals: " & text, vbInformation, "FieldCode Example"

    End Sub
    but i cant edit an already existing field.

  5. #5
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: at a loss: Setting Field Values with VBA

    Quote Originally Posted by janice703088 View Post
    but i cant edit an already existing field.
    Sure you can; just parse and/or subst the necessary components of the desired Field type.


    As example:

    If you have a Text entity that is Field mapped to a Polyline (object), and set to display the area, then want to edit the FieldCode to point to another Polyline (object), then you'd merely extract the FieldCode string, parse the prefix, ObjectId, and suffix, then concatenate them back together with the ObjectId of the target object.


    Cheers


    [Edit] - The simplest way to know the FieldCode you need, is to manually set one up the way you want it, and then examine the FieldCode string, copying from that to hard-code your source, and concatenate the bit(s) you need at runtime.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  6. #6
    Member
    Join Date
    2015-12
    Location
    Santa Clarita, CA
    Posts
    8
    Login to Give a bone
    0

    Default Re: at a loss: Setting Field Values with VBA

    I will try this thank you.

  7. #7
    Member
    Join Date
    2015-12
    Location
    Santa Clarita, CA
    Posts
    8
    Login to Give a bone
    0

    Default Re: at a loss: Setting Field Values with VBA

    Hey everyone it's been a very long time since I started this thread. I just solved the issue and would like to share the answer.

    with the help of this thread: summaryinfo

    I was able to alter the code slightly to change the value of my fields:
    Code:
    Dim Value As String
    Dim Num As Long
    Dim Index As Long
    Dim CustomKey As String
    Dim CustomValue As String
    Dim Sum As AcadSummaryInfo
    Set Sum = ThisDrawing.SummaryInfo
    
    Num = Sum.NumCustomInfo
    
    For Index = 0 To Num - 1
        Sum.GetCustomByIndex Index, CustomKey, CustomValue
        If CustomKey = fillArray(Index) Then
            Sum.SetCustomByIndex Index, CustomKey, "Test"
        End If
    Next Index
    
    Set Sum = Nothing
    I am altering this snippet to fill my fields with values from an excel sheet.

    Just thought I'd share even though the code that helped me has been on here since 2005, it was another post that pointed me in the right direction (summaryinfo) Thanks Augi community

    hopefully this saves someone some heartache and headaches.

Similar Threads

  1. Default field values
    By junkdiver in forum AutoCAD Fields
    Replies: 15
    Last Post: 2017-02-17, 01:06 PM
  2. Editing Field values
    By cadconcepts in forum AutoLISP
    Replies: 1
    Last Post: 2016-04-01, 07:52 PM
  3. object field values scales with VP?
    By randyspear in forum Dynamic Blocks - Technical
    Replies: 3
    Last Post: 2010-03-17, 03:26 PM
  4. HELP!! Setting Values (New user)
    By fets12 in forum AutoCAD General
    Replies: 2
    Last Post: 2007-12-06, 08:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •