+ Reply to Thread
Results 1 to 11 of 11

Change letters in microsoft word using vba and excel cell reference

  1. #1
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Change letters in microsoft word using vba and excel cell reference

    Hello! I have a document where I would like to change all letters to another spesific based on excel cell value:
    Sub SelectToBracketsDelete()
    Dim j As Integer
    For j = 2 To 10
    stringReplaced = stringReplaced + "string to be searched"
    For Each myStoryRange In ActiveDocument.StoryRanges
    With myStoryRange.Find
    .Text = workbooks("Bok1.xlsm").Worksheets("Ark1").Cells(j, 2).Value
    .Replacement.Text = workbooks("Bok1").Worksheets("Ark1").Cells(j, 3).Value
    .Wrap = wdFindContinue
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Highlight = False
    .Execute Replace:=wdReplaceAll
    End With

    Next myStoryRange
    Next j


    End Sub
    I got an error: sub og function not defined

    I have values in B column that is searched in word. C column is the replacement. Is it also possible to do this only with first sentence(to first .)?

    I appreciate any help!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Change letters in microsoft word using vba and excel cell reference

    You don't mention where the error occurs but I would assume
    Please Login or Register  to view this content.
    'Workbooks' means nothing to Word unless the code has a reference to Excel

    You need to add a reference to Excel (Tools/References menu)
    You then need to either start Excel and load the source workbook or get a reference to an existing instance which has the workbook loaded (probably easier to start Excel yourself).

    Once you have that working then you can start to debug any issues with the Find/Replace code.

  3. #3
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Change letters in microsoft word using vba and excel cell reference

    Thank you for the answer! Now it says "script out of range" with debug at ".Text = workbooks("Bok1.xlsm").Worksheets("Ark1").Cells(j, 2).Value"

    I added the reference in tools. Bok1.xlsm and Ark 1 is correct. Do you know why it is out of range?

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Change letters in microsoft word using vba and excel cell reference

    You then need to either start Excel and load the source workbook or get a reference to an existing instance which has the workbook loaded
    Did you?

    Bok1.xlsm and Ark 1 is correct
    You probably do have a workbook named 'bok1' which has a worksheet named 'Ark 1' but unless your code has a reference to them it means nothing.

    Have a read of this

  5. #5
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Change letters in microsoft word using vba and excel cell reference

    I tried again with some changes from that website. Now there`s nothing to debug and the numbers will not change. Can you take a look at the files i attach?

    Thank you for the help!
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Change letters in microsoft word using vba and excel cell reference

    Do you want to loop through the Excel range replacing all in Col B with the corresponding value form Col C?

  7. #7
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Change letters in microsoft word using vba and excel cell reference

    Yes. It works now, but some values is wrong. I`m trying to do the same back again, but it doesnt work properly. some letters are converted and some are not. maybe go letter by letter in word and search in excel: if found then change?

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Change letters in microsoft word using vba and excel cell reference

    ...or try this

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Change letters in microsoft word using vba and excel cell reference

    Thank you so much! Didn`t think of offset. it works perfectly! Modified somehow. can to the code several times, then do it backwards several times and its normal!

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Change letters in microsoft word using vba and excel cell reference

    can to the code several times, then do it backwards several times and its normal
    Sorry - I really have no idea what you mean but will assume ending with an exclamation (!) means something good

  11. #11
    Registered User
    Join Date
    03-04-2017
    Location
    norway
    MS-Off Ver
    2016
    Posts
    41

    Re: Change letters in microsoft word using vba and excel cell reference

    Yes, it works perfectly. Created a code in excel so i can "encrypt" a word file to the codes specified in excel

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Batch Change Author on Microsoft Word Documents
    By Chrisdudley7 in forum Word Formatting & General
    Replies: 2
    Last Post: 05-13-2014, 04:43 PM
  2. Replies: 2
    Last Post: 11-03-2008, 09:34 AM
  3. Copy & Paste Microsoft word to Microsoft Excel
    By dbacks3449 in forum Excel General
    Replies: 1
    Last Post: 05-02-2007, 06:49 PM
  4. Replies: 4
    Last Post: 01-20-2006, 01:50 PM
  5. Replies: 3
    Last Post: 12-07-2005, 12:00 PM
  6. Replies: 1
    Last Post: 02-25-2005, 04:06 PM
  7. [SOLVED] How do I import Microsoft Word files into Microsoft Excel?
    By Excel in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 01-08-2005, 09:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1