简体   繁体   English

如何通过 MS Excel VBA 在 Word 中修改纯文本内容控件

[英]How do I modify Plain Text Content Control in Word via MS Excel VBA

I am using MS Excel VBA to copy Cells from Excel to Word.我正在使用 MS Excel VBA 将单元格从 Excel 复制到 Word。 I established Plain Text Control Fields in my Word Document and add Values from my Excel Document.我在我的 Word 文档中建立了纯文本控制字段,并从我的 Excel 文档中添加了值。

Before I add them via VBA code, I open the Word Document and activate it (like in the code below).在通过 VBA 代码添加它们之前,我打开 Word 文档并激活它(如下面的代码所示)。

Now there are quotation marks in my plain text control fields after inserting the copied text.现在插入复制的文本后,我的纯文本控件字段中有引号。 I want to replace quotation marks in Content Control Object (3) with "space".我想用“空格”替换内容控制对象 (3) 中的引号。

Why do I not just delete it manually?为什么我不手动删除它? Because I established drop down fields in my Excel Document and change them quickly.因为我在 Excel 文档中建立了下拉字段并快速更改它们。 After changing them I generate new Values and insert them again in my word document.更改它们后,我生成新值并将它们再次插入到我的 Word 文档中。

I hope someone can tell me, how to use 'Find """" and Replace "" Command correctly for specific content control objects.我希望有人能告诉我,如何针对特定的内容控制对象正确使用“查找”“”和替换“”命令。

I looked up every single page in the internet, but I couldn´t find any specific commands which I can apply on content control objects.我在互联网上查找了每一页,但找不到可以应用于内容控制对象的任何特定命令。 I appreciate all the answers:)我感谢所有的答案:)

Sub InsertValuesinContentControls_ ()

Dim wdapp As Object
Dim wddoc As Object
Dim strdocname As String


On Error Resume Next

‘If my word doc is not open yet

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

If Err.Number = 429 Then
Err.Clear
Set wdapp = CreateObject("Word.Application")
End If

wdapp.Visible = True

strdocname = "C:\Users\Userxx\File\NameofDocument.docm"

'activate the word doc
wdapp.Activate

Set wddoc = wdapp.Documents(strdocname)

If wddoc Is Nothing Then Set wddoc = wdapp.Documents.Open(strdocname)


'Now I copy single cells in my plain text controls which I established in Word

Worksheets("Generator").Range("F5").Copy
wddoc.ContentControls(1).Range.Paste

Worksheets("Generator").Range("B5").Copy
wddoc.ContentControls(2).Range.Paste

Worksheets("Generator").Range("A11").Copy
wddoc.ContentControls(3).Range.Paste

With wddoc.ContentControls(3)
.Replace What:=””””, Replacement:=””, Lookat:=xlPart, MatchCase:=False

End with


End Sub

sadly I can't comment yet, but did you try with: https://docs.microsoft.com/en-us/office/vba/word/concepts/customizing-word/finding-and-replacing-text-or-formatting遗憾的是,我还不能发表评论,但是您是否尝试过: https : //docs.microsoft.com/en-us/office/vba/word/concepts/customizing-word/finding-and-replacing-text-or-格式化

talking about谈论

With ActiveDocument.Content.Find 
 .ClearFormatting 
 .Font.Bold = True 
 With .Replacement 
 .ClearFormatting 
 .Font.Bold = False 
 End With 
 .Execute FindText:="", ReplaceWith:="", _ 
 Format:=True, Replace:=wdReplaceAll 
End With

You shuld use .find on active document - word, then clear formating and set.find在活动文档上使用.find - word,然后清除格式并设置

.Execute FindText:="", ReplaceWith:="", _
Format:=True, Replace:=wdReplaceAll 

Hope I've helped.希望我有所帮助。 Have a great day.祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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