Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Vba open Word document

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
beyEZWUB
398 Views, 5 Replies

Vba open Word document

Dear community,

 

I'm seeking assistance as I'm encountering difficulty with a task involving a VBA macro.

The objective is to search and select a Word document using the macro. Once the document is selected, it should open automatically, and the text within it should be copied to the clipboard. Subsequently, this copied text should be inserted into the 'comment' property.

 

However, I've encountered a problem. The code provided below successfully enables the selection of a document. Yet, I'm unsure how to proceed with opening Word and completing the subsequent steps.

 

I've attempted solutions suggested by ChatGPT, but they haven't yielded satisfactory results and have generated numerous error messages.

 

Any guidance or assistance would be greatly appreciated.

 

I use the Code below in an User form. 

 

Private Sub Suche_Click()


' Create a new FileDialog object.
Dim oFileDlg As fileDialogCall ThisApplication.CreateFileDialog(oFileDlg)

' Define the filter to select part and assembly files or any file.
oFileDlg.Filter = "Word files (*.doc;*.docx)|*.doc;*.docx|All Files (*.*)|*.*"

' Define the part and assembly files filter to be the default filter.
oFileDlg.FilterIndex = 1

' Set the title for the dialog.
oFileDlg.DialogTitle = "Open File Test"

' Set the initial directory that will be displayed in the dialog.
oFileDlg.InitialDirectory = "This PC"

' Set the flag so an error will be raised if the user clicks the Cancel button.
oFileDlg.CancelError = True

' Show the open dialog. The same procedure is also used for the Save dialog.
' The commented code can be used for the Save dialog.
On Error Resume Next
oFileDlg.ShowOpen
' oFileDlg.ShowSave

' If an error was raised, the user clicked cancel, otherwise display the filename.
If Err Then
MsgBox "User cancelled out of dialog"
ElseIf oFileDlg.FileName <> "" Then
MsgBox "File " & oFileDlg.FileName & " was selected."
End If
End Sub

Tags (3)
5 REPLIES 5
Message 2 of 6
jeffrey4283
in reply to: beyEZWUB

I am not sure if there is way to accomplish this via Word document but you can definitely open Excel documents via iLogic and read/write to the document. This GoExcel link is the help page on it.

 

I use this method to add virtual parts and then populate the iProperty Description which is similar to what you are trying to do it sounds like. It is still a work in progeress but it works for what I was trying to do.

 

' Title: Add Virtual Part (From Excel)
' Description: Adds a virtual part to an assembly. Parts and descriptions are stored on an 
' 	external spreadsheet titled PartNumberTest.xlsx

' Notes:
' - Populate the spreadsheet with common items

' Gets the active document
Dim oDoc = ThisDoc.Document

' Check if this is being run in an assembly document if not it will exit the program
If oDoc.DocumentType = kPartDocumentObject Then
		MessageBox.Show("This is not an assembly. Please run from an assembly")
		Exit Sub
	Else If oDoc.DocumentType = kDrawingDocumentObject Then
		MessageBox.Show("This is not an assembly. Please run from an assembly")
		Exit Sub
	Else If oDoc.DocumentType = kPresentationDocumentObject Then
		MessageBox.Show("This is not an assembly. Please run from an assembly")
		Exit Sub
	Else
End If

' Sets an assembly doc variable
Dim aDoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
' Sets the filepath of the excel document
Dim oXLFile As String = "Add Filepath here\Add file name Here.xlsx"
' Sets a variable for the excel doc sheet
Dim oXLSheet As String = "Sheet 1"
' Sets a vaiable for the iproperty for part number
Dim oPartNumber As String = iProperties.Value("Project", "Part Number")

' Opens the excel file in the background
GoExcel.Open(oXLFile, oXLSheet)

' Creates and adds the part number column values to the array
Dim aryParts As New ArrayList
aryParts = GoExcel.CellValues("C2", "")

' Displays an input listbox populated with values from the Concat column of the spreadsheet
ilbParts = InputListBox("Select an Item to Add", aryParts, d0, Title := "Title", ListName := "List")

If ilbParts = Nothing Then
		MessageBox.Show("You did not select anything!")
		Exit Sub
	Else
End If


' Finds the row in the excel sheet where the Concat column matches the selected value in the input listbox
GoExcel.FindRow(oXLFile, oXLSheet, "Concat", "=", ilbParts)

' Retrieves the part number of the selected item
xlPartNumber = GoExcel.CurrentRowValue("Part Number")
' Retrieves the description of the selected item
xlPartDescription = GoExcel.CurrentRowValue("Description")

' Creates a new matrix for positioning the part
Dim oMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
' Gets the component definition of the active document
Dim oAssDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
' Adds a virtual component occurrence to the assembly with the specified part number and matrix
Dim oNewOcc As ComponentOccurrence = oAssDef.Occurrences.AddVirtual(xlPartNumber, oMatrix)
' Gets the definition of the virtual component
Dim oCVirtualCompDef As VirtualComponentDefinition = oNewOcc.Definition


' Sets the description property of the virtual component to the description retrieved from the excel sheet
oCVirtualCompDef.PropertySets.Item("Design Tracking Properties").Item("Description").Value = xlPartDescription

 

Message 3 of 6
beyEZWUB
in reply to: beyEZWUB

Hi jeffrey thank you for your quick advice, i work also with ilogic and excel and its works great, but in this case i cant change from word to excel.

 

The customer is king:)

Message 4 of 6
jeffrey4283
in reply to: beyEZWUB

That is unfortunate. The problem for you then is as far as I know with the GoExcel functionality you are opening the excel document from within Inventor and as far as I know you cannot do the same with word docs. You might be able to do some janky thing to access word through excel using the more advanced Excel COM functionality but that that is beyond my knowledge.

Message 5 of 6
WCrihfield
in reply to: beyEZWUB

Hi guys.  This is definitely possible because VBA was developed by Microsoft primarily to allow access to their API's, and pretty much every core Microsoft Office application has access to VBA tools for working within their documents.  The VBA in Inventor is basically the same thing, but 'pointing' to Inventor, instead of Microsoft Office stuff at the moment.  You would need first select the VBA project that you will be working in, then while that is selected, go into the Tools menu of the VBA Editor, then choose References.  A dialog will open.  Scroll and check the CheckBox next to the Microsoft Office stuff and the "Microsoft Word 16.0 Object Library" (16.0 may be different on your computer).  That will enable recognition of the API objects within their system, so you can get some feedback and support for those objects.  You can also to a search online for how to do that, because accessing Microsoft Word from VBA is pretty old stuff will a lot of documentation and conversations about online.  Now VBA in Inventor is practically obsolete though.  I would definitely suggest moving to iLogic, instead of VBA.

https://learn.microsoft.com/en-us/office/vba/api/Word.Documents.Open 

https://www.automateexcel.com/vba/word/open-document

https://stackoverflow.com/questions/37708781/opening-word-document-copying-specific-text-paste-into-... 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6
beyEZWUB
in reply to: beyEZWUB

Thanks for the tip, sometimes it's easier than you think

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report