Word 2007 vba - 样式不通过宏应用

发布于 2024-09-11 10:43:46 字数 950 浏览 2 评论 0原文

我有一个宏代码,基本上创建了一些表,然后在其中的一些表中键入一些文本,然后放置一些文本占位符。当我尝试将样式应用于文本时出现问题。起初我以为只是占位符不受代码影响。但似乎也没有应用由宏选择的常规文本。

代码基本上如下所示:

Selection.TypeText Text:="Entreprisecost:"
Selection.MoveRight Unit:=wdCell
Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
cc.SetPlaceholderText Text:="Description of the cost"
cc.DefaultTextStyle = "EnterpriseStyle"
Selection.Style = ActiveDocument.Styles("EnterpriseStyle")

请注意我如何定义占位符和选择的样式。

接下来,我尝试录制一个简单的宏,在其中选择整行,然后将样式应用于所选内容。当我录音时这有效。但是当我运行宏时它不起作用。很奇怪:

Selection.MoveUp Unit:=wdLine, Count:=5, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("ExperienceStyle")

为什么会出现这种情况?我的宏安全设置设置为默认中,但我当然选择在打开模板后启用宏。当我打开模板本身以及双击它以基于模板创建新文档时,会发生这种情况。有什么想法吗?

编辑:除了应用样式之外,宏的每一部分都有效。应用该样式的代码已运行,文本不会更改。当我选择文本来检查其样式时,我可以看到该样式已应用。但它不是同时的。奇怪的是,如果我选择文本,然后手动重新应用样式,也就是说,单击已选择的相同样式,然后我会看到该样式确实被应用了。

就好像样式已经被设置但没有实际应用一样。

I have a macro code that basically creates a few tables, then types in some text in some of then, and then places a few text placeholders. The problem appear when I try to apply styles to the text. At first I thought it was only the placeholders that aren't affected by the code. But it seems as though regular text, selected by the macro isn't applied as well.

The code basically looks like this:

Selection.TypeText Text:="Entreprisecost:"
Selection.MoveRight Unit:=wdCell
Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
cc.SetPlaceholderText Text:="Description of the cost"
cc.DefaultTextStyle = "EnterpriseStyle"
Selection.Style = ActiveDocument.Styles("EnterpriseStyle")

Notice how I define the style on both the placeholder AND the selection.

Next, I tried to record a simple macro where I select the entire row, then apply the style to the selection. This works when I'm recording. But it doesn't work when I run the macro. It's strange:

Selection.MoveUp Unit:=wdLine, Count:=5, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles("ExperienceStyle")

Why is this happening? My macro security settings are set to default medium, but I choose of course to enable macros once the template is opened. This happens when I both open the template itself and when i doubleclick it to create a new document based on the template. Any ideas?

Edit: Every bit of the macro works, besides applying styles. The code that applies the style is run, the the text doesn't change. And when I select the text to check which style its in, I can see that the style is applied. But it isn't at the same time. Strange, if I select the text, then manually reapply the style, meaning, clicking on the same style that's already selected, THEN I see that the style is really applied.

It's like the style is being set without it actually being applied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-09-18 10:43:46

首先,您必须帮助我们编写代码并进行设置。我假设您至少有一个六乘二的表格,并在单元格 6,1(底部左侧单元格)中进行选择。其次,不要让我们猜测变量是什么;使用 Dim 语句。第三,我们没有您的样式,所以我将它们更改为标准的 Normal.dot 样式。

话虽如此,您的代码可以正常工作,如下所示。我看到的唯一错误是您在最后一部分中使用了ExperienceStyle,在第一部分中使用了EnterpriseStyle。如果其中任何一个不存在,您都会收到错误消息。

Public Sub Test()
    Selection.TypeText Text:="Entreprisecost:"
    Selection.MoveRight Unit:=wdCell

    Dim cc As ContentControl
    Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Description of the cost"
    cc.DefaultTextStyle = "Title"
    Selection.Style = ActiveDocument.Styles("Title")
    Selection.Style = ActiveDocument.Styles("Strong") 'Proof the style is being changed.

    Selection.MoveUp Unit:=wdLine, Count:=5, Extend:=wdExtend
    Selection.Style = ActiveDocument.Styles("Strong")
End Sub

First, you have to help us out with the code and set up. I assume you have at least a six by two table with the selection in cell 6,1 (bottom, left cell). Second, don't make us guess what the variables are; use Dim statements. Third, we don't have your styles, so I changed them to standard Normal.dot ones.

With that said, your code works fine as below. The only wrong thing I can see is that you used ExperienceStyle in the last part and EnterpriseStyle in the first part. You would get an error if either one did not exist.

Public Sub Test()
    Selection.TypeText Text:="Entreprisecost:"
    Selection.MoveRight Unit:=wdCell

    Dim cc As ContentControl
    Set cc = Selection.Range.ContentControls.Add(wdContentControlText)
    cc.SetPlaceholderText Text:="Description of the cost"
    cc.DefaultTextStyle = "Title"
    Selection.Style = ActiveDocument.Styles("Title")
    Selection.Style = ActiveDocument.Styles("Strong") 'Proof the style is being changed.

    Selection.MoveUp Unit:=wdLine, Count:=5, Extend:=wdExtend
    Selection.Style = ActiveDocument.Styles("Strong")
End Sub
宣告ˉ结束 2024-09-18 10:43:46

我认为你的问题是你在应用新样式之前没有清除格式。尝试这样做并告诉我们是否有效

I think your proble is that you are not clearing format before applying new Style. Try to do that and tell us if it works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文