简体   繁体   English

Visual Basic 2010 - 未声明“函数”

[英]Visual Basic 2010 - “function” not declared

I started a few days ago to write a Macro.我前几天开始写一个宏。 It basically only consists of an UI in wich you can choose some options and based on those it gives you a couple of pages of a word document.它基本上只包含一个用户界面,您可以在其中选择一些选项,并根据这些选项为您提供几页 Word 文档。 It will be started from the .exe in the bin/release folder of the project.它将从项目的 bin/release 文件夹中的 .exe 启动。

I want to add a footer and a header for each page, but I have not been able to do so.我想为每个页面添加一个页脚和一个页眉,但我一直无法这样做。

Whenever I want to use a functionality similar to: "wdHeaderFooterPrimary" as in:每当我想使用类似于:“wdHeaderFooterPrimary”的功能时:

objWord = CreateObject("Word.Application")
objDoc = objWord.Documents.Add
objWord.Visible = True
objWord.Selection.TypeText("")

With objWord.Selection
    .Paragraphs.Alignment = 2
    .Font.size = 9
    .Font.Name = "Arial"
End With

With objWord.Sections(1)
    .Headers(wdHeaderFooterPrimary).Range.Text = "Header text"
    .Footers(wdHeaderFooterPrimary).Range.Text = "Footer text"
End With

It gives me an error, underlining the "wdHeader....".它给了我一个错误,在“wdHeader ....”下划线。 Up to now I have always found a workaround whenever I needed a functionality beginning with "wd...".到目前为止,每当我需要以“wd...”开头的功能时,我总是能找到一种解决方法。 But now I seem to have hit a wall.但现在我似乎碰壁了。 (I got some code from: https://msdn.microsoft.com/en-us/library/office/aa221968(v=office.11).aspx ) (我从: https : //msdn.microsoft.com/en-us/library/office/aa221968(v=office.11 ).aspx 得到了一些代码)

Can someone explain to me how to use it properly?有人可以向我解释如何正确使用它吗? Do I have to change settings?我必须更改设置吗? I am using Visual Basic 2010 Express to write the VBA code.我正在使用 Visual Basic 2010 Express 编写 VBA 代码。

Edit: The exact error is: "wdheaderFooterPrimary" was not declared.编辑:确切的错误是:未声明“wdheaderFooterPrimary”。 (translation following for 2nd part:) Perhaps the object could not be accessed based on its security level (?) (第二部分的翻译如下:)也许无法根据其安全级别访问该对象(?)

我最终创建了一个 wordtemplate.dot 文件,其中包含了我想要的页眉/页脚,并在创建新文件时将其加载到应用程序中。

As a definite solution to the problem, I found this through my researches, tried to work precisely, I give the code;作为问题的明确解决方案,我通过我的研究发现了这一点,试图精确地工作,我给出了代码;

.ActiveDocument.Sections.Item (1) .Footers.Item (1) .Range.text = "Write your own text" .ActiveDocument.Sections.Item (1) .Footers.Item (1) .Range.text = "写你自己的文字"

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

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