改变Word文本而不丢失图像 [英] Alter Word text without losing images

查看:75
本文介绍了改变Word文本而不丢失图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VBA自定义单词附件。我有一个模板word文档,我打开,更改文本并保存。这工作正常,但我更改文本时删除了文档顶部的图像。 


我没有生成模板文档,看起来图像只是InlineShapes粘贴在文档的顶部。  


这基本上是我第一次使用Word与VBA合作,因此答案可能非常简单。我不明白为什么图像正在消失。我想也许我可以复制图像然后在文本更改后将它们粘贴回来但是 
做 变量= doc.InlineShapes(1)导致错误,变量= doc.InlineShapes(1).copy()。 


感谢任何建议。 




 Sub test_word_attach()
filedump =" C:\ out files \"
'在哪里放置生成的单词附件。'


fp =" C:\locationofwordtemplate \documentname.docx"
设置myWord = CreateObject(" Word.Application")
设置doc = myWord.Documents.Open(fp,ReadOnly = False)

bodytxt = doc.Content.Text

new_body_text =替换(bodytxt,"Sir或Madam","MY NAME")
doc.Content.Text = new_body_text'这是InlineShapes消失的地方

doc.SaveAs2(filedump&" newfile"&" docx")
doc.SaveAs2 filedump& " newfile中" &安培; " .pdf",17

End Sub



解决方案

尝试以下方面:

 Sub test_word_attach()
Dim filedump As String,fp As String
Dim myWord As Object,doc As Object
filedump =" C:\out files \"
'将生成的单词附件放在何处。'
fp =" C:\locationofwordtemplate\documentname.docx"
设置myWord = CreateObject(" Word.Application")
设置doc = myWord.Documents.Open(fp ,, False,False)
使用doc
使用.Range。找到
.Text =" Sir或Madam"
.Replacement.Text =" MY NAME"
。执行替换:= wdReplaceAll

结尾.SaveAs2 filedump& " newfile中" &安培; " docx",12 ,,, False
.SaveAs2 filedump& " newfile中" &安培; " .pdf",17 ,,, False

End Sub结束


I am trying to customize word attachments with VBA. I have a template word document that I open, alter the text and save. That works fine except the images at the top of the document are removed when I change the text. 

I didn't originate the template document, it appears the images are just InlineShapes pasted at the top of the document.  

This is essentially the first time I've worked with VBA with Word so the answer could be very simple. I don't understand why the images are disappearing. I thought maybe I could copy the images and then paste them back in after the text changes but  doing  variable = doc.InlineShapes(1) causes an error as does variable = doc.InlineShapes(1).copy(). 

Any suggestions are appreciated. 


Sub test_word_attach()
   filedump = "C:\out files\" 
'where to put the word attachments that are generated.'

   
    fp = "C:\locationofwordtemplate\documentname.docx"
    Set myWord = CreateObject("Word.Application")
    Set doc = myWord.Documents.Open(fp, ReadOnly = False)
    
    bodytxt = doc.Content.Text
    
    new_body_text = Replace(bodytxt, "Sir or Madam", "MY NAME")
    doc.Content.Text = new_body_text 'this is where the InlineShapes disappear

    doc.SaveAs2 (filedump & "newfile" & " docx")
    doc.SaveAs2 filedump & "newfile" & ".pdf", 17  

End Sub
     


解决方案

Try something along the lines of:

Sub test_word_attach()
Dim filedump As String, fp As String
Dim myWord As Object, doc As Object
filedump = "C:\out files\"
'where to put the word attachments that are generated.'
fp = "C:\locationofwordtemplate\documentname.docx"
Set myWord = CreateObject("Word.Application")
Set doc = myWord.Documents.Open(fp, , False, False)
With doc
  With .Range.Find
    .Text = "Sir or Madam"
    .Replacement.Text = "MY NAME"
    .Execute Replace:=wdReplaceAll
  End With
  .SaveAs2 filedump & "newfile" & " docx", 12, , , False
  .SaveAs2 filedump & "newfile" & ".pdf", 17, , , False
End With
End Sub


这篇关于改变Word文本而不丢失图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆