The following may be of use to you.
I have not used them but saved them in my code snippets when I saw them online.
' Pragya Chalise
'
https://stackoverflow.com/questions/637 ... from-excel
' I put this in my normal template because I like the code. I do not understand all of it.
' Two of the procedures modify the Text right-click menu in Word to give access to an Excel handling macro in that Text right-click menu.
' I suspect that the ones dealing with the Text right-click menu should also go into the This Document module.
'
'
' ==========================================================
'
'
Code: Select all
Private Function FileOpenDialogBox()
'Display a Dialog Box that allows to select a single file.
'The path for the file picked will be stored in fullpath variable
With Application.FileDialog(msoFileDialogFilePicker)
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
'Show the dialog box
.Show
'Store in fullpath variable
fullpath = .SelectedItems.Item(1)
FileOpenDialogBox = fullpath
End With
'MsgBox FileOpenDialogBox
End Function
'
' ==========================================================
'
Code: Select all
Sub WorkOnAWorkbook()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String, msg1 As String
Dim val1, val2 As String
'specify the workbook to work on
WorkbookToWorkOn = FileOpenDialogBox
'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
End If
On Error GoTo Err_Handler
'If you want Excel to be visible, you could add the line: oXL.Visible = True here; but your code will run faster if you don't make it visible
'Open the workbook
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)
'Process each of the spreadsheets in the workbook
For Each oSheet In oXL.ActiveWorkbook.Worksheets
'put guts of your code here
' msg = msg & oSheet.Range("A1").Value
If oSheet.Name = "Sheet1" Then
lastrow = oSheet.Cells(oSheet.Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
' MsgBox "last used row in col A is " & lastrow
val1 = oSheet.range("A" & i).Value 'value of the bookmark
val2 = oSheet.range("B" & i).Value
ActiveDocument.Bookmarks.Add Name:=val1, range:=Selection.range
'update bookmark if bookmark exists
If ActiveDocument.Bookmarks.Exists(val1) = True Then
UpdateBookmark (val1), (val2)
'MsgBox i
j = j + 1 'counts number of bookmarks updated
ElseIf ActiveDocument.Bookmarks.Exists(val1) = False Then
k = k + 1 'gives total of bookmarks not found
End If
Next i
End If
'get next sheet
Next oSheet
'Exit Sub
'MsgBox msg, , msg1
If ExcelWasNotRunning Then
oXL.Quit
End If
'Make sure you release object references.
Set oRng = Nothing
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
'quit
Call update_all_bookmarks 'update all bookmarks
MsgBox j & " Bookmarks updated!."
Exit Sub
Err_Handler:
MsgBox WorkbookToWorkOn & " caused a problem. " & vbNewLine & err.Description, vbCritical, _
"Error: " & err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If
End Sub
'
' ==========================================================
'
Code: Select all
Private Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).range
BMRange.Text = TextToUse
'ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
'
' ==========================================================
'
Code: Select all
Private Sub update_all_bookmarks()
' select the document and update the macro
With Selection
.WholeStory
.Fields.Update
.MoveLeft Unit:=wdCharacter, Count:=1
End With
End Sub
'
' ==========================================================
'
Code: Select all
Sub RightClickMenu()
Dim MenuButton As CommandBarButton
With CommandBars("Text")
Set MenuButton = .Controls.Add(msoControlButton)
With MenuButton
.Caption = "Update from excel"
.Style = msoButtonCaption
.OnAction = "WorkOnAWorkbook"
End With
End With
End Sub
'
' ==========================================================
'
Code: Select all
Sub ResetRightClick()
Application.CommandBars("Text").Reset
End Sub
'
'
' ==========================================================
'
'
'
' The following goes to the MyDocument for these to work:
Code: Select all
Private Sub Document_Close()
ResetRightClick
End Sub
Code: Select all
Private Sub Document_Open()
Call RightClickMenu
End Sub