简体   繁体   English

从Excel到Word的VBScript

[英]VBScript from Excel to Word

I hope someone can please help me with this. 我希望有人可以帮助我。 The VB script below is in Excel and creates a new Word document. 下面的VB脚本在Excel中,并创建一个新的Word文档。 The code will copy a picture from Excel into Word. 该代码会将图片从Excel复制到Word。 Then it will create and position a TextBox over the picture. 然后它将创建一个TextBox并将其放置在图片上。 It will then put text in the TextBox. 然后它将文本放在TextBox中。 This code does work correctly. 此代码可以正常工作。

Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add

Sheets("Sheet1").Shapes("Picture1").Copy
wrdApp.Selection.Paste


With wrdDoc.Shapes.AddTextBox(msoTextOrientationHorizontal, 200, 150, 96, 50)
    .Select
    .Name = "TextBox1"
End With


With wrdApp.Selection
    .ShapeRange.Fill.Visible = 0
    .ShapeRange.Line.Visible = 0
    .TypeText Text:="My picture text"
End With

As mentioned, the above code does work. 如上所述,以上代码确实有效。 The problem is I need to put this in a loop to copy the picture again (an X number of times). 问题是我需要将其放入循环中以再次复制图片(X倍)。 When I write the code to put it in a loop, it will paste the additional picture within the TextBox because that was last selected. 当我编写将其放入循环中的代码时,它会将其他图片粘贴到TextBox中,因为该图片是最后选择的。 I somehow need to code something like the Ctrl + End keys so it will bring the curser to the end of the document and outside of the TextBox. 我不知何故需要编写Ctrl + End键之类的代码,以便将光标置于文档的末尾和TextBox的外部。 I can't get that to work. 我不能让它工作。

In order to do that I need to use this code: 为此,我需要使用以下代码:

Selection.EndKey Unit:=wdStory

So the proper place to insert it within the code should be this section, like what I have below: 因此,将其插入代码中的适当位置应该是此部分,就像我下面的内容:

With wrdApp.Selection
    .ShapeRange.Fill.Visible = 0
    .ShapeRange.Line.Visible = 0
    .TypeText Text:="My picture text"
    .EndKey Unit:=wdStory
End With

This gives me a VB error when I add that line in. Now if I copy this code into Word and run it from the Macro, it will work perfectly. 当我添加该行时,这给了我一个VB错误。现在,如果我将此代码复制到Word中并从宏中运行它,它将可以正常工作。 However, this code is in Excel and has to create everything in Word. 但是,此代码在Excel中,并且必须在Word中创建所有内容。

Can someone please tell me how I can get it so it moves the curser out of the TextBox and to the end of the document? 有人可以告诉我如何获得它,以便将光标从TextBox移到文档末尾吗? I know the issue is because the code is in Excel and trying to go to Word, there has to be some way to get it to work or a similar code using ASCII. 我知道问题是因为代码在Excel中并且尝试转到Word,所以必须有某种方法使其能够工作或使用ASCII的类似代码。 I have tried everything I can think of and searched for hours for a solution. 我已经尝试了所有可以想到的方法,并花了数小时寻找解决方案。

Thanks for any help someone can provide, 感谢您提供的任何帮助,

Chris 克里斯

If I understand your question correctly, this works for me: 如果我正确理解了您的问题,那么这对我有用:

With wrdapp.ActiveDocument
  .Range(.Range.Characters.Count, .Range.Characters.Count).Select
End With

Edit 编辑

Try this (add stuff, remove stuff, see what works): 尝试以下操作(添加,删除,查看有效的方法):

With wrdapp.ActiveDocument
  .Range(.Range.Characters.Count, .Range.Characters.Count).Select
  .GoTo wdgotobookmark, name:="\EndOfDoc"
  Selection.Move wdCharacter, .Characters.Count
End With

Though this might not be the ideal way to go, the code below will clear the selection in Word, moving the focus out of the textbox. 尽管这可能不是理想的方法,但是下面的代码将清除Word中的选择,将焦点移出文本框。

wrdApp.Activate
SendKeys "{ESC}"

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

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