![]() |
#1
|
|||
|
|||
![]()
Perhaps this is obvious to those familiar with Word VBA. But this example would have saved me hours, if not days, of frustration.
Rather than build an entire word VBA subsystem, I opted to render a roster of vendor and client names and addresses as tabs in an excel spreadsheet to be brought into my word template. It turns out that my wife, who would be using this application, is not comfortable with using Excel data features such as insert row and sort. So I thought to create a form in Excel to do what we old mainframe programmers called "CRUD" (Create, Retrieve, Update, Delete) operations. It worked fine -- in Excel as Roster.xlsm (note module enabled). But when I brought the document into Word, it put the form in never-never land -- well not exactly, but not intuitively accessible either. What worked was obvious in retrospect, but not intuitive either. Here it is. First, define and load as an Excel .xlsx file (withOUT any forms or macros) Code:
Function OpenAWorkbook(ByVal wkFilename As String) As Boolean Dim xlApp As Object ' Dim xlBook As Object 'moved to Declarations On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Err Then Set xlApp = CreateObject("Excel.Application") End If On Error GoTo lblFailed Set xlBook = xlApp.Workbooks.Open(FileName:=wordPath + wkFilename) Load UserFormMaintainRoster UserFormMaintainRoster.Show xlApp.Visible = True lbl_Exit: Set xlApp = Nothing Set xlBook = Nothing OpenAWorkbook = True Exit Function lblFailed: Set xlApp = Nothing Set xlBook = Nothing OpenAWorkbook = False Exit Function End Function Code:
Sub LoadComboBox() 'Based on https://stackoverflow.com/questions/29565846/how-to-populate-a-combobox Dim N As Long With Sheets(sheetName) N = .Cells(Rows.Count, 1).End(xlUp).Row End With With ComboBoxNames .Clear For sheetRow = 2 To N 'skip title row .AddItem Sheets(sheetName).Cells(sheetRow, 1).Value Next sheetRow End With End Sub Code:
Sub DoUpdate() With Sheets(sheetName) .Cells(sheetRow, 1).Value = TextBoxName.Text .Cells(sheetRow, 2).Value = TextBoxAddress.Text .Cells(sheetRow, 3).Value = TextBoxCity.Text If OptionButtonActive.Value = True Then .Cells(sheetRow, 4).Value = isActive Else .Cells(sheetRow, 4).Value = isInactive End If End With End Sub |
![]() |
Tags |
excel 2016, forms vba, vba word |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
derekcentrico | Word VBA | 10 | 12-12-2016 12:27 PM |
![]() |
dherr | Excel Programming | 13 | 12-02-2014 08:31 AM |
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data | Faldinio | Word VBA | 7 | 10-19-2014 06:03 AM |
![]() |
elmousa68 | Word VBA | 5 | 10-15-2013 05:10 PM |
email solution for Excel 2003 | Kat | Excel | 0 | 06-29-2010 04:10 PM |