Word and AutoCAD

Discussion in 'AutoCAD' started by Justin Fisk, Apr 26, 2004.

  1. Justin Fisk

    Justin Fisk Guest

    I am attempting to write some code to insert a selected word document into
    AutoCAD as a Mtext object. I am not a VBA pro, so forgive my crappy code.
    When I attempt to run I get an error then I attempt to open the Word "Open"
    Dialog box (Dialogs(wdDialogFileOpen)) to select the file I want to convert.
    The error " The requested member of the collection does not exist". There
    is probably something very simple I am missing.
    TIA
    Justin

    Public Sub vbdInsertWordDoc()
    Dim strText As String
    Dim varInsPnt As Variant
    Dim objMText As AcadMText
    Dim dblTextWidth As Double
    Dim intCnt As Integer
    Dim strPrompt As String
    Dim intActiveSpace As Integer
    Dim strPath As String
    Static objWordApp As Object
    strPrompt = "Enter mtext width "
    strPath = WordApplicationGetOpenFileName("*.doc", True, True)
    Set objWordApp = Nothing
    Set objWordApp = CreateObject("Word.Application")
    objWordApp.Documents.Open (strPath)
    objWordApp.Visible = True
    ThisDrawing.Utility.InitializeUserInput 1
    intActiveSpace = ThisDrawing.ActiveSpace
    For intCnt = 1 To objWordApp.ActiveDocument.Paragraphs.Count
    objWordApp.ActiveDocument.Paragraphs(intCnt).Range.Select
    strText = strText & objWordApp.Selection.Text & vbCrLf
    Next
    AppActivate ThisDrawing.Application.Caption
    If intActiveSpace = 1 Then
    varInsPnt = ThisDrawing.Utility.GetPoint(, "Pick the text insertion
    point: ")
    dblTextWidth = ThisDrawing.Utility.GetDistance(varInsPnt, strPrompt)
    Set objMText = ThisDrawing.ModelSpace.AddMText(varInsPnt, dblTextWidth,
    strText)
    Else
    varInsPnt = ThisDrawing.Utility.GetPoint(, "Pick the text insertion
    point: ")
    dblTextWidth = ThisDrawing.Utility.GetDistance(varInsPnt, strPrompt)
    Set objMText = ThisDrawing.PaperSpace.AddMText(varInsPnt, dblTextWidth,
    strText)
    End If
    End Sub
    Function WordApplicationGetOpenFileName(FileFilter As String, ReturnPath As
    Boolean, _
    ReturnFile As Boolean) As String
    ' returns the folder and/or filename to a single user selected file
    Dim strFileName As String, strPathName As String
    Static objWordApp As Object
    Set objWordApp = Nothing
    Set objWordApp = CreateObject("Word.Application")
    Set apWordApp = GetObject(, "Word.Application")
    If Not ReturnPath And Not ReturnFile Then Exit Function
    If FileFilter = "" Then FileFilter = "*.*"
    With objWordApp.Dialogs(wdDialogFileOpen)
    .Name = FileFilter
    On Error GoTo MultipleFilesSelected
    If .Display = -1 Then
    strFileName = .Name
    End If
    On Error GoTo 0
    End With
    On Error GoTo 0
    ' remove any "-characters
    If InStr(1, strFileName, " ", vbTextCompare) > 0 Then
    strFileName = Mid$(strFileName, 2, Len(strFileName) - 2)
    End If
    If ReturnPath Then
    strPathName = CurDir & Application.PathSeparator
    Else
    strPathName = ""
    End If
    If Not ReturnFile Then strFileName = ""
    WordApplicationGetOpenFileName = strPathName & strFileName
    MultipleFilesSelected:
    End Function
     
    Justin Fisk, Apr 26, 2004
    #1
  2. Justin Fisk

    Ken Hutson Guest

    Justin,
    Add a project reference to the Microsoft Word object library. I hit another
    bug after that. Had to do with the value of the path string.
    Application.PathSeparator was unknown. Perhaps it's a constant defiined in
    your code.

    Cheers,
    Ken Hutson
    San Antonio, TX
     
    Ken Hutson, Apr 26, 2004
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.