Replace some of the text in word (.docx)

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hi

I have a VBA that opens a word document where I have a standard text, and some of the words are ment to change depending on who customer we have in mind.

It seems a bit unstable, sometimes it takes half of the words, if I add a sign like a comma after the word it seems to catch them better and manage to replace them.

Right now it doesn't change any of the words....

If anyone has any experience with this I would appreciate ideas and suggestions to the code. I think it might be a slow code that I have written.

This is is:


Code:
Option Explicit
 'the document
Dim Inv_doc As Object
 'the application
Dim WD As Object, which_document As Object
Dim FName As String
Dim DesktopB As String
    
Sub AutoNameEdit()
     'where is the template located
     
    FName = ActiveWorkbook.Sheets("Ark3").Range("A2").Value
    DesktopB = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    
    Dim which_document As String
    which_document = DesktopB & "\SalesTools\Intro.docx"
    
    'Set which_document = DesktopB & "\Intro.docx" ' I cannot get the DTAddress to work here
    
         'need an instance of word
    Set WD = CreateObject("Word.Application")
    WD.Visible = True
    Set Inv_doc = WD.Documents.Open(which_document)
     '*** code to manipulate your document
     'replace the text in the document with text in cells
    Call Change_Bookmark("txtCompName", Cells(2, 1).Value)
    Call Change_Bookmark("txtCompNo", Cells(2, 2).Value)
    Call Change_Bookmark("txtStreet", Cells(2, 3).Value)
    Call Change_Bookmark("txtStreetNo", Cells(2, 4).Value)
    Call Change_Bookmark("txtPostNo", Cells(2, 5).Value)
    Call Change_Bookmark("txtStorage", Cells(2, 6).Value)
    Call Change_Bookmark("txtCompRef", Cells(2, 7).Value)
    Call Change_Bookmark("txtCity", Cells(2, 12).Value)
    Call Change_Bookmark("chkCamera", Cells(2, 13).Value)
    Call Change_Bookmark("chkGate", Cells(2, 14).Value)
    Call Change_Bookmark("chkFence", Cells(2, 15).Value)
    WD.Activate
    
    
    Inv_doc.SaveAs DesktopB & "\SalesTools\" & FName & "-" & Date & ".docx"
    Inv_doc.Close
    WD.Quit
    Set Inv_doc = Nothing
    Set WD = Nothing
End Sub
Sub Change_Bookmark(Template_value As String, New_Value As String)
    Dim oword As Object
    For Each oword In Inv_doc.Words
        If oword.Text = Template_value Then
            oword.Text = New_Value
        End If
    Next oword
    Set oword = Nothing
End Sub

Regards
Espen
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Trial this format (adjust sheet name) ...
Code:
Call Change_Bookmark("txtCompName", _
             CStr(Sheets("Sheet1").Cells(2, 1).Value)) [\code] 
There's something punky with your sub  Change_Bookmark (which would really make a better function). You should loop your Word bookmarks using ranges and evaluate/replace their content. HTH. Dave
ps. also to speed things up... in your sub/function once you replace the text you should exit the loop and exit the sub/function
pps skip the wd.activate line of code
 
Last edited:
Upvote 0
Hi Ken

Thank you for the links, I read them all and they were very useful. I have now solved the problem, I discovered that the replacement function is quite sensitive, but works fine when its tuned right.

Yet, I discovered another problem, one of the textboxes where I type my text into - it gives me an error when I run the replacement code. It says that the string is too long. I counted the words, 61 letters. And some lineshifts.

Do you know if I can solve this issu? I read on forums that the limit usually is 255 characters, but maybe my lineshifts are counted too?

Regards
Espen
 
Upvote 0
it is a userform, and it stores the data in a cell in excel. Then I run a macro that replace the text in a word document, thats when I get the error message.
 
Upvote 0

Forum statistics

Threads
1,215,360
Messages
6,124,491
Members
449,166
Latest member
hokjock

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top