1

I have this .dwg file that has hundreds of block references. I am trying to create hyperlink to a pdf file from all of the block references. The pdf are on my D drive.

For example, names of the block refernece are: '2:test', '26:test', '234:test'. Essentially hyperlink for each point would be: '2:test' would hyperlink to D:\Reports\File-002.pdf; '26:test' would hyperlink to D:\Reports\File-026.pdf; '234:test' would hyperlink to D:\Reports\File-234.pdf.

From block references i get the number before the ':', and its matching pdf would be 'File-' followed by the number before ':' in 3 digits. There are lot of these to do by hands, and i think i can program for this.

I have enough basic programming knowledge to manipulate the string to get my number and convert it in 3 digits. The question i have and/or need help is with how to cycle through each block reference(for loop) on the file and be able to write to its hyperlink property? Is this even possible?

Before coming here i kind of looked at these links but they did not prove helpful: Link1; Link2; Link3

Thanks for the hints

UPDATE

Private Sub CommandButton1_Click()

   Dim ReadData As String

    Open "C:\Desktop\Files\DesignFile.DWG" For Input As #1

    Do Until EOF(1)
       Line Input #1, ReadData
       MsgBox ReadData 'Adding Line to read the whole line, not only first 128 positions

    Loop

    Close #1

End Sub
USER420
  • 337
  • 3
  • 12

2 Answers2

0

You can try this:

Dim stringInput

stringInput = "2:test', '26:test', '234:test"

stringSplit = Split(stringInput, ",")

For i = 0 To UBound(stringSplit)

    Debug.Print (stringSplit(i))
Next i

Outputs:

2:test'

'26:test'

'234:test

Charlie
  • 2,004
  • 6
  • 20
  • 40
  • How can i get vba to automatically detect all the block reference as string input, and also write to their hyperlink property? – USER420 Apr 19 '16 at 18:53
  • Are the refs in a spreadsheet or the .dwg file? If the file then you need to "load them into a string" then my code will work.http://stackoverflow.com/questions/11528694/read-parse-text-file-line-by-line-in-vba – Charlie Apr 19 '16 at 18:58
  • The block reference are in the dwg file. – USER420 Apr 19 '16 at 19:11
  • Did you try the link I suggested to parse the dwg file? – Charlie Apr 19 '16 at 19:13
  • Yes, I looked at that link. But see i cant even load the block reference name. In visual basic editor i created a user form with a command button. I was trying to do this in steps so all i want is to read the block references name in messagebox when i hit the button. I am putting the code in updated section on the question. Sorry if i am not clear, just new to this – USER420 Apr 19 '16 at 19:30
0

you can try this

Option Explicit

Sub test()
Dim acBlockRef As AcadBlockReference    
Dim baseStrng As String

baseStrng = "D:\Reports\File-"    
For Each acBlockRef In BlockRefsSSet("BlockRefs")
    acBlockRef.Hyperlinks.Add("PDF").URL = baseStrng & Format(Left(acBlockRef.Name, InStr(acBlockRef.Name, "-") - 1), "000") & ".pdf"
Next acBlockRef
ThisDrawing.SelectionSets("BlockRefs").Delete

End Sub

'-----------------------------------------------------------------
'helper functions
'------------------
Function BlockRefsSSet(ssetName As String, Optional acDoc As Variant) As AcadSelectionSet
'returns a selection set of all block references in the passed drawing
Dim acSelSet As AcadSelectionSet
Dim Filtertype(0) As Integer
Dim Filterdata(0) As Variant

Set BlockRefsSSet = CreateSelectionSet(ssetName, acDoc)
Filtertype(0) = 0: Filterdata(0) = "INSERT"
BlockRefsSSet.Select acSelectionSetAll, , , Filtertype, Filterdata

End Function


Function CreateSelectionSet(selsetName As String, Optional acDoc As Variant) As AcadSelectionSet
'returns a selection set with the given name
'if a selectionset with the given name already exists, it'll be cleared
'if a selectionset with the given name doesn't exist, it'll be created
Dim acSelSet As AcadSelectionSet

If IsMissing(acDoc) Then Set acDoc = ThisDrawing

On Error Resume Next
Set acSelSet = acDoc.SelectionSets.Item(selsetName) 'try to get an exisisting selection set
On Error GoTo 0
If acSelSet Is Nothing Then Set acSelSet = acDoc.SelectionSets.Add(selsetName) 'if unsuccsessful, then create it

acSelSet.Clear 'cleare the selection set

Set CreateSelectionSet = acSelSet
End Function
'-----------------------------------------------------------------

with following notes:

  • you can't have a colon (":") in a block name

    so I used a hypen ("-") as its substitute

  • every block reference object will be attached the URL ("D:\Reports\File-nnn.pdf") associated with the block name it's a reference of

user3598756
  • 28,893
  • 4
  • 18
  • 28