Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. Dim ws As Worksheet
  2.  
  3. Set ws = ThisWorkbook.Sheets("Sheet6")
  4.  
  5. Set objWord = CreateObject("Word.Application")
  6.  
  7. objWord.Visible = True
  8.  
  9. objWord.Documents.Open "C:GR1 CPA Test1.docx" ' change as required
  10.  
  11. With objWord.ActiveDocument
  12. .Bookmarks("CN1").Range.Text = ws.Range("C25").Value
  13. .Bookmarks("CN2").Range.Text = ws.Range("C25").Value
  14. .Bookmarks("CNo").Range.Text = ws.Range("C26").Value
  15. .Bookmarks("CL1").Range.Text = ws.Range("C27").Value
  16. .Bookmarks("Ex1").Range.Text = ws.Range("C28").Value
  17. .Bookmarks("Ex2").Range.Text = ws.Range("C28").Value
  18. .Bookmarks("Su1").Range.Text = ws.Range("C29").Value
  19. .Bookmarks("Su2").Range.Text = ws.Range("C29").Value
  20. .Bookmarks("Su3").Range.Text = ws.Range("C29").Value
  21.  
  22. .Save
  23. .Close
  24.  
  25. End With
  26.  
  27. Set objWord = Nothing
  28.  
  29.  
  30. End Sub
  31.  
  32. 'Replace the text in a bookmark or insert text into an empty (zero-length) bookmark
  33. Sub SetBookmarkText(oDoc As Word.Document, sBookmark As String, sText As String)
  34.  
  35. Dim BMRange As Word.Range
  36.  
  37. If oDoc.Range.Bookmarks.Exists(sBookmark) Then
  38. Set BMRange = oDoc.Range.Bookmarks(sBookmark).Range
  39. BMRange.Text = sText
  40. oDoc.Range.Bookmarks.Add sBookmark, BMRange
  41. Else
  42. MsgBox "Bookmark '" & sBookmark & "' not found in document '" & oDoc.Name & "'" & _
  43. vbCrLf & "Content not updated"
  44. End If
  45.  
  46. End Sub
  47.  
  48. Dim ws As Worksheet, doc as object
  49.  
  50. Set ws = ThisWorkbook.Sheets("Sheet6")
  51.  
  52. Set objWord = CreateObject("Word.Application")
  53.  
  54. objWord.Visible = True
  55.  
  56. Set doc = objWord.Documents.Open("C:GR1 CPA Test1.docx")
  57.  
  58. SetBookmarkText doc, "CN1", ws.Range("C25").Value
  59. SetBookmarkText doc, "CN2", ws.Range("C25").Value
  60. 'etc etc
  61.  
  62. doc.Save
  63. doc.Close
  64. Set objWord = Nothing
  65.  
  66. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement