Hmmmmm? Request for assistance post deleted by a moderator (at the request of the poster) after a solution was posted (without acknowledgement of the assistance provided) or any comment as to why the query was deleted? Most unusual. I'll return to volunteering my time at other sites. Dave
Posts by DLS
-
-
Hi rharri1972. I'm not real sure about your needs for double clicking and/or selecting a select button? If you want to select the part number in the listbox as U type, it seems the following code will work. You will need to adjust the sheet name and range to where your part numbers are located. U need a userform1 that has a listbox1 and a textbox1 on it. HTH. Dave
Userform code...
Code
Display MoreDim Arr As Variant Private Sub UserForm_Initialize() '***change sheet name and range to suit Arr = Sheets("sheet1").Range("A1:A10") With UserForm1.ListBox1 .List = Arr End With End Sub Private Sub TextBox1_Change() Dim Cnt As Integer, Cnt2 As Integer UserForm1.ListBox1.ListIndex = -1 For Cnt = 1 To Len(UserForm1.TextBox1.Text) For Cnt2 = LBound(Arr) To UBound(Arr) If Left(Arr(Cnt2, 1), Cnt) = UserForm1.TextBox1.Text Then UserForm1.ListBox1.ListIndex = Cnt2 - 1 Exit Sub End If Next Cnt2 Next Cnt End Sub
-
Hi Akhus. Seems like you're not having much luck with this one. The likely reason is that the Word document(s) are poorly formatted for data retrieval. This will get U started. You will need to change the folder path to suit. Expand the "SearchWord" array to include all of the search items in the same order of your Sample wb headers.... note that the SearchWord items are case sensitive and must be what is in the document NOT what U have for headers in your Sample wb. Good luck. Dave
Code
Display MoreSub test() Dim WrdApp As Object, Cnt As Integer, FileStr As String, FlCnt As Integer Dim WrdDoc As Object, TblCell As Variant, SearchWord() As Variant Dim FSO As Object, FolDir As Object, FileNm As Object, ColCnt As Integer '*** SearchWord is case sensitive and must be the same as the Word doc SearchWord = Array("Name", "Husband's name", "Maternal UHID", "Age") 'start Word On Error Resume Next Set WrdApp = GetObject(, "Word.Application") If Err.Number <> 0 Then On Error GoTo ErFix Set WrdApp = CreateObject("Word.Application") End If WrdApp.Visible = False FlCnt = 1 Set FSO = CreateObject("scripting.filesystemobject") '***change directory to suit Set FolDir = FSO.GetFolder("D:\testfolder2") 'loop files For Each FileNm In FolDir.Files If FileNm.Name Like "*" & ".docx" Then FileStr = CStr(FileNm) Set WrdDoc = WrdApp.Documents.Open(FileStr) FlCnt = FlCnt + 1 'admission Sheets("sheet1").Range("A" & FlCnt) = Right(Left(WrdApp.ActiveDocument.Paragraphs(3), 29), 10) 'discharge Sheets("sheet1").Range("B" & FlCnt) = Right(WrdApp.ActiveDocument.Paragraphs(3), 12) 'loop through table cells ColCnt = 2 For Cnt = LBound(SearchWord) To UBound(SearchWord) For Each TblCell In WrdApp.ActiveDocument.Tables(1).Range.Cells If InStr(TblCell.Range, SearchWord(Cnt)) Then ColCnt = ColCnt + 1 Sheets("sheet1").Cells(FlCnt, ColCnt) = WrdApp.ActiveDocument.Tables(1).Cell(TblCell.RowIndex, TblCell.ColumnIndex) 'remove pilcrow Sheets("sheet1").Cells(FlCnt, ColCnt) = Application.WorksheetFunction.Clean(Sheets("sheet1").Cells(FlCnt, ColCnt)) 'remove searchword plus 2 (:_) Sheets("sheet1").Cells(FlCnt, ColCnt) = Right(Sheets("sheet1").Cells(FlCnt, ColCnt), _ Len(Sheets("sheet1").Cells(FlCnt, ColCnt)) - (Len(SearchWord(Cnt)) + 2)) Exit For End If Next TblCell Next Cnt 'close and save doc WrdApp.ActiveDocument.Close savechanges:=False Set WrdDoc = Nothing End If Next FileNm Set FolDir = Nothing Set FSO = Nothing WrdApp.Quit Set WrdApp = Nothing MsgBox "Finished" Exit Sub ErFix: On Error GoTo 0 MsgBox "error" Set FolDir = Nothing Set FSO = Nothing Set WrdDoc = Nothing WrdApp.Quit Set WrdApp = Nothing End Sub
-
Hi Harry. I've had this problem before with copying sheets containing named ranges from wb to wb. I resolved it by adding the sheets to be copied to a collection and then copying the collection items to the wb. HTH. Dave
-
Re: "i'm A Spammer" Slander.
Thanks Dave. That is much better. Have a nice day. Dave
-
What's up with the label on my user profile? I'm not a spammer and I don't think that I deserve this slander. Dave
-
-
Re: Create Word Report From Named Ranges
Your use of Path is an issue. Here's some archive code that should help. Good luck! Dave
ps. sorry code author unkownCode
Display MoreSub test() Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim BMRange As Word.Range Dim company As Range Dim address As Range Dim address2 As Range Dim city As Range Dim state As Range Dim zip As Range Dim CuurentDate As Date Dim Cnt As Integer Dim LastRow As Integer LastRow = Sheets("sheet1").UsedRange.End(xlDown).Row Set wdApp = CreateObject("Word.Application") 'Create an instance of word For Cnt = 2 To LastRow Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\mwildrick\Desktop\TEST\Publisher Payment Form.dotm") 'Open word file 'now set your excel ranges Set company = ThisWorkbook.Sheets("CompanyInfo").Range("A" & Cnt) Set address = ThisWorkbook.Sheets("CompanyInfo").Range("B" & Cnt) Set city = ThisWorkbook.Sheets("CompanyInfo").Range("D" & Cnt) Set state = ThisWorkbook.Sheets("CompanyInfo").Range("E" & Cnt) Set zip = ThisWorkbook.Sheets("CompanyInfo").Range("F" & Cnt) 'Set your word bookmark Set BMRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="company") BMRange.Text = company Set BMRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="Address") BMRange.Text = address Set BMRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="City") BMRange.Text = city Set BMRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="State") BMRange.Text = state Set BMRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="Zip") BMRange.Text = zip 'Save your word doc With wdApp.ActiveDocument .SaveAs ThisWorkbook.Path & "\" & company & ".doc" .Close End With Next Cnt 'Close out word wdApp.Quit Set BMRange = Nothing Set wdDoc = Nothing Set wdApp = Nothing End Sub
-
Re: Create Word Report From Named Ranges
I think you need to open the .dot file containing the bookmarks not add it. Also your use of Path as a String variable is probably not good as Path has a specific meaning. HTH. Dave
-
Re: Cell For Cell Data Transfer Script
I was sort of wanting to learn how to do this, so here's some code to transfer an XL range cell to cell to a Word table. The Word table is generated by XL and sized by the XL range. In this test the used range is the XL range. There's abit of extra code for inserting text before and after the table. Not sure about your header aches but I hope this is of some benefit for you. Have a nice day. Dave
Code
Display MoreSub XLCellToWordTableCell() Dim oWDBasic As Object, Rng As Range, Txtstr As String Dim wrdDoc As Object, Ocell As Variant, TC As Variant Dim Lastrow As Integer, Lastcol As Integer, Cnt As Integer 'adds text & table to Word doc from XL 'makes Word Table and adds used range to table 'no Word reference required On Error GoTo ErFix 'open existing word doc file ie. "D:\tabletest.doc" '*** "D:\tabletest.doc" MUST exist ie. change as needed Set oWDBasic = CreateObject("Word.Application") Set wrdDoc = oWDBasic.Documents.Open(Filename:="D:\tabletest.doc") 'clear doc With wrdDoc .Range(0, .Characters.Count).Delete End With 'add text to top of doc and spaces before table oWDBasic.ActiveDocument.Select With oWDBasic.Selection .typetext Text:=" *** Table Test *** " & vbCrLf .typeparagraph .typeparagraph End With 'determine table sixe from Xl range (used range in this eg.) Lastrow = Sheets("Sheet1").UsedRange.Rows.Count Lastcol = Sheets("Sheet1").UsedRange.Columns.Count 'add variable sized table With wrdDoc ' insert table at current selection point .Tables.Add oWDBasic.Selection.Range, numrows:=Lastrow, _ Numcolumns:=Lastcol End With 'autoformat table/table options 'wdTableFormat3DEffects1 (0) 'etc 'wdTableFormatWeb3 (42) 'table autoformat #'s 0 to 42 (3 in this eg.) oWDBasic.ActiveDocument.Tables(1).AutoFormat Format:=3, _ ApplyBorders:=True, ApplyFont:=True, ApplyColor:=True 'vba set XL range With Sheets("Sheet1") Set Rng = .Range(.Cells(1, 1), .Cells(Lastrow, Lastcol)) End With 'insert XL cell.value to table location Cnt = 1 For Each Ocell In Rng Set TC = oWDBasic.ActiveDocument.Tables(1).Range.Cells(Cnt) TC.Range.InsertAfter Ocell.Value Cnt = Cnt + 1 Next Ocell 'fit table and add adjustments/margins With oWDBasic.ActiveDocument.Tables(1) .Columns.AutoFit '.Rows.SetLeftIndent LeftIndent:=-57.6, RulerStyle:=False '.Columns(3).SetWidth ColumnWidth:=153.3, RulerStyle:=False '.Columns(4).SetWidth ColumnWidth:=144, RulerStyle:=False End With 'carry on with remaining doc contents 'eg. make text string with space following table Txtstr = vbCrLf & "Test finished at: " & Now() 'add text string after table With oWDBasic.ActiveDocument .content.InsertAfter Txtstr End With 'close and save doc oWDBasic.ActiveDocument.Close savechanges:=True Set wrdDoc = Nothing oWDBasic.Quit Set oWDBasic = Nothing MsgBox "Finished" Exit Sub ErFix: On Error GoTo 0 MsgBox "error" Set wrdDoc = Nothing oWDBasic.Quit Set oWDBasic = Nothing End Sub
-
Re: Cell For Cell Data Transfer Script
I was messing around with XL and Word tables here. Perhaps this will help. Dave
-
Re: Search For Any Non-alpha Characters
It seems like something like the following code should work for you...it turns any cell containing letters blue. I suspect that you probably want to re-organize the logic to highlight cells if they contain any ascii character other than numbers (this would address those pesky square thingees (end of line/end of paragraph characters) that can occur depending upon the "dumping method"). If the problem is thingees and the only reason your highlighting these cells is to make manual changes (yuck) to remove same thingees, then consider using the Clean function. HTH. Dave
Code
Display MoreSub ColorAlphCells() 'colors cells containing letters Dim i As Integer, Temp As Integer Dim C As Variant, Myrange As Range 'change range to suit Set Myrange = Sheets("sheet1").UsedRange For Each C In Myrange For i = 1 To Len(C.Value) Temp = Asc(Mid(C.Value, i, 1)) If ((Temp > 64) And (Temp < 90)) Or _ ((Temp > 96) And (Temp < 123)) Then C.Interior.ColorIndex = 5 Exit For End If Next i Next C End Sub
-
Re: Formula Entry Into Chart Title
If you want to trial a VBA solution, here's some example code that should get you started. HTH. Dave
Code
Display MoreSub ChartLabel() 'test with sht 1 named range ("year") ... '..sht1 listbox with selection ("Listbox1")... '..sht1 command button ("Commandbutton1")... '..sht1 embed chart... Dim TempStr As String, TempStr2 As String 'make conditional string If Sheets("Sheet1").CommandButton1.BackColor <> _ RGB(0, 128, 64) Then 'green TempStr2 = "PLAN" Else TempStr2 = "ACTUAL" End If 'make string(tempstr) to add named range, listbox txt... '..and conditional string(tempstr2)to chart title tempstr = Sheets("Sheet1").Range("year").Value & _ " " + Sheets("Sheet1").ListBox1.Text + _ " " + TempStr2 'activate chart and add string Sheets("Sheet1").ChartObjects(1).Activate With ActiveChart .HasTitle = True .ChartTitle.Characters.Text = tempstr End With End Sub
-
Re: Use Vba To Reformat Any Bulleted List That Has Lost Its Formatting?
Hi FD. I hope this post will be helpful. It seems I recently endeavoured to achieve something similiar to what your after. If you add some custom styles to your doc you can use VBA to apply them within the context of a find. Perhaps you could search for a keyword or simply the line feed character and then apply the styles you need to reformat the doc. See the posts on styles in this link (just ignore the other dribble). It is XL VBA but perhaps it will give you some ideas to trial. HTH. Dave
http://www.vbaexpress.com/forum/showthread.php?t=10971