简体   繁体   中英

VBA/VBS Pasting in field not working in Word 2013

I have an VBScript (although I can relate it to VBA) which has:

Set selection = application.Selection
With document.Fields.Add(selection.Range, 35, "Default" , True)
  .Result.Paste
  ...

Note: document variable has been declared before and 35 if field type quote.

Problem In previous versions of Word, pasting would be done in the field itself, therefore replacing "Default" text with new text, but in Word 2013, this will result in deleting "Default" text and pasting data before the field as plain text. So you will end up with plain text and empty field.

I made a workaround which uses selection to paste it in field.

Set application = GetObject(, "Word.Application")

Function PasteInField(f)
    Set s = application.Selection
    'select all except last char and paste data instead of it
    f.Result.Select
    s.SetRange s.Start, s.Start+Len(f.Result.Text)-1
    s.Paste
    'select last char and remove it
    f.Result.Select
    s.SetRange s.End-1, s.End
    s.Delete
end Function

It will basically select all but last char, then paste instead, and lastly, remove remaining character. My field is created like this:

set field = application.ActiveDocument.Fields.Add(selection.Range, 35, "DEFAULT" , True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM